You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.8 KiB
Java
125 lines
4.8 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.accounting.persistence;
|
|
|
|
import java.io.StringWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.List;
|
|
|
|
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.scope.api.ScopeProvider;
|
|
import org.gcube.documentstore.persistence.PersistenceBackend;
|
|
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
|
import org.gcube.documentstore.persistence.PersistenceCouchDB;
|
|
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.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
*
|
|
*/
|
|
public class PersistenceCouchDBTest {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchDBTest.class);
|
|
|
|
public static PersistenceBackend getPersistence(){
|
|
ScopeProvider.instance.set("/gcube/devNext");
|
|
PersistenceBackendFactory.setFallbackLocation(null);
|
|
return PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
|
|
}
|
|
|
|
@Test
|
|
public void persistenceIsCouchDB() {
|
|
PersistenceBackend persistence = getPersistence();
|
|
Assert.assertTrue(persistence instanceof PersistenceCouchDB);
|
|
}
|
|
|
|
private static void publishScopedResource(Resource resource, List<String> 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;
|
|
}
|
|
}
|
|
|
|
private static void unPublishScopedResource(Resource resource, List<String> scopes) throws RegistryNotFoundException, Exception {
|
|
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
|
|
String id = resource.id();
|
|
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, scopes);
|
|
scopedPublisher.remove(resource, scopes);
|
|
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
|
|
}
|
|
|
|
public void testScopeRecheck() throws Exception {
|
|
final String vreScopeToUse = "/gcube/devsec/LucioVRE";
|
|
final String parentVREScopeToUse = "/gcube/devsec";
|
|
|
|
ScopeProvider.instance.set(vreScopeToUse);
|
|
|
|
ServiceEndpoint serviceEndpoint = null;
|
|
try {
|
|
AccountingPersistenceConfiguration persitenceConfiguration =
|
|
new AccountingPersistenceConfiguration(PersistenceCouchDB.class);
|
|
serviceEndpoint = persitenceConfiguration.getServiceEndpoint(
|
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
|
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
|
|
PersistenceCouchDB.class);
|
|
List<String> scopes = new ArrayList<String>();
|
|
scopes.add(ScopeProvider.instance.get());
|
|
unPublishScopedResource(serviceEndpoint, scopes);
|
|
} catch(IndexOutOfBoundsException e){
|
|
ScopeProvider.instance.set(parentVREScopeToUse);
|
|
AccountingPersistenceConfiguration persitenceConfiguration =
|
|
new AccountingPersistenceConfiguration(PersistenceCouchDB.class);
|
|
serviceEndpoint = persitenceConfiguration.getServiceEndpoint(
|
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
|
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
|
|
PersistenceCouchDB.class);
|
|
ScopeProvider.instance.set(vreScopeToUse);
|
|
}
|
|
|
|
|
|
long startTime = Calendar.getInstance().getTimeInMillis();
|
|
long endTime = startTime;
|
|
while(endTime <= (startTime + 10*1000)){ // 10 sec
|
|
endTime = Calendar.getInstance().getTimeInMillis();
|
|
}
|
|
|
|
logger.debug("Going to check First Time");
|
|
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
|
|
logger.debug("First {} : {}", PersistenceBackend.class.getSimpleName(), first);
|
|
|
|
List<String> scopes = new ArrayList<String>();
|
|
scopes.add(ScopeProvider.instance.get());
|
|
publishScopedResource(serviceEndpoint, scopes);
|
|
|
|
startTime = Calendar.getInstance().getTimeInMillis();
|
|
endTime = startTime;
|
|
while(endTime <= (startTime + (PersistenceBackendFactory.FALLBACK_RETRY_TIME + 100))){
|
|
endTime = Calendar.getInstance().getTimeInMillis();
|
|
}
|
|
|
|
logger.debug("Going to check Second Time");
|
|
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
|
|
logger.debug("Second {} : {}", PersistenceBackend.class.getSimpleName(), second);
|
|
|
|
Assert.assertNotEquals(first, second);
|
|
|
|
}
|
|
}
|