diff --git a/.classpath b/.classpath index e43402f..f619a53 100644 --- a/.classpath +++ b/.classpath @@ -6,22 +6,12 @@ - - - - - - - - - - diff --git a/pom.xml b/pom.xml index 83da09c..f2c222f 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,11 @@ accounting-lib [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + org.reflections + reflections + LATEST + junit diff --git a/src/main/java/org/gcube/accounting/analytics/Info.java b/src/main/java/org/gcube/accounting/analytics/Info.java index be362d3..877fca9 100644 --- a/src/main/java/org/gcube/accounting/analytics/Info.java +++ b/src/main/java/org/gcube/accounting/analytics/Info.java @@ -3,18 +3,21 @@ */ package org.gcube.accounting.analytics; + /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ public interface Info { - public long getData(); + public long getDate(); public Number getInfo(); + /* public String getUnity(); // Number of Occurrences, Kb, public String getUnityDescription(); // Total Kb accumulated, Single Operation Kb + */ } diff --git a/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java b/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java index 5288a91..a4d6f28 100644 --- a/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java @@ -3,25 +3,58 @@ */ package org.gcube.accounting.analytics; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Set; import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery; -import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; +import org.reflections.Reflections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ */ public class ResourceRecordQuery { - + + private static Logger logger = LoggerFactory.getLogger(ResourceRecordQuery.class); + + protected static Map, Set> resourceRecords = null; + + public static Map, Set> getResourceRecordsTypes() { + if(resourceRecords==null){ + resourceRecords = new HashMap, Set>(); + Package usageRecordPackage = ServiceUsageRecord.class.getPackage(); + Reflections reflections = new Reflections(usageRecordPackage.getName()); + Set> resourceRecordsTypes = reflections.getSubTypesOf(SingleUsageRecord.class); + for(Class resourceRecordsType : resourceRecordsTypes){ + try { + SingleUsageRecord singleUsageRecord = resourceRecordsType.newInstance(); + resourceRecords.put(resourceRecordsType, singleUsageRecord.getRequiredFields()); + } catch (InstantiationException | IllegalAccessException e) { + logger.error(String.format("Unable to correctly istantiate %s", resourceRecordsType.getSimpleName()), e.getCause()); + } + } + + } + return resourceRecords; + } + + protected AccountingPersistenceQuery accountingPersistenceQuery; protected ResourceRecordQuery(AccountingPersistenceQuery accountingPersistenceQuery){ this.accountingPersistenceQuery = accountingPersistenceQuery; } - public List getInfo(Class usageRecordType, + public List getInfo(Class usageRecordType, TemporalConstraint temporalConstraint, List filters) throws Exception{ return accountingPersistenceQuery.query(usageRecordType, temporalConstraint, filters); } + + } diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java index 32962f9..544a000 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java @@ -8,7 +8,7 @@ import java.util.List; import org.gcube.accounting.analytics.Filter; import org.gcube.accounting.analytics.Info; import org.gcube.accounting.analytics.TemporalConstraint; -import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.SingleUsageRecord; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -18,10 +18,10 @@ public abstract class AccountingPersistenceQuery { protected abstract void prepareConnection(AccountingPersistenceQueryConfiguration configuration) throws Exception; - protected abstract List reallyQuery(Class usageRecordType, + protected abstract List reallyQuery(Class usageRecordType, TemporalConstraint temporalConstraint, List filters) throws Exception; - public List query(Class usageRecordType, + public List query(Class usageRecordType, TemporalConstraint temporalConstraint, List filters) throws Exception{ return reallyQuery(usageRecordType, temporalConstraint, filters); } diff --git a/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java b/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java new file mode 100644 index 0000000..58af9bd --- /dev/null +++ b/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java @@ -0,0 +1,64 @@ +/** + * + */ +package org.gcube.accounting.analytics; + +import java.util.HashSet; +import java.util.Set; + +import org.gcube.accounting.datamodel.BasicUsageRecord; +import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.PortletUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.TaskUsageRecord; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + */ +public class ResourceRecordQueryTest { + + public class TestUsageRecord extends BasicUsageRecord implements SingleUsageRecord { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = 1939161386352514727L; + + } + + public static Set> getExpectedResourceRecordsTypes(){ + Set> expected = new HashSet>(); + expected.add(ServiceUsageRecord.class); + expected.add(StorageUsageRecord.class); + expected.add(JobUsageRecord.class); + expected.add(TaskUsageRecord.class); + expected.add(PortletUsageRecord.class); + return expected; + } + + @Test + public void testGetResourceRecordsTypes(){ + Set> expected = getExpectedResourceRecordsTypes(); + Set> found = ResourceRecordQuery.getResourceRecordsTypes().keySet(); + Assert.assertTrue(expected.containsAll(found)); + Assert.assertTrue(found.containsAll(expected)); + } + + + @Test + public void testGetResourceRecordsTypesWithFakeClass(){ + Set> expected = getExpectedResourceRecordsTypes(); + expected.add(TestUsageRecord.class); + Set> found = ResourceRecordQuery.getResourceRecordsTypes().keySet(); + Assert.assertTrue(expected.containsAll(found)); + Assert.assertFalse(found.containsAll(expected)); + } + + + + +} diff --git a/src/test/resources/devNext.gcubekey b/src/test/resources/devNext.gcubekey new file mode 100644 index 0000000..260f269 --- /dev/null +++ b/src/test/resources/devNext.gcubekey @@ -0,0 +1 @@ +6 4Zð/Uä‰ Cå±ß˜ \ No newline at end of file diff --git a/src/test/resources/devsec.gcubekey b/src/test/resources/devsec.gcubekey new file mode 100644 index 0000000..260f269 --- /dev/null +++ b/src/test/resources/devsec.gcubekey @@ -0,0 +1 @@ +6 4Zð/Uä‰ Cå±ß˜ \ No newline at end of file diff --git a/src/test/resources/gcube.gcubekey b/src/test/resources/gcube.gcubekey new file mode 100644 index 0000000..260f269 --- /dev/null +++ b/src/test/resources/gcube.gcubekey @@ -0,0 +1 @@ +6 4Zð/Uä‰ Cå±ß˜ \ 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..4f36cc8 --- /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