From 28f552106daee09f1a1dc4ab4b7ae0f526c61620 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 18 Dec 2015 16:04:19 +0000 Subject: [PATCH] refs #1746: Separate Accounting Model and generalize solution https://support.d4science.org/issues/1746 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib--mongodb@121989 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 28 ++ .project | 23 ++ .settings/org.eclipse.core.resources.prefs | 6 + .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 104 +++++++ .../documentstore/persistence/EnumCodec.java | 35 +++ .../persistence/GenericCodec.java | 50 ++++ .../persistence/PersistenceMongoDB.java | 255 ++++++++++++++++++ ...cumentstore.persistence.PersistenceBackend | 1 + .../persistence/PersistenceMongoDBTest.java | 121 +++++++++ .../persistence/PersistenceMongoDBTest.java | 77 ++++++ src/test/resources/logback-test.xml | 16 ++ 13 files changed, 725 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/documentstore/persistence/EnumCodec.java create mode 100644 src/main/java/org/gcube/documentstore/persistence/GenericCodec.java create mode 100644 src/main/java/org/gcube/documentstore/persistence/PersistenceMongoDB.java create mode 100644 src/main/resources/META-INF/services/org.gcube.documentstore.persistence.PersistenceBackend create mode 100644 src/test/java/org/gcube/accounting/persistence/PersistenceMongoDBTest.java create mode 100644 src/test/java/org/gcube/documentstore/persistence/PersistenceMongoDBTest.java create mode 100644 src/test/resources/logback-test.xml diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f5d7d86 --- /dev/null +++ b/.classpath @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..8c451f8 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + document-store-lib-mongodb + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..893c5af --- /dev/null +++ b/pom.xml @@ -0,0 +1,104 @@ + + 4.0.0 + + org.gcube.tools + maven-parent + 1.0.0 + + + org.gcube.data.publishing + document-store-lib-mongodb + 1.0.0-SNAPSHOT + Document Store MongoDB + Document Store MongoDB Implementation + + + UTF-8 + ${project.basedir}/distro + + + + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId} + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId} + https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId} + + + + + org.gcube.data.publishing + document-store-lib + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + org.slf4j + slf4j-api + 1.7.5 + + + org.mongodb + mongo-java-driver + [3.0.0, 3.0.7] + + + org.reflections + reflections + 0.9.9-RC1 + + + + + org.gcube.accounting + accounting-lib + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + test + + + org.gcube.core + common-encryption + [1.0.2-SNAPSHOT, 2.0.0-SNAPSHOT) + test + + + org.gcube.resources + registry-publisher + [1.0.0-SNAPSHOT,) + test + + + junit + junit + 4.11 + test + + + ch.qos.logback + logback-classic + 1.0.13 + test + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + jar-with-dependencies + + + + + package + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/documentstore/persistence/EnumCodec.java b/src/main/java/org/gcube/documentstore/persistence/EnumCodec.java new file mode 100644 index 0000000..84b74bf --- /dev/null +++ b/src/main/java/org/gcube/documentstore/persistence/EnumCodec.java @@ -0,0 +1,35 @@ +/** + * + */ +package org.gcube.documentstore.persistence; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.Codec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; + +public class EnumCodec> implements Codec { + + protected final Class eClass; + + public EnumCodec(Class eClass){ + this.eClass = eClass; + } + + @Override + public void encode(BsonWriter writer, E e, EncoderContext ec) { + writer.writeString(e.name()); + } + + @Override + public Class getEncoderClass() { + return this.eClass; + } + + @Override + public E decode(BsonReader reader, DecoderContext dc) { + String enumString = reader.readString(); + return (E) Enum.valueOf(getEncoderClass(), enumString); + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/documentstore/persistence/GenericCodec.java b/src/main/java/org/gcube/documentstore/persistence/GenericCodec.java new file mode 100644 index 0000000..7a86bd9 --- /dev/null +++ b/src/main/java/org/gcube/documentstore/persistence/GenericCodec.java @@ -0,0 +1,50 @@ +/** + * + */ +package org.gcube.documentstore.persistence; + +import java.io.Serializable; +import java.lang.reflect.Constructor; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.Codec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; + +public class GenericCodec> implements Codec { + + protected final Class tClass; + + public GenericCodec(Class tClass){ + this.tClass = tClass; + } + + @Override + public void encode(BsonWriter writer, T t, EncoderContext ec) { + writer.writeString(t.toString()); + } + + @Override + public Class getEncoderClass() { + return tClass; + } + + public T getFromString(String stringRepresentation) throws Exception { + @SuppressWarnings("rawtypes") + Class[] argTypes = { String.class }; + Constructor constructor = getEncoderClass().getDeclaredConstructor(argTypes); + Object[] arguments = {stringRepresentation}; + return constructor.newInstance(arguments); + } + + @Override + public T decode(BsonReader reader, DecoderContext dc) { + String stringRepresentation = reader.readString(); + try { + return getFromString(stringRepresentation); + } catch(Exception e){ + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/documentstore/persistence/PersistenceMongoDB.java b/src/main/java/org/gcube/documentstore/persistence/PersistenceMongoDB.java new file mode 100644 index 0000000..dc7ab1d --- /dev/null +++ b/src/main/java/org/gcube/documentstore/persistence/PersistenceMongoDB.java @@ -0,0 +1,255 @@ +/** + * + */ +package org.gcube.documentstore.persistence; + +import java.io.Serializable; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.bson.Document; +import org.bson.codecs.Codec; +import org.bson.codecs.configuration.CodecConfigurationException; +import org.bson.codecs.configuration.CodecRegistries; +import org.bson.codecs.configuration.CodecRegistry; +import org.gcube.documentstore.records.Record; +import org.gcube.documentstore.records.RecordUtility; +import org.reflections.Reflections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoCredential; +import com.mongodb.ReadPreference; +import com.mongodb.ServerAddress; +import com.mongodb.client.MongoDatabase; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + */ +public class PersistenceMongoDB extends PersistenceBackend { + + private static final Logger logger = LoggerFactory.getLogger(PersistenceMongoDB.class); + + public static final String URL_PROPERTY_KEY = "URL"; + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String PASSWORD_PROPERTY_KEY = "password"; + public static final String DB_NAME = "dbName"; + public static final String COLLECTION_NAME = "collectionName"; + + protected String collectionName; + + protected static final ReadPreference READ_PREFERENCE; + protected static final MongoClientOptions MONGO_CLIENT_OPTIONS; + + static { + READ_PREFERENCE = ReadPreference.secondaryPreferred(); + + List> additionalCodecs = discoverAdditionalCodecs(); + CodecRegistry mongoDefaultCR = MongoClient.getDefaultCodecRegistry(); + CodecRegistry cr = addCoded(mongoDefaultCR, additionalCodecs); + MONGO_CLIENT_OPTIONS = createMongoClientOptions(cr); + + } + + protected PersistenceBackendConfiguration configuration; + protected MongoClient mongoClient; + protected MongoClientOptions mongoClientOptions; + protected MongoDatabase mongoDatabase; + + public static CodecRegistry addCoded(CodecRegistry cr, List> codecs){ + CodecRegistry crFromCodes = CodecRegistries.fromCodecs(codecs); + return CodecRegistries.fromRegistries(cr, crFromCodes); + } + + protected static MongoClientOptions createMongoClientOptions(CodecRegistry cr){ + /* + mongoClientOptions = MongoClientOptions.builder(). + codecRegistry(cr).connectionsPerHost(10).connectTimeout(30000). + readPreference(READ_PREFERENCE).build(); + */ + return MongoClientOptions.builder().codecRegistry(cr).build(); + } + + public PersistenceMongoDB() throws Exception { + super(); + mongoClientOptions = MONGO_CLIENT_OPTIONS; + } + + @Override + public void close() throws Exception { + mongoClient.close(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static List> discoverAdditionalCodecs(){ + List codecs = new ArrayList(); + + Set> enumClasses = new HashSet<>(); + + Reflections recordClassesReflections = new Reflections(); + Set> recordClasses = recordClassesReflections.getSubTypesOf(Record.class); + for(Class recordClass : recordClasses){ + Class auxClass = recordClass; + while(auxClass!=null){ + Class[] classes = auxClass.getClasses(); + for(Class clz : classes){ + if(clz.isEnum()){ + if(!enumClasses.contains((Class) clz)){ + logger.trace("Found Enum {}", clz); + enumClasses.add((Class) clz); + } + } + } + auxClass = auxClass.getSuperclass(); + } + + } + logger.trace("Found Enums : {}",enumClasses); + + for(Class enumClass : enumClasses){ + EnumCodec enumCodec = new EnumCodec<>(enumClass); + codecs.add(enumCodec); + } + + GenericCodec uriCodec = new GenericCodec<>(URI.class); + codecs.add(uriCodec); + return (List>) codecs; + } + + + /** + * {@inheritDoc} + */ + @Override + protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception { + logger.debug("Preparing Connection for {}", this.getClass().getSimpleName()); + this.configuration = configuration; + + String url = configuration.getProperty(URL_PROPERTY_KEY); + String username = configuration.getProperty(USERNAME_PROPERTY_KEY); + String password = configuration.getProperty(PASSWORD_PROPERTY_KEY); + String dbName = configuration.getProperty(DB_NAME); + + MongoCredential credential = MongoCredential.createScramSha1Credential( + username, dbName, password.toCharArray()); + + url = url.startsWith("http://") ? url.replace("http://", "") : url; + ServerAddress serverAddress = new ServerAddress(url); + + mongoClient = new MongoClient(serverAddress, + Arrays.asList(credential), mongoClientOptions); //, MONGO_CLIENT_OPTIONS); + + mongoDatabase = mongoClient.getDatabase(dbName); + + collectionName = configuration.getProperty(COLLECTION_NAME); + if(mongoDatabase.getCollection(collectionName)==null){ + mongoDatabase.createCollection(collectionName); + } + } + + protected void createItem(Document document) throws Exception { + mongoDatabase.getCollection(collectionName).insertOne(document); + } + + @SuppressWarnings("unchecked") + protected static List> findMissingCodecs(CodecRegistry cr, Record record){ + @SuppressWarnings("rawtypes") + List codecs = new ArrayList<>(); + Collection> properties = record.getResourceProperties().values(); + for(@SuppressWarnings("rawtypes") Comparable value : properties){ + try { + try { + cr.get(value.getClass()); + logger.trace("Codec found for {} : {}", value.getClass(), value); + }catch(CodecConfigurationException cce){ + logger.trace("No Codec found for {} : {}", value.getClass(), value); + if(value.getClass().isEnum()){ + @SuppressWarnings({ "rawtypes" }) + EnumCodec enumCodec = + new EnumCodec<>((Class) value.getClass()); + codecs.add(enumCodec); + logger.trace("Adding {} to manage {} : {}", enumCodec, value.getClass(), value); + }else{ + @SuppressWarnings({ "rawtypes" }) + GenericCodec genericCodec = new GenericCodec<>(value.getClass()); + try { + Comparable recreatedValue = genericCodec.getFromString(value.toString()); + if(value.compareTo(recreatedValue)==0){ + codecs.add(genericCodec); + logger.trace("Adding {} to manage {} : {}", genericCodec, value.getClass(), value); + }else{ + String message = String.format("%s != %s", value, recreatedValue); + throw new Exception(message); + } + }catch(Exception e){ + logger.error("{} cannot be used for {} : {}", GenericCodec.class.getSimpleName(), value.getClass(), value, e); + continue; + } + } + } + }catch(Exception ex){ + logger.error("Error evaluating if {} can be serialized as bson Object", value, ex); + continue; + } + } + return (List>) codecs; + } + + + protected void checkSerializability(Record record) throws Exception{ + CodecRegistry cr = mongoClientOptions.getCodecRegistry(); + List> codecs = findMissingCodecs(cr, record); + + if(!codecs.isEmpty()){ + logger.debug("Recreating Mongo CLient to Add Codecs"); + CodecRegistry newCR = addCoded(cr, (List>) codecs); + mongoClientOptions = createMongoClientOptions(newCR); + mongoClient.close(); + prepareConnection(configuration); + } + } + + /** + * {@inheritDoc} + */ + @Override + protected void reallyAccount(Record record) throws Exception { + checkSerializability(record); + Document document = usageRecordToDocument(record); + createItem(document); + } + + public static Document usageRecordToDocument(Record record) throws Exception { + Document document = new Document(); + document.putAll(record.getResourceProperties()); + return document; + } + + protected static Record documentToUsageRecord(Document document) throws Exception { + Map> map = new + HashMap>(); + Set> set = document.entrySet(); + for(Entry entry : set){ + @SuppressWarnings("unchecked") + Comparable value = + (Comparable) entry.getValue(); + map.put(entry.getKey(), value); + } + Record record = RecordUtility.getRecord(map); + return record; + } + + + + +} diff --git a/src/main/resources/META-INF/services/org.gcube.documentstore.persistence.PersistenceBackend b/src/main/resources/META-INF/services/org.gcube.documentstore.persistence.PersistenceBackend new file mode 100644 index 0000000..f1146b7 --- /dev/null +++ b/src/main/resources/META-INF/services/org.gcube.documentstore.persistence.PersistenceBackend @@ -0,0 +1 @@ +org.gcube.documentstore.persistence.PersistenceMongoDB \ No newline at end of file diff --git a/src/test/java/org/gcube/accounting/persistence/PersistenceMongoDBTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceMongoDBTest.java new file mode 100644 index 0000000..a9f42fc --- /dev/null +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceMongoDBTest.java @@ -0,0 +1,121 @@ +/** + * + */ +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.PersistenceMongoDB; +import org.gcube.documentstore.persistence.PersistenceBackend; +import org.gcube.documentstore.persistence.PersistenceBackendFactory; +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 PersistenceMongoDBTest { + + private static final Logger logger = LoggerFactory.getLogger(PersistenceMongoDBTest.class); + + public static PersistenceBackend getPersistence(){ + ScopeProvider.instance.set("/gcube/devNext"); + PersistenceBackendFactory.setFallbackLocation(null); + return PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get()); + } + + @Test + public void persistenceIsMongoDB() { + PersistenceBackend accountingPersistence = getPersistence(); + Assert.assertTrue(accountingPersistence instanceof PersistenceMongoDB); + } + + 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; + } + } + + 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, 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(PersistenceMongoDB.class); + serviceEndpoint = persitenceConfiguration.getServiceEndpoint( + AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY, AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME, + PersistenceMongoDB.class); + List scopes = new ArrayList(); + scopes.add(ScopeProvider.instance.get()); + unPublishScopedResource(serviceEndpoint, scopes); + } catch(IndexOutOfBoundsException e){ + ScopeProvider.instance.set(parentVREScopeToUse); + AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(PersistenceMongoDB.class); + serviceEndpoint = persitenceConfiguration.getServiceEndpoint( + AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY, + AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME, + PersistenceMongoDB.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 scopes = new ArrayList(); + 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); + + } +} diff --git a/src/test/java/org/gcube/documentstore/persistence/PersistenceMongoDBTest.java b/src/test/java/org/gcube/documentstore/persistence/PersistenceMongoDBTest.java new file mode 100644 index 0000000..e87be81 --- /dev/null +++ b/src/test/java/org/gcube/documentstore/persistence/PersistenceMongoDBTest.java @@ -0,0 +1,77 @@ +/** + * + */ +package org.gcube.documentstore.persistence; + +import java.net.URL; + +import org.bson.Document; +import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord.OperationType; +import org.gcube.accounting.datamodel.basetypes.TestUsageRecord; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.documentstore.records.Record; +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 PersistenceMongoDBTest { + + private static final Logger logger = LoggerFactory.getLogger(PersistenceMongoDBTest.class); + + @Test + public void testJsonNodeUsageRecordConversions() throws Exception { + Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope(); + logger.debug("UsageRecord : {}", record.toString()); + Document document = PersistenceMongoDB.usageRecordToDocument(record); + logger.debug("Document : {}", document.toString()); + Record r = PersistenceMongoDB.documentToUsageRecord(document); + Assert.assertEquals(0, record.compareTo(r)); + Assert.assertEquals(0, r.compareTo(record)); + } + + public enum AUX { + TEST, TESTER, TESTING + } + + @Test + public void testConfiguration() throws Exception{ + ScopeProvider.instance.set("/gcube/devNext"); + URL url = new URL("http://mongo-test.d4science.org"); + + PersistenceBackendConfiguration persitenceConfiguration = PersistenceBackendConfiguration.getUnconfiguredInstance(); + persitenceConfiguration.addProperty(PersistenceMongoDB.URL_PROPERTY_KEY, url.toString()); + persitenceConfiguration.addProperty(PersistenceMongoDB.USERNAME_PROPERTY_KEY, "accounting"); + persitenceConfiguration.addProperty(PersistenceMongoDB.PASSWORD_PROPERTY_KEY, "testpwd"); + persitenceConfiguration.addProperty(PersistenceMongoDB.DB_NAME,"accounting"); + persitenceConfiguration.addProperty(PersistenceMongoDB.COLLECTION_NAME, UsageRecord.class.getSimpleName()); + PersistenceMongoDB mongo = new PersistenceMongoDB(); + mongo.prepareConnection(persitenceConfiguration); + + + Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope(); + record.setResourceProperty("Test", AUX.TESTER); + mongo.reallyAccount(record); + + record = TestUsageRecord.createTestStorageUsageRecordAutomaticScope(); + record.setResourceProperty("Test", AUX.TESTER); + mongo.reallyAccount(record); + + record = TestUsageRecord.createTestJobUsageRecordAutomaticScope(); + mongo.reallyAccount(record); + + } + + @Test + public void test(){ + EnumCodec enumCodec = new EnumCodec(OperationType.class); + Assert.assertEquals(OperationType.class, enumCodec.getEncoderClass()); + } + + +} \ No newline at end of file diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..de895c0 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,16 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n + + + + + + + + + + + \ No newline at end of file