From e43bfe1843388beffe25fb17236ae324c62afd5e Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 9 Jun 2015 13:53:21 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Implementing library git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115267 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../accounting/datamodel/RawUsageRecord.java | 55 ++++++++++++++++--- .../implementations/JobUsageRecord.java | 6 ++ .../implementations/PortletUsageRecord.java | 7 +++ .../implementations/ServiceUsageRecord.java | 7 +++ .../StorageUsageUsageRecord.java | 7 +++ .../implementations/TaskUsageRecord.java | 7 ++- .../aggregated/JobUsageRecord.java | 11 +++- .../aggregated/PortletUsageRecord.java | 11 +++- .../aggregated/ServiceUsageRecord.java | 7 +++ .../aggregated/StorageStatusUsageRecord.java | 5 +- .../aggregated/StorageUsageUsageRecord.java | 9 +++ .../NotEmptyIfNotNullValidatorTest.java | 30 ++++++++++ 12 files changed, 151 insertions(+), 11 deletions(-) create mode 100644 src/test/java/org/gcube/accounting/datamodel/validations/validators/NotEmptyIfNotNullValidatorTest.java diff --git a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java index 92a8e58..5a0917a 100644 --- a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java @@ -5,6 +5,7 @@ package org.gcube.accounting.datamodel; import java.io.Serializable; import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; @@ -52,12 +53,20 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { protected static final String START_TIME = "startTime"; @AggregatedField @ValidLong protected static final String END_TIME = "endTime"; - @RequiredField @NotEmpty - protected static final String RESOURCE_TYPE = "resourceType"; + + //@RequiredField @NotEmpty + //protected static final String RESOURCE_TYPE = "resourceType"; + protected static final String USAGE_RECORD_TYPE = "resourceType"; + + @RequiredField @NotEmpty public static final String RESOURCE_SCOPE = "resourceScope"; @DeprecatedWarning @NotEmpty protected static final String RESOURCE_OWNER = "resourceOwner"; + + @AggregatedField @NotEmptyIfNotNull + protected static final String AGGREGATED = "aggregated"; + @AggregatedField @NotEmptyIfNotNull protected static final String AGGREGATED_USAGE_RECORD_ID = "aggregatedUsageRecordId"; @NotEmptyIfNotNull @MoveToAggregatedUsageRecordId @@ -116,16 +125,16 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { initializeValidation(); } - protected RawUsageRecord(){ + public RawUsageRecord(){ init(); this.resourceProperties = new HashMap(); this.resourceProperties.put(ID, UUID.randomUUID().toString()); - this.resourceProperties.put(RESOURCE_TYPE, this.getClass().getSimpleName()); + this.resourceProperties.put(USAGE_RECORD_TYPE, this.getClass().getSimpleName()); Calendar calendar = Calendar.getInstance(); this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis()); } - protected RawUsageRecord(Map properties) throws InvalidValueException { + public RawUsageRecord(Map properties) throws InvalidValueException { init(); setResourceProperties(properties); } @@ -546,6 +555,28 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { return 1; } + + @SuppressWarnings("unchecked") + protected static Class getClass(String usageRecordName, boolean aggregated) throws ClassNotFoundException { + Class clz = null; + + Class utilityClass = org.gcube.accounting.datamodel.implementations.JobUsageRecord.class; + if(aggregated){ + utilityClass = org.gcube.accounting.datamodel.implementations.aggregated.JobUsageRecord.class; + } + + String classCanonicalName = utilityClass.getCanonicalName(); + classCanonicalName.replace(utilityClass.getSimpleName(), usageRecordName); + + try { + clz = (Class) Class.forName(classCanonicalName); + } catch (ClassNotFoundException e) { + logger.error("Unable to retrieve class {}", classCanonicalName); + throw e; + } + + return clz; + } /** * This method use the resourceType value contained in the Map to instance @@ -557,8 +588,18 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { * @throws Exception if fails */ public static UsageRecord getUsageRecord(Map usageRecordMap) throws Exception { - // TODO implements - throw new UnsupportedOperationException(); + String className = (String) usageRecordMap.get(USAGE_RECORD_TYPE); + boolean aggregated = (Boolean) usageRecordMap.get(AGGREGATED); + + Class clz = getClass(className, aggregated); + logger.debug("Trying to instantiate {}", clz.getClass().getSimpleName()); + + @SuppressWarnings("rawtypes") + Class[] usageRecordArgTypes = { Map.class }; + Constructor usageRecordConstructor = clz.getDeclaredConstructor(usageRecordArgTypes); + Object[] usageRecordArguments = {usageRecordMap}; + + return usageRecordConstructor.newInstance(usageRecordArguments); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java index 75314c9..8e7a4a8 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java @@ -3,7 +3,9 @@ */ package org.gcube.accounting.datamodel.implementations; +import java.io.Serializable; import java.util.Calendar; +import java.util.Map; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; @@ -54,6 +56,10 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord super(); } + public JobUsageRecord(Map properties) throws InvalidValueException{ + super(properties); + } + /** * @return the Job Id */ diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java index 53e80a4..55994a5 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java @@ -3,6 +3,9 @@ */ package org.gcube.accounting.datamodel.implementations; +import java.io.Serializable; +import java.util.Map; + import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.decorators.RequiredField; @@ -35,6 +38,10 @@ public class PortletUsageRecord extends RawUsageRecord implements SingleUsageRec super(); } + public PortletUsageRecord(Map properties) throws InvalidValueException { + super(properties); + } + @Deprecated public String getUserId() { return (String) this.resourceProperties.get(USER_ID); diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java index 4c1b253..6f0055d 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java @@ -3,6 +3,9 @@ */ package org.gcube.accounting.datamodel.implementations; +import java.io.Serializable; +import java.util.Map; + import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.decorators.AggregatedField; @@ -47,6 +50,10 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec super(); } + public ServiceUsageRecord(Map properties) throws InvalidValueException { + super(properties); + } + public String getCallerIP() { return (String) this.resourceProperties.get(CALLER_IP); } diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java index c5a1e8a..37d3123 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java @@ -3,6 +3,9 @@ */ package org.gcube.accounting.datamodel.implementations; +import java.io.Serializable; +import java.util.Map; + import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.decorators.RequiredField; @@ -45,6 +48,10 @@ public class StorageUsageUsageRecord extends RawUsageRecord implements SingleUsa super(); } + public StorageUsageUsageRecord(Map properties) throws InvalidValueException { + super(properties); + } + public String getObjectURI() { return (String) this.resourceProperties.get(OBJECT_URI); } diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java index 24fa070..6f780cf 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java @@ -3,7 +3,9 @@ */ package org.gcube.accounting.datamodel.implementations; +import java.io.Serializable; import java.util.Calendar; +import java.util.Map; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; @@ -61,11 +63,14 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord @ValidInteger public static final String PROCESSORS = "processors"; - public TaskUsageRecord(){ super(); } + public TaskUsageRecord(Map properties) throws InvalidValueException { + super(properties); + } + /** * @return the Job Id */ diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/JobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/JobUsageRecord.java index 38048ed..aea9fac 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/JobUsageRecord.java @@ -3,9 +3,12 @@ */ package org.gcube.accounting.datamodel.implementations.aggregated; +import java.io.Serializable; import java.util.List; +import java.util.Map; import org.gcube.accounting.datamodel.AggregatedUsageRecord; +import org.gcube.accounting.exception.InvalidValueException; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -20,8 +23,14 @@ public class JobUsageRecord extends org.gcube.accounting.datamodel.implementatio public JobUsageRecord(){ super(); + this.resourceProperties.put(AGGREGATED, true); } - + + public JobUsageRecord(Map properties) throws InvalidValueException{ + super(properties); + this.resourceProperties.put(AGGREGATED, true); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/PortletUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/PortletUsageRecord.java index 87819be..3a18d02 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/PortletUsageRecord.java @@ -3,9 +3,12 @@ */ package org.gcube.accounting.datamodel.implementations.aggregated; +import java.io.Serializable; import java.util.List; +import java.util.Map; import org.gcube.accounting.datamodel.AggregatedUsageRecord; +import org.gcube.accounting.exception.InvalidValueException; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -21,8 +24,14 @@ public class PortletUsageRecord extends org.gcube.accounting.datamodel.implement public PortletUsageRecord(){ super(); + this.resourceProperties.put(AGGREGATED, true); } - + + public PortletUsageRecord(Map properties) throws InvalidValueException{ + super(properties); + this.resourceProperties.put(AGGREGATED, true); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/ServiceUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/ServiceUsageRecord.java index e79b6dd..9150673 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/ServiceUsageRecord.java @@ -3,7 +3,9 @@ */ package org.gcube.accounting.datamodel.implementations.aggregated; +import java.io.Serializable; import java.util.List; +import java.util.Map; import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.decorators.AggregatedField; @@ -28,8 +30,13 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement public ServiceUsageRecord(){ super(); + this.resourceProperties.put(AGGREGATED, true); } + public ServiceUsageRecord(Map properties) throws InvalidValueException{ + super(properties); + this.resourceProperties.put(AGGREGATED, true); + } public String getInvocationCount() { return (String) this.resourceProperties.get(INVOCATION_COUNT); diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageStatusUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageStatusUsageRecord.java index 7230f13..bf09056 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageStatusUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageStatusUsageRecord.java @@ -4,10 +4,12 @@ package org.gcube.accounting.datamodel.implementations.aggregated; import org.gcube.accounting.datamodel.RawUsageRecord; +/* import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; import org.gcube.accounting.datamodel.validations.annotations.ValidInteger; import org.gcube.accounting.datamodel.validations.annotations.ValidLong; import org.gcube.accounting.exception.InvalidValueException; +*/ /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -20,6 +22,7 @@ public class StorageStatusUsageRecord extends RawUsageRecord { */ private static final long serialVersionUID = 2456314684466092685L; + /* @NotEmpty public static final String PROVIDER_ID = "providerId"; @NotEmpty @@ -74,5 +77,5 @@ public class StorageStatusUsageRecord extends RawUsageRecord { public void setDataCount(int dataCount) throws InvalidValueException { setResourceProperty(DATA_COUNT, dataCount); } - + */ } diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageUsageUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageUsageUsageRecord.java index c605d81..28b24f4 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageUsageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/aggregated/StorageUsageUsageRecord.java @@ -3,9 +3,12 @@ */ package org.gcube.accounting.datamodel.implementations.aggregated; +import java.io.Serializable; import java.util.List; +import java.util.Map; import org.gcube.accounting.datamodel.AggregatedUsageRecord; +import org.gcube.accounting.exception.InvalidValueException; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -20,6 +23,12 @@ public class StorageUsageUsageRecord extends org.gcube.accounting.datamodel.impl public StorageUsageUsageRecord(){ super(); + this.resourceProperties.put(AGGREGATED, true); + } + + public StorageUsageUsageRecord(Map properties) throws InvalidValueException{ + super(properties); + this.resourceProperties.put(AGGREGATED, true); } /** diff --git a/src/test/java/org/gcube/accounting/datamodel/validations/validators/NotEmptyIfNotNullValidatorTest.java b/src/test/java/org/gcube/accounting/datamodel/validations/validators/NotEmptyIfNotNullValidatorTest.java new file mode 100644 index 0000000..c19e8a0 --- /dev/null +++ b/src/test/java/org/gcube/accounting/datamodel/validations/validators/NotEmptyIfNotNullValidatorTest.java @@ -0,0 +1,30 @@ +/** + * + */ +package org.gcube.accounting.datamodel.validations.validators; + +import java.io.Serializable; + +import org.gcube.accounting.exception.InvalidValueException; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class NotEmptyIfNotNullValidatorTest { + + @Test + public void testBoolean() throws InvalidValueException{ + NotEmptyIfNotNullValidator notEmptyIfNotNullValidator = new NotEmptyIfNotNullValidator(); + Serializable primitiveTrue = notEmptyIfNotNullValidator.validate(null, true, null); + Assert.assertTrue((Boolean) primitiveTrue); + Serializable primitiveFalse = notEmptyIfNotNullValidator.validate(null, false, null); + Assert.assertFalse((Boolean) primitiveFalse); + Serializable booleanClassTrue = notEmptyIfNotNullValidator.validate(null, Boolean.TRUE, null); + Assert.assertTrue((Boolean) booleanClassTrue); + Serializable booleanClassFalse = notEmptyIfNotNullValidator.validate(null, Boolean.FALSE, null); + Assert.assertFalse((Boolean) booleanClassFalse); + } +}