diff --git a/pom.xml b/pom.xml index 4b2b2b8..52eafd5 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,17 @@ provided + + + org.gcube.resources + registry-publisher + test + + + org.gcube.core + common-smartgears + test + junit junit diff --git a/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java b/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java index c0d9d95..70eabc0 100644 --- a/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java +++ b/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java @@ -116,19 +116,23 @@ public class PersistenceConfiguration { } private static String decrypt(String encrypted) throws Exception { + /* Key key = null; return StringEncrypter.getEncrypter().decrypt(encrypted, key); + */ + return encrypted; } - private static PersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{ PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration(); Group accessPoints = serviceEndpoint.profile().accessPoints(); for(AccessPoint accessPoint : accessPoints){ persistenceConfiguration.uri = new URI(accessPoint.address()); persistenceConfiguration.username = accessPoint.name(); + String encryptedPassword = accessPoint.password(); String password = decrypt(encryptedPassword); + persistenceConfiguration.password = password; persistenceConfiguration.propertyMap = accessPoint.propertyMap(); } diff --git a/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java new file mode 100644 index 0000000..db18fad --- /dev/null +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java @@ -0,0 +1,162 @@ +/** + * + */ +package org.gcube.accounting.persistence; + +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +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.smartgears.configuration.Mode; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class PersistenceConfigurationTest { + + private static final Logger logger = LoggerFactory.getLogger(PersistenceConfigurationTest.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 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 = "CouchDBPersistence"; + + public static final String FAKE_USERNAME = "fakeusername"; + public static final String FAKE_PASSWORD = "fakepassword"; + + public static final String TEST_SCOPE = "/gcube/devsec"; + + public static final String DB_NAME_PROPERTY_NAME = "dbName"; + public static final String DB_NAME_PROPERTY_VALUE = "accounting"; + + + + /** + * 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 RegistryNotFoundException, 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 (RegistryNotFoundException 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(); + profile.category(PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY); + profile.name(PersistenceConfiguration.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(Mode.online.toString()); + + AccessPoint accessPointElement = new AccessPoint(); + accessPointElement.description(DESCRIPTION); + + accessPointElement.credentials(FAKE_USERNAME, FAKE_PASSWORD); + + Group properties = accessPointElement.properties(); + + Property className = new Property(); + className.nameAndValue(PersistenceConfiguration.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; + } + + @Test + public void testPersistenceFromIS() throws RegistryNotFoundException, Exception{ + List scopes = new ArrayList(); + scopes.add(TEST_SCOPE); + ServiceEndpoint serviceEndpoint = createServiceEndpoint(); + publishScopedResource(serviceEndpoint, scopes); + + PersistenceConfiguration.getPersistenceConfiguration(TEST_SCOPE, COUCHDB_CLASS_NAME); + + unPublishScopedResource(serviceEndpoint); + } +}