From b12e0f11a34937e5dc777e739403e1bd476723a1 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 1 Oct 2015 14:16:53 +0000 Subject: [PATCH] Added test git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@119318 82a268e6-3cf1-43bd-a215-b396298e98cf --- ...ccountingPersistenceConfigurationTest.java | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java diff --git a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java new file mode 100644 index 0000000..868afa6 --- /dev/null +++ b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java @@ -0,0 +1,247 @@ +/** + * + */ +package org.gcube.accounting.persistence; + +import java.io.StringWriter; +import java.net.URL; +import java.util.Arrays; +import java.util.List; + +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Profile; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +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.common.scope.api.ScopeProvider; +import org.gcube.informationsystem.publisher.AdvancedScopedPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.informationsystem.publisher.ScopedPublisher; +import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class AccountingPersistenceConfigurationTest { + + private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceConfigurationTest.class); + + public static final String PROFILE_DESCRIPTION = "This ServiceEndpoint contains the parameter to connect to DB to persist log accounting."; + public static final String RUNNING_ON = "http://localhost:5984"; + + public static final String LOAD_BALANCER = "loadBalancer"; + + public static final String READY = "READY"; + + public static final String TEST_VERSION = "1.0.0"; + public static final short[] VERSION_SLICES = new short[]{1,6,0,0}; + + public static final String DESCRIPTION = "CouchDB Server"; + public static final String COUCHDB_CLASS_NAME = "AccountingPersistenceCouchDB"; + + public static final String FAKE_USERNAME = "fakeusername"; + public static final String FAKE_PASSWORD = "fakepassword"; + + public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devNext"}; + public static final String GCUBE_SCOPE = SCOPES[0]; + public static final String GCUBE_DEVNEXT_SCOPE = SCOPES[1]; + + public static final String DB_NAME_PROPERTY_NAME = "dbName"; + public static final String DB_NAME_PROPERTY_VALUE = "accounting"; + + + public static final String[] ALL_SCOPES = new String[]{ + "/gcube", + "/gcube/devsec", + "/gcube/devsec/devVRE", + "/gcube/devsec/VALEN-TINA", + "/gcube/devsec/USTORE_VRE", + "/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); + + 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; + } + } + + /** + * 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) throws RegistryNotFoundException, Exception { + //StringWriter stringWriter = new StringWriter(); + //Resources.marshal(resource, stringWriter); + + ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); + AdvancedScopedPublisher advancedScopedPublisher = new AdvancedScopedPublisher(scopedPublisher); + + String id = resource.id(); + logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, ScopeProvider.instance.get()); + + //scopedPublisher.remove(resource, scopes); + advancedScopedPublisher.forceRemove(resource); + + 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 + * @return the created {@link ServiceEndpoint} + */ + protected static ServiceEndpoint createServiceEndpoint(){ + logger.debug("Getting Available Plugins and their own supported capabilities"); + + logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities"); + ServiceEndpoint serviceEndpoint = new ServiceEndpoint(); + Profile profile = serviceEndpoint.newProfile(); + AccountingPersistenceConfiguration accountingPersistenceConfiguration = new AccountingPersistenceConfiguration(); + profile.category(accountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY); + profile.name(accountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME); + profile.version(TEST_VERSION); + profile.description(PROFILE_DESCRIPTION); + + + Platform platform = profile.newPlatform(); + platform.name(RUNNING_ON); + + platform.version(VERSION_SLICES[0]); + platform.minorVersion(VERSION_SLICES[1]); + platform.buildVersion(VERSION_SLICES[2]); + platform.revisionVersion(VERSION_SLICES[3]); + + Runtime runtime = profile.newRuntime(); + runtime.hostedOn(RUNNING_ON); + runtime.status(READY); + + Group accessPoints = profile.accessPoints(); + + AccessPoint accessPointElement = new AccessPoint(); + accessPoints.add(accessPointElement); + accessPointElement.description(DESCRIPTION); + accessPointElement.credentials(FAKE_USERNAME, FAKE_PASSWORD); + + accessPointElement.address(RUNNING_ON); + accessPointElement.name(LOAD_BALANCER); + + Group properties = accessPointElement.properties(); + + Property className = new Property(); + className.nameAndValue(AccountingPersistenceConfiguration.PERSISTENCE_CLASS_NAME, COUCHDB_CLASS_NAME); + properties.add(className); + + Property dbName = new Property(); + dbName.nameAndValue(DB_NAME_PROPERTY_NAME, DB_NAME_PROPERTY_VALUE); + properties.add(dbName); + + StringWriter stringWriter = new StringWriter(); + Resources.marshal(serviceEndpoint, stringWriter); + logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString()); + + return serviceEndpoint; + } + + protected void clean(){ + ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE); + + AccountingPersistenceConfiguration accountingPersistenceConfiguration = new AccountingPersistenceConfiguration(); + + SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) + .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", accountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY)) + .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", accountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME)) + .addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", RUNNING_ON)) + .setResult("$resource"); + + DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); + List serviceEndpoints = client.submit(query); + + for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { + try { + logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", + serviceEndpoint.id(), GCUBE_DEVNEXT_SCOPE); + unPublishScopedResource(serviceEndpoint); + } catch(Exception e){ + logger.debug("Exception trying to unpublish the old ServiceEndpoint with ID {} from scope {}", + serviceEndpoint.id(), GCUBE_DEVNEXT_SCOPE, e); + } + } + } + + @Test + public void testPersistenceConfigurationFromIS() throws Exception{ + ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE); + boolean createResource = false; + ServiceEndpoint serviceEndpoint = null; + if(createResource){ + List scopes = Arrays.asList(SCOPES); + serviceEndpoint = createServiceEndpoint(); + publishScopedResource(serviceEndpoint, scopes); + } + + try { + AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(COUCHDB_CLASS_NAME); + if(createResource){ + Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON))); + Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0); + Assert.assertTrue(persitenceConfiguration.getPassword().compareTo(FAKE_PASSWORD)==0); + Assert.assertTrue(persitenceConfiguration.getProperty(DB_NAME_PROPERTY_NAME).compareTo(DB_NAME_PROPERTY_VALUE)==0); + } + } finally { + if(createResource){ + unPublishScopedResource(serviceEndpoint); + } + } + + } + + @Test + public void testPersistenceConfigurationFromISInDifferentScopes() throws Exception{ + for(String scope : ALL_SCOPES){ + logger.debug("START ======================================================"); + ScopeProvider.instance.set(scope); + try { + AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(COUCHDB_CLASS_NAME); + logger.debug("{} : {}", AccountingPersistenceConfiguration.class.getSimpleName(), persitenceConfiguration); + }catch(IndexOutOfBoundsException e){ + logger.debug("No AccountingPersistenceConfiguration : \n {} {} \n\n", e.getClass().getName(), e.getMessage()); + } catch(Exception e){ + logger.error("Error getting AccountingPersistenceConfiguration", e); + throw e; + } + logger.debug(" END ======================================================"); + } + } + +}