From 321eacfd38e9e9a72391dc765a131badb705f6c9 Mon Sep 17 00:00:00 2001 From: "fabio.sinibaldi" Date: Thu, 21 Mar 2019 17:34:11 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-publishing/gCat-Feeder-Suite@178654 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gCatFeeder/utils/TokenUtils.java | 9 +++ .../gCatFeeder/service/GCatFeeder.java | 7 ++ .../gCatFeeder/service/ServiceConstants.java | 2 + .../gCatFeeder/service/rest/Executions.java | 14 +++- .../gCatFeeder/service/BaseTest.java | 24 +++++- .../gCatFeeder/service/ExecutionsTest.java | 48 +++++++++++ .../mockups/PersistenceManagerMock.java | 79 +++++++++++++++++++ 7 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/ExecutionsTest.java create mode 100644 gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/mockups/PersistenceManagerMock.java diff --git a/commons/src/main/java/org/gcube/data/publishing/gCatFeeder/utils/TokenUtils.java b/commons/src/main/java/org/gcube/data/publishing/gCatFeeder/utils/TokenUtils.java index 32e4e7c..5de7efc 100644 --- a/commons/src/main/java/org/gcube/data/publishing/gCatFeeder/utils/TokenUtils.java +++ b/commons/src/main/java/org/gcube/data/publishing/gCatFeeder/utils/TokenUtils.java @@ -1,5 +1,9 @@ package org.gcube.data.publishing.gCatFeeder.utils; +import static org.gcube.common.authorization.client.Constants.authorizationService; + +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; public class TokenUtils { @@ -11,4 +15,9 @@ public class TokenUtils { public static String getCurrentToken() { return SecurityTokenProvider.instance.get(); } + + public static String getClientId(String token) throws ObjectNotFound, Exception { + AuthorizationEntry entry = authorizationService().get(token); + return entry.getClientInfo().getId(); + } } diff --git a/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/GCatFeeder.java b/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/GCatFeeder.java index d4cb714..0bde7b7 100644 --- a/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/GCatFeeder.java +++ b/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/GCatFeeder.java @@ -35,4 +35,11 @@ public class GCatFeeder extends ResourceConfig{ registerClasses(Capabilities.class); } + + public GCatFeeder(AbstractBinder binder) { + super(); + register(binder); + registerClasses(Executions.class); + registerClasses(Capabilities.class); + } } diff --git a/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/ServiceConstants.java b/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/ServiceConstants.java index aa7327c..6523ff2 100644 --- a/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/ServiceConstants.java +++ b/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/ServiceConstants.java @@ -14,6 +14,8 @@ public class ServiceConstants { public static final String COLLECTOR_ID_PARAMETER="collector"; public static final String CATALOGUE_ID_PARAMETER="collector"; + + public static final String DEFAULT_VALUE="ALL"; } public static interface Capabilities{ diff --git a/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/rest/Executions.java b/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/rest/Executions.java index 6224190..e406673 100644 --- a/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/rest/Executions.java +++ b/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/rest/Executions.java @@ -45,12 +45,18 @@ public class Executions { try { ExecutionRequest request=new ExecutionRequest(); - for(String collector:info.getQueryParameters().get(ServiceConstants.Executions.COLLECTOR_ID_PARAMETER)) - request.addCollectorId(collector); + if(info.getQueryParameters().containsKey(ServiceConstants.Executions.COLLECTOR_ID_PARAMETER)) + for(String collector:info.getQueryParameters().get(ServiceConstants.Executions.COLLECTOR_ID_PARAMETER)) + request.addCollectorId(collector); + else request.addCollectorId(ServiceConstants.Executions.DEFAULT_VALUE); + + if(info.getQueryParameters().containsKey(ServiceConstants.Executions.CATALOGUE_ID_PARAMETER)) for(String catalogue:info.getQueryParameters().get(ServiceConstants.Executions.CATALOGUE_ID_PARAMETER)) - request.addCollectorId(catalogue); - + request.addControllerId(catalogue); + else request.addControllerId(ServiceConstants.Executions.DEFAULT_VALUE); + + log.trace("Submitting request {} ",request); return engine.submit(request); diff --git a/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/BaseTest.java b/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/BaseTest.java index 9791bf6..32f3dac 100644 --- a/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/BaseTest.java +++ b/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/BaseTest.java @@ -5,6 +5,17 @@ import java.sql.SQLException; import javax.ws.rs.core.Application; +import org.gcube.data.publishing.gCatFeeder.service.engine.CatalogueControllersManager; +import org.gcube.data.publishing.gCatFeeder.service.engine.CollectorsManager; +import org.gcube.data.publishing.gCatFeeder.service.engine.ExecutionManager; +import org.gcube.data.publishing.gCatFeeder.service.engine.FeederEngine; +import org.gcube.data.publishing.gCatFeeder.service.engine.PersistenceManager; +import org.gcube.data.publishing.gCatFeeder.service.engine.impl.CatalogueControllersManagerImpl; +import org.gcube.data.publishing.gCatFeeder.service.engine.impl.CollectorsManagerImpl; +import org.gcube.data.publishing.gCatFeeder.service.engine.impl.ExecutionManagerImpl; +import org.gcube.data.publishing.gCatFeeder.service.engine.impl.FeederEngineImpl; +import org.gcube.data.publishing.gCatFeeder.service.mockups.PersistenceManagerMock; +import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.test.JerseyTest; import org.junit.BeforeClass; @@ -20,7 +31,18 @@ public class BaseTest extends JerseyTest{ @Override protected Application configure() { - return new GCatFeeder(); + AbstractBinder binder = new AbstractBinder() { + @Override + protected void configure() { + bind(FeederEngineImpl.class).to(FeederEngine.class); + bind(CatalogueControllersManagerImpl.class).to(CatalogueControllersManager.class); + bind(CollectorsManagerImpl.class).to(CollectorsManager.class); + bind(ExecutionManagerImpl.class).to(ExecutionManager.class); + bind(PersistenceManagerMock.class).to(PersistenceManager.class); + } + }; + + return new GCatFeeder(binder); } } diff --git a/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/ExecutionsTest.java b/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/ExecutionsTest.java new file mode 100644 index 0000000..5e002f2 --- /dev/null +++ b/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/ExecutionsTest.java @@ -0,0 +1,48 @@ +package org.gcube.data.publishing.gCatFeeder.service; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import org.junit.Test; + +public class ExecutionsTest extends BaseTest { + + + @Test + public void getAll() { + WebTarget target= + target(ServiceConstants.Executions.PATH); + + System.out.println(target.getUri()); + Response resp=target.request().get(); + System.out.println(resp.getStatus() + " : "+ resp.readEntity(String.class)); + if(resp.getStatus()!=200) throw new RuntimeException("GetAll error should never happen"); + } + + @Test + public void submitALLAndPoll() { + WebTarget target= + target(ServiceConstants.Executions.PATH); + + System.out.println(target.getUri()); + Response resp=target.request().post(Entity.json("")); + System.out.println(resp.getStatus() + " : "+ resp.readEntity(String.class)); +// if(resp.getStatus()!=200) throw new RuntimeException("GetAll error should never happen"); + + + + } + + @Test + public void wrongSubmission() { + WebTarget target= + target(ServiceConstants.Executions.PATH). + queryParam(ServiceConstants.Executions.CATALOGUE_ID_PARAMETER, TestCommon.FAKE_CATALOGUE_ID+"_NOT"); + + System.out.println(target.getUri()); + Response resp=target.request().post(Entity.json("")); + if(resp.getStatus()!=400) throw new RuntimeException("Expected ERROR STATUS 400 BUT received "+resp.getStatus()); + } + +} diff --git a/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/mockups/PersistenceManagerMock.java b/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/mockups/PersistenceManagerMock.java new file mode 100644 index 0000000..df76ee4 --- /dev/null +++ b/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/mockups/PersistenceManagerMock.java @@ -0,0 +1,79 @@ +package org.gcube.data.publishing.gCatFeeder.service.mockups; + +import java.util.Collection; +import java.util.Collections; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import javax.inject.Singleton; + + +import java.util.function.BiFunction; + +import org.gcube.data.publishing.gCatFeeder.service.engine.PersistenceManager; +import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionDescriptor; +import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionDescriptorFilter; +import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionRequest; +import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionStatus; +import org.gcube.data.publishing.gCatFeeder.service.model.fault.ElementNotFound; +import org.gcube.data.publishing.gCatFeeder.service.model.fault.InvalidRequest; +import org.gcube.data.publishing.gCatFeeder.service.model.fault.PersistenceError; + +@Singleton +public class PersistenceManagerMock implements PersistenceManager { + + @Override + public ExecutionDescriptor create(ExecutionRequest request) throws PersistenceError, InvalidRequest { + return getEquivalent(request); + } + + @Override + public ExecutionDescriptor getById(String id) throws PersistenceError, ElementNotFound, InvalidRequest { + if(theMap.containsKey(id)) + return theMap.get(id); + else throw new ElementNotFound("Unable to find request with id "+id); + } + + @Override + public Collection get(ExecutionDescriptorFilter filter) + throws PersistenceError, InvalidRequest { + return Collections.emptyList(); + } + + @Override + public boolean update(ExecutionDescriptor toUpdate) throws PersistenceError, ElementNotFound { + if(theMap.containsKey(toUpdate.getId())) { + theMap.replace(toUpdate.getId(), toUpdate); + return true; + } + else throw new ElementNotFound("Unable to find request with id "+toUpdate.getId()); + } + + @Override + public boolean acquire(String id) throws PersistenceError, ElementNotFound { + theMap.computeIfPresent(id, BiFunction bi = (x,y) ->{ + + }); + } + + // Actuall persistence in map + + private ConcurrentHashMap theMap=new ConcurrentHashMap<>(); + + private ExecutionDescriptor getEquivalent(ExecutionRequest req) { + ExecutionDescriptor toReturn=new ExecutionDescriptor(); + toReturn.setCallerContext("TEST"); + toReturn.setCallerIdentity("TESTER"); + toReturn.setCallerEncryptedToken("TEST"); + + toReturn.setCatalogues(req.getToInvokeControllers()); + toReturn.setCollectors(req.getToInvokeCollectors()); + + toReturn.setId(UUID.randomUUID().toString()); + + toReturn.setStatus(ExecutionStatus.PENDING); + theMap.put(toReturn.getId(), toReturn); + return toReturn; + } + +}