refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Fixing tests

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@117034 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-07-06 15:01:03 +00:00
parent fbf99786bb
commit 311e106118
5 changed files with 66 additions and 57 deletions

View File

@ -14,7 +14,6 @@ import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property; import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.Group; import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory; import org.gcube.resources.discovery.icclient.ICFactory;
@ -101,8 +100,7 @@ public class PersistenceConfiguration {
return value; return value;
} }
private static ServiceEndpoint getServiceEndpoint(String scope, String persistenceClassName){ private static ServiceEndpoint getServiceEndpoint(String persistenceClassName){
ScopeProvider.instance.set(scope);
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); 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/Category/text() eq '%s'", SERVICE_ENDPOINT_CATEGORY));
query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", SERVICE_ENDPOINT_NAME)); query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", SERVICE_ENDPOINT_NAME));
@ -142,8 +140,8 @@ public class PersistenceConfiguration {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static PersistenceConfiguration getPersistenceConfiguration(String scope, String persistenceClassName) throws Exception { public static PersistenceConfiguration getPersistenceConfiguration(String persistenceClassName) throws Exception {
ServiceEndpoint serviceEndpoint = getServiceEndpoint(scope, persistenceClassName); ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName);
return createPersistenceConfiguration(serviceEndpoint); return createPersistenceConfiguration(serviceEndpoint);
} }

View File

@ -4,6 +4,8 @@
package org.gcube.accounting.persistence; package org.gcube.accounting.persistence;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import org.gcube.accounting.aggregation.scheduler.AggregationScheduler; import org.gcube.accounting.aggregation.scheduler.AggregationScheduler;
@ -27,6 +29,12 @@ public abstract class PersistenceFactory {
private static String fallbackLocation; private static String fallbackLocation;
private static Map<String, Persistence> persistences;
static {
persistences = new HashMap<String, Persistence>();
}
private static File file(File file) throws IllegalArgumentException { private static File file(File file) throws IllegalArgumentException {
if(!file.isDirectory()){ if(!file.isDirectory()){
@ -51,57 +59,61 @@ public abstract class PersistenceFactory {
} }
} }
public synchronized static Persistence getPersistence(){ public static Persistence getPersistence(){
Persistence persistence = null;
String scope = ScopeProvider.instance.get(); String scope = ScopeProvider.instance.get();
String name = "";
if(scope!=null){
ScopeBean bean = new ScopeBean(scope);
if(bean.is(Type.VRE)){
bean = bean.enclosingScope();
}
name = bean.name();
}
String separator = name.compareTo("")==0 ? "" : ".";
File fallbackFile = new File(fallbackLocation, String.format("%s%s%s", name, separator ,ACCOUTING_FALLBACK_FILENAME)); Persistence persistence = persistences.get(scope);
FallbackPersistence fallbackPersistence = new FallbackPersistence(fallbackFile); if(persistence==null){
try {
ServiceLoader<Persistence> serviceLoader = ServiceLoader.load(Persistence.class); String name = "";
for (Persistence foundPersistence : serviceLoader) { if(scope!=null){
if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){ ScopeBean bean = new ScopeBean(scope);
continue; if(bean.is(Type.VRE)){
} bean = bean.enclosingScope();
try {
String foundPersistenceClassName = foundPersistence.getClass().getSimpleName();
logger.debug("Testing {}", foundPersistenceClassName);
PersistenceConfiguration configuration = PersistenceConfiguration.getPersistenceConfiguration(scope, foundPersistenceClassName);
foundPersistence.prepareConnection(configuration);
/*
* Uncomment the following line of code if you want to try
* to create a test UsageRecord before setting the
* foundPersistence as default
*
* foundPersistence.accountWithFallback(TestUsageRecord.createTestServiceUsageRecord());
*/
persistence = foundPersistence;
break;
} catch (Exception e) {
logger.debug(String.format("%s not initialized correctly. It will not be used", foundPersistence.getClass().getSimpleName()));
} }
name = bean.name();
} }
if(persistence==null){ String separator = name.compareTo("")==0 ? "" : ".";
File fallbackFile = new File(fallbackLocation, String.format("%s%s%s", name, separator ,ACCOUTING_FALLBACK_FILENAME));
FallbackPersistence fallbackPersistence = new FallbackPersistence(fallbackFile);
try {
ServiceLoader<Persistence> serviceLoader = ServiceLoader.load(Persistence.class);
for (Persistence foundPersistence : serviceLoader) {
if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){
continue;
}
try {
String foundPersistenceClassName = foundPersistence.getClass().getSimpleName();
logger.debug("Testing {}", foundPersistenceClassName);
PersistenceConfiguration configuration = PersistenceConfiguration.getPersistenceConfiguration(foundPersistenceClassName);
foundPersistence.prepareConnection(configuration);
/*
* Uncomment the following line of code if you want to try
* to create a test UsageRecord before setting the
* foundPersistence as default
*
* foundPersistence.accountWithFallback(TestUsageRecord.createTestServiceUsageRecord());
*/
persistence = foundPersistence;
break;
} catch (Exception e) {
logger.debug(String.format("%s not initialized correctly. It will not be used", foundPersistence.getClass().getSimpleName()));
}
}
if(persistence==null){
persistence = fallbackPersistence;
}
} catch(Exception e){
logger.error("Unable to instance a Persistence Implementation. Using fallback as default",
e.getCause());
persistence = fallbackPersistence; persistence = fallbackPersistence;
} }
} catch(Exception e){ persistence.setAggregationScheduler(AggregationScheduler.getInstance());
logger.error("Unable to instance a Persistence Implementation. Using fallback as default", persistence.setFallback(fallbackPersistence);
e.getCause());
persistence = fallbackPersistence;
} }
persistence.setAggregationScheduler(AggregationScheduler.getInstance());
persistence.setFallback(fallbackPersistence);
return persistence; return persistence;
} }

View File

@ -54,9 +54,9 @@ public class PersistenceConfigurationTest {
public static final String FAKE_USERNAME = "fakeusername"; public static final String FAKE_USERNAME = "fakeusername";
public static final String FAKE_PASSWORD = "fakepassword"; public static final String FAKE_PASSWORD = "fakepassword";
public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devsec"}; public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devNext"};
public static final String GCUBE_SCOPE = SCOPES[0]; public static final String GCUBE_SCOPE = SCOPES[0];
public static final String GCUBE_DEVSEC_SCOPE = SCOPES[1]; 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_NAME = "dbName";
public static final String DB_NAME_PROPERTY_VALUE = "accounting"; public static final String DB_NAME_PROPERTY_VALUE = "accounting";
@ -163,7 +163,7 @@ public class PersistenceConfigurationTest {
} }
protected void clean(){ protected void clean(){
ScopeProvider.instance.set(GCUBE_DEVSEC_SCOPE); ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE);
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY)) .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY))
@ -177,17 +177,18 @@ public class PersistenceConfigurationTest {
for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
try { try {
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), GCUBE_DEVSEC_SCOPE); serviceEndpoint.id(), GCUBE_DEVNEXT_SCOPE);
unPublishScopedResource(serviceEndpoint); unPublishScopedResource(serviceEndpoint);
} catch(Exception e){ } catch(Exception e){
logger.debug("Exception trying to unpublish the old ServiceEndpoint with ID {} from scope {}", logger.debug("Exception trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), GCUBE_DEVSEC_SCOPE, e); serviceEndpoint.id(), GCUBE_DEVNEXT_SCOPE, e);
} }
} }
} }
@Test @Test
public void testPersistenceConfigurationFromIS() throws Exception{ public void testPersistenceConfigurationFromIS() throws Exception{
ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE);
boolean createResource = false; boolean createResource = false;
ServiceEndpoint serviceEndpoint = null; ServiceEndpoint serviceEndpoint = null;
if(createResource){ if(createResource){
@ -197,7 +198,7 @@ public class PersistenceConfigurationTest {
} }
try { try {
PersistenceConfiguration persitenceConfiguration = PersistenceConfiguration.getPersistenceConfiguration(GCUBE_DEVSEC_SCOPE, COUCHDB_CLASS_NAME); PersistenceConfiguration persitenceConfiguration = PersistenceConfiguration.getPersistenceConfiguration(COUCHDB_CLASS_NAME);
if(createResource){ if(createResource){
Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON))); Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON)));
Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0); Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0);

View File

@ -1 +0,0 @@
6 4Z<34>/U<><55> C<><43>ߘ

View File

@ -1 +0,0 @@
6 4Z<34>/U<><55> C<><43>ߘ