diff --git a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java index 0b61e81..38ca09c 100644 --- a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java +++ b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java @@ -5,11 +5,10 @@ package org.gcube.accounting.persistence; import java.io.StringWriter; import java.net.URL; -import java.util.Arrays; -import java.util.List; import org.gcube.accounting.datamodel.BasicUsageRecord; -import org.gcube.common.resources.gcore.Resource; +import org.gcube.accounting.testutility.TestUtility; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; @@ -19,10 +18,9 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime; import org.gcube.common.resources.gcore.common.Platform; import org.gcube.common.resources.gcore.utils.Group; import org.gcube.documentstore.persistence.PersistenceBackend; -import org.gcube.informationsystem.publisher.RegistryPublisherFactory; -import org.gcube.informationsystem.publisher.ScopedPublisher; -import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,49 +54,16 @@ public class AccountingPersistenceConfigurationTest { } - public static final String[] ALL_SCOPES = new String[]{ - "/gcube", - "/gcube/devsec", - "/gcube/devsec/devVRE", - "/gcube/devNext", - "/gcube/devNext/NextNext" - }; - - /** - * Publish the provided resource on all Service Scopes retrieved from - * Context - * @param resource to be published - * @throws RegistryNotFoundException if the Registry is not found so the - * resource has not be published - */ - private static void publishScopedResource(Resource resource, List scopes) throws Exception { - StringWriter stringWriter = new StringWriter(); - Resources.marshal(resource, stringWriter); + @Before + public void before() throws Exception{ + SecurityTokenProvider.instance.set(TestUtility.TOKEN); + } - ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); - try { - logger.debug("Trying to publish to {}:\n{}", scopes, stringWriter); - scopedPublisher.create(resource, scopes); - } catch (Exception e) { - logger.error("The resource was not published", e); - throw e; - } + @After + public void after(){ + SecurityTokenProvider.instance.reset(); } - - /** - * Remove the resource from IS - * @param resource to be unpublished - * @throws RegistryNotFoundException if the Registry is not found so the - * resource has not be published - */ - private static void unPublishScopedResource(Resource resource, List scopes) throws RegistryNotFoundException, Exception { - ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); - String id = resource.id(); - logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, BasicUsageRecord.getScopeFromToken()); - scopedPublisher.remove(resource, scopes); - logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id); - } - + /** * Create the Service Endpoint using information related to discovered * available plugins and their own discovered capabilities @@ -155,11 +120,10 @@ public class AccountingPersistenceConfigurationTest { public void testPersistenceConfigurationFromIS() throws Exception{ boolean createResource = true; ServiceEndpoint serviceEndpoint = null; - List scopes = Arrays.asList(ALL_SCOPES); if(createResource){ serviceEndpoint = createServiceEndpoint(); - publishScopedResource(serviceEndpoint, scopes); + TestUtility.publishResource(serviceEndpoint); } Thread.sleep(5000); // Waiting 5 sec @@ -179,7 +143,7 @@ public class AccountingPersistenceConfigurationTest { } } finally { if(createResource){ - unPublishScopedResource(serviceEndpoint, scopes); + TestUtility.unPublishResource(serviceEndpoint); } } diff --git a/src/test/java/org/gcube/accounting/testutility/TestUtility.java b/src/test/java/org/gcube/accounting/testutility/TestUtility.java index b4040f9..f14b8e3 100644 --- a/src/test/java/org/gcube/accounting/testutility/TestUtility.java +++ b/src/test/java/org/gcube/accounting/testutility/TestUtility.java @@ -3,6 +3,19 @@ */ package org.gcube.accounting.testutility; +import java.io.StringWriter; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; +import org.gcube.informationsystem.publisher.RegistryPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @@ -11,4 +24,62 @@ public class TestUtility { public static final String TOKEN = ""; -} + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(TestUtility.class); + + public static String getScopeFromToken(){ + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry; + try { + authorizationEntry = Constants.authorizationService().get(token); + } catch (Exception e) { + throw new RuntimeException(e); + } + String scope = authorizationEntry.getContext(); + return scope; + } + + /** + * Publish the provided resource on current scope + * @param resource to be published + * @throws RegistryNotFoundException if the Registry is not found so the + * resource has not be published + */ + public 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{}", getScopeFromToken(), stringWriter); + registryPublisher.create(resource); + } catch (Exception e) { + logger.error("The resource was not published", e); + throw e; + } + } + + /** + * Remove the resource from IS from curretn scope + * @param resource to be unpublished + * @throws RegistryNotFoundException if the Registry is not found so the + * resource has not be published + */ + public 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, getScopeFromToken()); + + registryPublisher.remove(resource); + + logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id); + } + +} \ No newline at end of file