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:
parent
fbf99786bb
commit
311e106118
|
@ -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.Property;
|
||||
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.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
|
@ -101,8 +100,7 @@ public class PersistenceConfiguration {
|
|||
return value;
|
||||
}
|
||||
|
||||
private static ServiceEndpoint getServiceEndpoint(String scope, String persistenceClassName){
|
||||
ScopeProvider.instance.set(scope);
|
||||
private static 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));
|
||||
|
@ -142,8 +140,8 @@ public class PersistenceConfiguration {
|
|||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static PersistenceConfiguration getPersistenceConfiguration(String scope, String persistenceClassName) throws Exception {
|
||||
ServiceEndpoint serviceEndpoint = getServiceEndpoint(scope, persistenceClassName);
|
||||
public static PersistenceConfiguration getPersistenceConfiguration(String persistenceClassName) throws Exception {
|
||||
ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName);
|
||||
return createPersistenceConfiguration(serviceEndpoint);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
package org.gcube.accounting.persistence;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.gcube.accounting.aggregation.scheduler.AggregationScheduler;
|
||||
|
@ -27,6 +29,12 @@ public abstract class PersistenceFactory {
|
|||
|
||||
private static String fallbackLocation;
|
||||
|
||||
private static Map<String, Persistence> persistences;
|
||||
|
||||
static {
|
||||
persistences = new HashMap<String, Persistence>();
|
||||
}
|
||||
|
||||
private static File file(File file) throws IllegalArgumentException {
|
||||
|
||||
if(!file.isDirectory()){
|
||||
|
@ -51,57 +59,61 @@ public abstract class PersistenceFactory {
|
|||
}
|
||||
}
|
||||
|
||||
public synchronized static Persistence getPersistence(){
|
||||
Persistence persistence = null;
|
||||
|
||||
public static Persistence getPersistence(){
|
||||
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));
|
||||
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(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()));
|
||||
|
||||
Persistence persistence = persistences.get(scope);
|
||||
if(persistence==null){
|
||||
|
||||
String name = "";
|
||||
if(scope!=null){
|
||||
ScopeBean bean = new ScopeBean(scope);
|
||||
if(bean.is(Type.VRE)){
|
||||
bean = bean.enclosingScope();
|
||||
}
|
||||
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;
|
||||
}
|
||||
} catch(Exception e){
|
||||
logger.error("Unable to instance a Persistence Implementation. Using fallback as default",
|
||||
e.getCause());
|
||||
persistence = fallbackPersistence;
|
||||
persistence.setAggregationScheduler(AggregationScheduler.getInstance());
|
||||
persistence.setFallback(fallbackPersistence);
|
||||
}
|
||||
persistence.setAggregationScheduler(AggregationScheduler.getInstance());
|
||||
persistence.setFallback(fallbackPersistence);
|
||||
|
||||
return persistence;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,9 +54,9 @@ public class PersistenceConfigurationTest {
|
|||
public static final String FAKE_USERNAME = "fakeusername";
|
||||
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_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_VALUE = "accounting";
|
||||
|
@ -163,7 +163,7 @@ public class PersistenceConfigurationTest {
|
|||
}
|
||||
|
||||
protected void clean(){
|
||||
ScopeProvider.instance.set(GCUBE_DEVSEC_SCOPE);
|
||||
ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE);
|
||||
|
||||
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
|
||||
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY))
|
||||
|
@ -177,17 +177,18 @@ public class PersistenceConfigurationTest {
|
|||
for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
|
||||
try {
|
||||
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
|
||||
serviceEndpoint.id(), GCUBE_DEVSEC_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_DEVSEC_SCOPE, e);
|
||||
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){
|
||||
|
@ -197,7 +198,7 @@ public class PersistenceConfigurationTest {
|
|||
}
|
||||
|
||||
try {
|
||||
PersistenceConfiguration persitenceConfiguration = PersistenceConfiguration.getPersistenceConfiguration(GCUBE_DEVSEC_SCOPE, COUCHDB_CLASS_NAME);
|
||||
PersistenceConfiguration persitenceConfiguration = PersistenceConfiguration.getPersistenceConfiguration(COUCHDB_CLASS_NAME);
|
||||
if(createResource){
|
||||
Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON)));
|
||||
Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0);
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
64Z<34>/U<><55>C<><43>ߘ
|
|
@ -1 +0,0 @@
|
|||
64Z<34>/U<><55>C<><43>ߘ
|
Loading…
Reference in New Issue