From 51cc4bff5a29e6d50b7060e549aaead351364615 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 8 Jul 2015 14:41:05 +0000 Subject: [PATCH] Improving code git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@117094 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../AccountingPersistenceConfiguration.java | 44 +++++++++---------- .../AccountingPersistenceFactory.java | 2 +- .../PersistenceConfigurationTest.java | 13 +++--- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java index b6996b2..a61ec6d 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java @@ -23,8 +23,8 @@ import org.gcube.resources.discovery.icclient.ICFactory; */ public class AccountingPersistenceConfiguration { - protected final static String SERVICE_ENDPOINT_CATEGORY = "Accounting"; - protected final static String SERVICE_ENDPOINT_NAME = "Persistence"; + protected final String SERVICE_ENDPOINT_CATEGORY = "Accounting"; + protected final String SERVICE_ENDPOINT_NAME = "Persistence"; protected static final String PERSISTENCE_CLASS_NAME = "persistenceClassName"; @@ -34,15 +34,25 @@ public class AccountingPersistenceConfiguration { protected Map propertyMap; + protected void init(){ + this.propertyMap = new HashMap(); + } + public AccountingPersistenceConfiguration(){ - propertyMap = new HashMap(); + init(); } public AccountingPersistenceConfiguration(URI uri, String username, String password){ + init(); this.uri = uri; this.username = username; this.password = password; - this.propertyMap = new HashMap(); + } + + public AccountingPersistenceConfiguration(String persistenceClassName) throws Exception { + init(); + ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName); + setValues(serviceEndpoint); } /** @@ -100,7 +110,7 @@ public class AccountingPersistenceConfiguration { return value; } - private static ServiceEndpoint getServiceEndpoint(String persistenceClassName){ + protected ServiceEndpoint getServiceEndpoint(String persistenceClassName){ SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", SERVICE_ENDPOINT_CATEGORY)); query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", SERVICE_ENDPOINT_NAME)); @@ -117,32 +127,18 @@ public class AccountingPersistenceConfiguration { return StringEncrypter.getEncrypter().decrypt(encrypted); } - protected static AccountingPersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{ - AccountingPersistenceConfiguration persistenceConfiguration = new AccountingPersistenceConfiguration(); + protected void setValues(ServiceEndpoint serviceEndpoint) throws Exception{ Group accessPoints = serviceEndpoint.profile().accessPoints(); for(AccessPoint accessPoint : accessPoints){ - persistenceConfiguration.uri = new URI(accessPoint.address()); - persistenceConfiguration.username = accessPoint.username(); + this.uri = new URI(accessPoint.address()); + this.username = accessPoint.username(); String encryptedPassword = accessPoint.password(); String password = decrypt(encryptedPassword); - persistenceConfiguration.password = password; - persistenceConfiguration.propertyMap = accessPoint.propertyMap(); + this.password = password; + this.propertyMap = accessPoint.propertyMap(); } - return persistenceConfiguration; - } - - /** - * - * @param scope - * @param persistenceClassName - * @return - * @throws Exception - */ - public static AccountingPersistenceConfiguration getPersistenceConfiguration(String persistenceClassName) throws Exception { - ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName); - return createPersistenceConfiguration(serviceEndpoint); } } diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java index cf83031..9e02098 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java @@ -87,7 +87,7 @@ public abstract class AccountingPersistenceFactory { try { String foundPersistenceClassName = foundPersistence.getClass().getSimpleName(); logger.debug("Testing {}", foundPersistenceClassName); - AccountingPersistenceConfiguration configuration = AccountingPersistenceConfiguration.getPersistenceConfiguration(foundPersistenceClassName); + AccountingPersistenceConfiguration configuration = new AccountingPersistenceConfiguration(foundPersistenceClassName); foundPersistence.prepareConnection(configuration); /* * Uncomment the following line of code if you want to try diff --git a/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java index a1cafd6..5b5c4a2 100644 --- a/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java @@ -117,8 +117,9 @@ public class PersistenceConfigurationTest { 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); + AccountingPersistenceConfiguration accountingPersistenceConfiguration = new AccountingPersistenceConfiguration(); + profile.category(accountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY); + profile.name(accountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME); profile.version(TEST_VERSION); profile.description(PROFILE_DESCRIPTION); @@ -165,9 +166,11 @@ public class PersistenceConfigurationTest { 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/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"); @@ -198,7 +201,7 @@ public class PersistenceConfigurationTest { } try { - AccountingPersistenceConfiguration persitenceConfiguration = AccountingPersistenceConfiguration.getPersistenceConfiguration(COUCHDB_CLASS_NAME); + 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);