/** * */ 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.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.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.Assert; 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); private static final String PROFILE_DESCRIPTION = "This ServiceEndpoint contains the parameter to connect to DB to persist log accounting"; private static final String HOSTED_ON = "pc-frosini.isti.cnr.it"; private static final String ENDPOINT = "http://localhost:5984"; private static final String READY = "READY"; private static final String PLATFORM_NAME = "Platform Name"; private static final String TEST_VERSION = "1.0.0"; private static final short[] VERSION_SLICES = new short[]{1,6,0,0}; private static final String DESCRIPTION = "Persistence Configuration Test"; private static final String FAKE_USERNAME = "fakeusername"; private static final String FAKE_PASSWORD = "fakepassword"; private static final String DB_NAME_PROPERTY_KEY = "dbName"; private static final String DB_NAME_PROPERTY_VALUE = "accounting"; private abstract class AccountingPersistenceFakeDB extends PersistenceBackend { } 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); 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, 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 * @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(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY); profile.name(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME); profile.version(TEST_VERSION); profile.description(PROFILE_DESCRIPTION); Platform platform = profile.newPlatform(); platform.name(PLATFORM_NAME); 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(HOSTED_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(ENDPOINT); accessPointElement.name(AccountingPersistenceFakeDB.class.getSimpleName()); Group properties = accessPointElement.properties(); Property dbName = new Property(); dbName.nameAndValue(DB_NAME_PROPERTY_KEY, DB_NAME_PROPERTY_VALUE); dbName.encrypted(false); properties.add(dbName); StringWriter stringWriter = new StringWriter(); Resources.marshal(serviceEndpoint, stringWriter); logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString()); return serviceEndpoint; } public void testPersistenceConfigurationFromIS() throws Exception{ boolean createResource = true; ServiceEndpoint serviceEndpoint = null; List scopes = Arrays.asList(ALL_SCOPES); if(createResource){ serviceEndpoint = createServiceEndpoint(); publishScopedResource(serviceEndpoint, scopes); } Thread.sleep(5000); // Waiting 5 sec try { AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(AccountingPersistenceFakeDB.class); if(createResource){ String uri = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.URL_PROPERTY_KEY); Assert.assertTrue(uri.compareTo(new URL(ENDPOINT).toString())==0); String username = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY); Assert.assertTrue(username.compareTo(FAKE_USERNAME)==0); String password = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY); Assert.assertTrue(password.compareTo(FAKE_PASSWORD)==0); String dbName = persitenceConfiguration.getProperty(DB_NAME_PROPERTY_KEY); Assert.assertTrue(dbName.compareTo(DB_NAME_PROPERTY_VALUE)==0); } } finally { if(createResource){ unPublishScopedResource(serviceEndpoint, scopes); } } } public void getUsernamePasswordForScopes() throws Exception{ logger.debug("START ======================================================"); try { AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(AccountingPersistenceFakeDB.class); String uri = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.URL_PROPERTY_KEY); String username = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY); String password = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY); logger.debug("{} - {} - {} - {}", BasicUsageRecord.getScopeFromToken(), uri, username, password); }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; } finally { logger.debug(" END ======================================================"); } } }