From c1d3e71f41099077e432ebbbc1cb9ca09f89df9e Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 9 Feb 2016 16:33:06 +0000 Subject: [PATCH] refs #2112: Port SmartExecutor to be compliant with new Authorization https://support.d4science.org/issues/2112 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@124010 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../executor/SmartExecutorInitializator.java | 24 ++++++------- .../jsonbased/JSONLaunchParameter.java | 14 ++++---- ...SmartExecutorPersistenceConfiguration.java | 2 +- .../SmartExecutorPersistenceFactory.java | 10 +++--- .../couchdb/CouchDBPersistenceConnector.java | 6 ++-- .../PluginStateEvolutionObjectNode.java | 2 +- .../executor/SmartExecutorImplTest.java | 27 -------------- ...SmartExecutorPersistenceConnectorTest.java | 11 ++++-- .../pluginmanager/RunnablePluginTest.java | 35 +++++++++---------- .../SmartExecutorSchedulerTest.java | 27 ++++---------- .../executor/pluginmanager/TestUtility.java | 14 ++++++++ 11 files changed, 74 insertions(+), 98 deletions(-) create mode 100644 src/test/java/org/gcube/vremanagement/executor/pluginmanager/TestUtility.java diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 6591f42..a1fb98d 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -46,7 +46,7 @@ public class SmartExecutorInitializator implements ApplicationManager { public static final long JOIN_TIMEOUT = 1000; - public static String getScope(){ + public static String getScopeFromToken(){ String token = SecurityTokenProvider.instance.get(); AuthorizationEntry authorizationEntry; try { @@ -65,14 +65,14 @@ public class SmartExecutorInitializator implements ApplicationManager { * @throws RegistryNotFoundException if the Registry is not found so the * resource has not be published */ - private static void publishScopedResource(Resource resource) throws Exception { + private static void publishResource(Resource resource) throws Exception { StringWriter stringWriter = new StringWriter(); Resources.marshal(resource, stringWriter); RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); try { - logger.debug("Trying to publish to {}:\n{}", getScope(), stringWriter); + logger.debug("Trying to publish to {}:\n{}", getScopeFromToken(), stringWriter); registryPublisher.create(resource); } catch (Exception e) { logger.error("The resource was not published", e); @@ -86,14 +86,14 @@ public class SmartExecutorInitializator implements ApplicationManager { * @throws RegistryNotFoundException if the Registry is not found so the * resource has not be published */ - private static void unPublishScopedResource(Resource resource) throws Exception { + private static void unPublishResource(Resource resource) throws Exception { //StringWriter stringWriter = new StringWriter(); //Resources.marshal(resource, stringWriter); RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); String id = resource.id(); - logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScope()); + logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScopeFromToken()); registryPublisher.remove(resource); @@ -224,11 +224,11 @@ public class SmartExecutorInitializator implements ApplicationManager { for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { try { logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", - serviceEndpoint.id(), getScope()); - unPublishScopedResource(serviceEndpoint); + serviceEndpoint.id(), getScopeFromToken()); + unPublishResource(serviceEndpoint); } catch(Exception e){ logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}", - serviceEndpoint.id(), getScope(), e); + serviceEndpoint.id(), getScopeFromToken(), e); } } }catch(Exception e){ @@ -275,7 +275,7 @@ public class SmartExecutorInitializator implements ApplicationManager { // Persistence to clean previous situation of a failure of HostingNode try { - publishScopedResource(serviceEndpoint); + publishResource(serviceEndpoint); } catch (RegistryNotFoundException e) { logger.error("Unable to Create ServiceEndpoint. the Service will be aborted", e); return; @@ -306,7 +306,7 @@ public class SmartExecutorInitializator implements ApplicationManager { "\n-------------------------------------------------------\n" + "Smart Executor is Stopping on scope {}\n" + "-------------------------------------------------------", - getScope()); + getScopeFromToken()); SmartExecutorScheduler.getInstance().stopAll(); @@ -317,14 +317,14 @@ public class SmartExecutorInitializator implements ApplicationManager { } catch (Exception e) { logger.error("Unable to correctly close {} for scope {}", SmartExecutorPersistenceConnector.class.getSimpleName(), - getScope(), e); + getScopeFromToken(), e); } logger.trace( "\n-------------------------------------------------------\n" + "Smart Executor Stopped Successfully on scope {}\n" + "-------------------------------------------------------", - getScope()); + getScopeFromToken()); } } diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java index d633b78..6fcc670 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java @@ -48,36 +48,36 @@ public class JSONLaunchParameter extends LaunchParameter { public JSONLaunchParameter(String pluginName, Map pluginCapabilities, Map inputs) { super(pluginName, pluginCapabilities, inputs); - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); } public JSONLaunchParameter(String pluginName, Map inputs, Scheduling scheduling) throws ParseException { super(pluginName, inputs, scheduling); - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); } public JSONLaunchParameter(String pluginName, Map pluginCapabilities, Map inputs, Scheduling scheduling) throws ParseException { super(pluginName, pluginCapabilities, inputs, scheduling); - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); } @Deprecated public JSONLaunchParameter(String pluginName, Map inputs, Scheduling scheduling, boolean persist) throws ParseException { super(pluginName, inputs, scheduling, persist); - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); } @Deprecated public JSONLaunchParameter(String pluginName, Map pluginCapabilities, Map inputs, Scheduling scheduling, boolean persist) throws ParseException { super(pluginName, pluginCapabilities, inputs, scheduling, persist); this.scheduling = new JSONScheduling(scheduling); - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); } public JSONLaunchParameter(LaunchParameter parameter) throws ParseException { super(parameter.getPluginName(), parameter.getPluginCapabilities(), parameter.getInputs(), parameter.getScheduling()); this.scheduling = new JSONScheduling(parameter.getScheduling()); - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); } public JSONLaunchParameter(JSONObject jsonObject) throws JSONException, ParseException, ScopeNotMatchException { @@ -113,7 +113,7 @@ public class JSONLaunchParameter extends LaunchParameter { this.usedBy = jsonObject.getString(USED_BY); } - this.scope = SmartExecutorInitializator.getScope(); + this.scope = SmartExecutorInitializator.getScopeFromToken(); if(jsonObject.has(SCOPE)){ String jsonScope = jsonObject.getString(SCOPE); if(jsonScope.compareTo(scope)!=0){ diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java index 1f386f2..aa6c6b9 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java @@ -126,7 +126,7 @@ public class SmartExecutorPersistenceConfiguration { List serviceEndpoints = client.submit(query); if(serviceEndpoints.size()>1){ query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE)); - query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", SmartExecutorInitializator.getScope())); + query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", SmartExecutorInitializator.getScopeFromToken())); serviceEndpoints = client.submit(query); } return serviceEndpoints.get(0); diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java index 426d430..3d79add 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java @@ -43,12 +43,12 @@ public abstract class SmartExecutorPersistenceFactory { */ public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception { SmartExecutorPersistenceConnector persistence = - getPersistenceConnector(SmartExecutorInitializator.getScope()); + getPersistenceConnector(SmartExecutorInitializator.getScopeFromToken()); if(persistence==null){ logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.", SmartExecutorPersistenceConnector.class.getSimpleName(), - SmartExecutorInitializator.getScope(), + SmartExecutorInitializator.getScopeFromToken(), Map.class.getSimpleName()); String className = CouchDBPersistenceConnector.class.getSimpleName(); @@ -56,7 +56,7 @@ public abstract class SmartExecutorPersistenceFactory { new SmartExecutorPersistenceConfiguration(className); persistence = new CouchDBPersistenceConnector(configuration); - persistenceConnectors.put(SmartExecutorInitializator.getScope(), + persistenceConnectors.put(SmartExecutorInitializator.getScopeFromToken(), persistence); } @@ -65,10 +65,10 @@ public abstract class SmartExecutorPersistenceFactory { public static synchronized void closePersistenceConnector() throws Exception { SmartExecutorPersistenceConnector persistence = - getPersistenceConnector(SmartExecutorInitializator.getScope()); + getPersistenceConnector(SmartExecutorInitializator.getScopeFromToken()); if(persistence!=null){ persistence.close(); - persistenceConnectors.remove(SmartExecutorInitializator.getScope()); + persistenceConnectors.remove(SmartExecutorInitializator.getScopeFromToken()); } } diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/CouchDBPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/CouchDBPersistenceConnector.java index 6dfa12d..b466572 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/CouchDBPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/CouchDBPersistenceConnector.java @@ -205,7 +205,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, PLUGIN_STATE_DOCUMENT)); - String scope = SmartExecutorInitializator.getScope(); + String scope = SmartExecutorInitializator.getScopeFromToken(); ArrayNode startKey = new ObjectMapper().createArrayNode(); startKey.add(scope); ArrayNode endKey = new ObjectMapper().createArrayNode(); @@ -273,7 +273,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, SCHEDULED_TASKS_DOCUMENT)); query = query.viewName(ORPHAN_VIEW); - String scope = SmartExecutorInitializator.getScope(); + String scope = SmartExecutorInitializator.getScopeFromToken(); ArrayNode startKey = new ObjectMapper().createArrayNode(); startKey.add(scope); ArrayNode endKey = new ObjectMapper().createArrayNode(); @@ -316,7 +316,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect JSONObject obj = jlp.toJSON(); obj.append(TYPE_JSON_FIELD, SCHEDULED_TASK_TYPE); obj.append(USED_BY_FIELD, consumerID); - obj.append(ScheduledTaskConfiguration.SCOPE, SmartExecutorInitializator.getScope()); + obj.append(ScheduledTaskConfiguration.SCOPE, SmartExecutorInitializator.getScopeFromToken()); createItem(obj, uuid.toString()); } catch (Exception e) { logger.error("Error Adding Scheduled Task UUID : {}, Consumer : {}, LaunchParameter : {}", diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/PluginStateEvolutionObjectNode.java b/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/PluginStateEvolutionObjectNode.java index ffabda2..77fb353 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/PluginStateEvolutionObjectNode.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/couchdb/PluginStateEvolutionObjectNode.java @@ -76,7 +76,7 @@ public class PluginStateEvolutionObjectNode { } public static void addScope(ObjectNode objectNode){ - objectNode.put(SCOPE_FIELD, SmartExecutorInitializator.getScope()); + objectNode.put(SCOPE_FIELD, SmartExecutorInitializator.getScopeFromToken()); } public static ObjectNode getObjectMapper(PluginStateEvolution pluginStateEvolution){ diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java index 9dfea23..aff9c7e 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java @@ -20,33 +20,6 @@ import org.junit.Test; */ public class SmartExecutorImplTest { - /* - @Test - public void createServiceEndpointTest() { - TODO Redesign this test - ServiceEndpoint serviceEndpoint = SmartExecutorInitalizator.createServiceEndpoint(); - - Profile profile = serviceEndpoint.profile(); - Assert.assertEquals(SmartExecutor.WEB_SERVICE_SERVICE_NAME, profile.category()); - Assert.assertEquals(SmartExecutor.class.getSimpleName(), profile.name()); - Group accessPoints = profile.accessPoints(); - Assert.assertEquals(1, accessPoints.size()); - HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration(); - Map supportedCapabilities = hwpd.getSupportedCapabilities(); - for(AccessPoint accessPoint : accessPoints){ - Assert.assertEquals(hwpd.getName(),accessPoint.name()); - Group properties = accessPoint.properties(); - Assert.assertEquals(supportedCapabilities.size(), properties.size()); - for(Property property : properties){ - String propertyName = property.name(); - Assert.assertTrue(supportedCapabilities.containsKey(propertyName)); - Assert.assertEquals(supportedCapabilities.get(propertyName), property.value()); - } - } - - } - */ - @Test public void helloWorldTest() throws Exception{ Map inputs = new HashMap(); diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index de1539f..ccfe5c1 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -16,6 +16,8 @@ import org.gcube.vremanagement.executor.configuration.ScheduledTaskConfiguration import org.gcube.vremanagement.executor.persistence.couchdb.CouchDBPersistenceConnector; import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; +import org.gcube.vremanagement.executor.pluginmanager.TestUtility; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -31,10 +33,14 @@ public class SmartExecutorPersistenceConnectorTest { private static Logger logger = LoggerFactory.getLogger(SmartExecutorPersistenceConnectorTest.class); @Before - public void before() throws Exception{ - SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071"); + public void bfore(){ + SecurityTokenProvider.instance.set(TestUtility.TOKEN); } + @After + public void after(){ + SecurityTokenProvider.instance.reset(); + } @Test public void getConnectionTest() throws Exception { @@ -78,7 +84,6 @@ public class SmartExecutorPersistenceConnectorTest { List lc = stc.getAvailableScheduledTasks(); logger.debug("Available Scheduled Tasks : {}", lc); - } diff --git a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePluginTest.java b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePluginTest.java index 63b2af2..52aa923 100644 --- a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePluginTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePluginTest.java @@ -16,9 +16,9 @@ import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConn import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginStateNotification; -import org.junit.AfterClass; +import org.junit.After; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,22 +28,24 @@ import org.slf4j.LoggerFactory; * */ public class RunnablePluginTest { + private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class); - @BeforeClass - public static void prepare() throws Exception{ - try { - SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071"); - SmartExecutorPersistenceFactory.getPersistenceConnector(); - } catch (Exception e) { - logger.error("Unable to Create JDBCPersistenceConnector"); - throw e; - } + @Before + public void before() throws Exception { + SecurityTokenProvider.instance.set(TestUtility.TOKEN); + SmartExecutorPersistenceFactory.getPersistenceConnector(); + } + + @After + public void after() throws Exception { + SecurityTokenProvider.instance.reset(); + SmartExecutorPersistenceFactory.getPersistenceConnector().close(); } @Test public void launchNullInputsTest() throws Exception { - System.out.println("Testing Null inputs"); + logger.debug("Testing Null inputs"); UUID uuid = UUID.randomUUID(); HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration(); SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); @@ -60,7 +62,7 @@ public class RunnablePluginTest { @Test public void launchEmptyInputsTest() throws Exception { - System.out.println("Testing Empty inputs"); + logger.debug("Testing Empty inputs"); Map inputs = new HashMap(); UUID uuid = UUID.randomUUID(); HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration(); @@ -80,7 +82,7 @@ public class RunnablePluginTest { @Test public void launchValidInputsTest() throws Exception { - System.out.println("Testing Some inputs"); + logger.debug("Testing Some inputs"); Map inputs = new HashMap(); inputs.put("Test", "Test"); long sleepTime = 10000; @@ -111,9 +113,4 @@ public class RunnablePluginTest { } - @AfterClass - public static void finish() throws Exception{ - SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); - persistenceConnector.close(); - } } diff --git a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java index 4e13581..6fd39e6 100644 --- a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java @@ -39,29 +39,16 @@ public class SmartExecutorSchedulerTest { public static SmartExecutorPersistenceConnector pc; - @BeforeClass - public static void prepare() throws Exception{ - try { - SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071"); - /* - pc = new JDBCPersistenceConnector("."); - SmartExecutorPersistenceConnector.setPersistenceConnector(pc); - */ - pc = SmartExecutorPersistenceFactory.getPersistenceConnector(); - } catch (Exception e) { - logger.error("Unable to Create SmartExecutorPersistenceConnector"); - throw e; - } + @BeforeClass + public static void before() throws Exception { + SecurityTokenProvider.instance.set(TestUtility.TOKEN); + pc = SmartExecutorPersistenceFactory.getPersistenceConnector(); } @AfterClass - public static void close() throws Exception{ - try { - SmartExecutorPersistenceFactory.closePersistenceConnector(); - } catch (Exception e) { - logger.error("Unable to Close JDBCPersistenceConnector"); - throw e; - } + public static void after() throws Exception { + pc.close(); + SecurityTokenProvider.instance.reset(); } public UUID scheduleTest(Scheduling scheduling, Long sleepTime) throws Exception { diff --git a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/TestUtility.java b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/TestUtility.java new file mode 100644 index 0000000..95a17bb --- /dev/null +++ b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/TestUtility.java @@ -0,0 +1,14 @@ +/** + * + */ +package org.gcube.vremanagement.executor.pluginmanager; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class TestUtility { + + public static final String TOKEN = ""; + +}