From 7203fafe1735734037ec69c16907532b5b3eb510 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 3 Jun 2015 09:57:38 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Fixing validators git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115165 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../accounting/datamodel/RawUsageRecord.java | 8 +++---- .../implementations/JobUsageRecord.java | 11 +++++---- .../implementations/PortletUsageRecord.java | 1 - .../implementations/ServiceUsageRecord.java | 3 +++ .../StorageStatusUsageRecord.java | 18 ++++++++------- .../implementations/TaskUsageRecord.java | 23 ++++++++++--------- .../validators/ValidIPValidator.java | 9 ++++---- .../datamodel/validators/ValidInteger.java | 15 ++++++++++++ ...idator.java => ValidIntegerValidator.java} | 10 ++++---- .../{ValidTime.java => ValidLong.java} | 4 ++-- .../validators/ValidLongValidator.java | 22 ++++++++++++++++++ .../validators/ValidTimeValidatorTest.java | 4 ++-- 12 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 src/main/java/org/gcube/accounting/datamodel/validators/ValidInteger.java rename src/main/java/org/gcube/accounting/datamodel/validators/{ValidTimeValidator.java => ValidIntegerValidator.java} (54%) rename src/main/java/org/gcube/accounting/datamodel/validators/{ValidTime.java => ValidLong.java} (80%) create mode 100644 src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java diff --git a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java index ae6a13b..1385c76 100644 --- a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java @@ -18,7 +18,7 @@ import java.util.Set; import java.util.UUID; import org.gcube.accounting.datamodel.validators.NotEmptyIfNotNull; -import org.gcube.accounting.datamodel.validators.ValidTime; +import org.gcube.accounting.datamodel.validators.ValidLong; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.FieldValidator; import org.gcube.common.validator.annotations.NotEmpty; @@ -40,11 +40,11 @@ public class RawUsageRecord implements UsageRecord, Serializable { public static final String CREATOR_ID = "creatorId"; @NotEmpty public static final String CONSUMER_ID = "consumerId"; - @ValidTime + @ValidLong public static final String CREATION_TIME = "creationTime"; - @ValidTime + @ValidLong public static final String START_TIME = "startTime"; - @ValidTime + @ValidLong public static final String END_TIME = "endTime"; @NotEmpty public static final String RESOURCE_TYPE = "resourceType"; 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 5fe2013..5781d3c 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java @@ -6,7 +6,8 @@ package org.gcube.accounting.datamodel.implementations; import java.util.Calendar; import org.gcube.accounting.datamodel.RawUsageRecord; -import org.gcube.accounting.datamodel.validators.ValidTime; +import org.gcube.accounting.datamodel.validators.ValidInteger; +import org.gcube.accounting.datamodel.validators.ValidLong; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.NotEmpty; @@ -31,15 +32,15 @@ public class JobUsageRecord extends RawUsageRecord { public static final String JOB_QUALIFIER = "jobQualifier"; @NotEmpty public static final String JOB_NAME = "jobName"; - @ValidTime + @ValidLong public static final String JOB_START_TIME = "jobStartTime"; - @ValidTime + @ValidLong public static final String JOB_END_TIME = "jobEndTime"; @NotEmpty public static final String JOB_STATUS = "jobStatus"; - + @ValidInteger public static final String VMS_USED = "vmsUsed"; - + @ValidLong public static final String WALL_DURATION = "wallDuration"; public JobUsageRecord(){ 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 1ac6796..6bc89f4 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java @@ -20,7 +20,6 @@ public class PortletUsageRecord extends RawUsageRecord { @NotEmpty public static final String USER_ID = "userId"; - @NotEmpty public static final String OPERATION_ID = "operationId"; 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 b5e3d1b..6d0f5e7 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java @@ -5,6 +5,7 @@ package org.gcube.accounting.datamodel.implementations; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.validators.ValidIP; +import org.gcube.accounting.datamodel.validators.ValidInteger; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.NotEmpty; @@ -30,8 +31,10 @@ public class ServiceUsageRecord extends RawUsageRecord { @NotEmpty public static final String DOMAIN = "domain"; //@Aggregated + @ValidInteger public static final String INVOCATION_COUNT = "invocationCount"; //@Aggregated + @ValidInteger public static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime"; @NotEmpty public static final String SERVICE_CLASS = "serviceClass"; diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java index 1ea9645..ce95d51 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java @@ -4,6 +4,8 @@ package org.gcube.accounting.datamodel.implementations; import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.validators.ValidInteger; +import org.gcube.accounting.datamodel.validators.ValidLong; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.NotEmpty; @@ -24,9 +26,9 @@ public class StorageStatusUsageRecord extends RawUsageRecord { public static final String QUALIFIER = "qualifier"; @NotEmpty public static final String DATA_TYPE = "dataType"; - + @ValidLong public static final String DATA_VOLUME = "dataVolume"; - + @ValidInteger public static final String DATA_COUNT = "dataCount"; public StorageStatusUsageRecord(){ @@ -57,19 +59,19 @@ public class StorageStatusUsageRecord extends RawUsageRecord { setResourceSpecificProperty(DATA_TYPE, dataType); } - public String getDataVolume() { - return (String) this.resourceSpecificProperties.get(DATA_VOLUME); + public long getDataVolume() { + return (Long) this.resourceSpecificProperties.get(DATA_VOLUME); } - public void setDataVolume(String dataVolume) throws InvalidValueException { + public void setDataVolume(long dataVolume) throws InvalidValueException { setResourceSpecificProperty(DATA_VOLUME, dataVolume); } - public String getDataCount() { - return (String) this.resourceSpecificProperties.get(DATA_COUNT); + public int getDataCount() { + return (Integer) this.resourceSpecificProperties.get(DATA_COUNT); } - public void setDataCount(String dataCount) throws InvalidValueException { + public void setDataCount(int dataCount) throws InvalidValueException { setResourceSpecificProperty(DATA_COUNT, dataCount); } 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 9538154..77d6e61 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java @@ -6,7 +6,8 @@ package org.gcube.accounting.datamodel.implementations; import java.util.Calendar; import org.gcube.accounting.datamodel.RawUsageRecord; -import org.gcube.accounting.datamodel.validators.ValidTime; +import org.gcube.accounting.datamodel.validators.ValidInteger; +import org.gcube.accounting.datamodel.validators.ValidLong; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.NotEmpty; @@ -33,27 +34,27 @@ public class TaskUsageRecord extends RawUsageRecord { public static final String REF_VM = "refVM"; @NotEmpty public static final String DOMAIN = "domain"; - @ValidTime + @ValidLong public static final String USAGE_START_TIME = "usageStartTime"; - @ValidTime + @ValidLong public static final String USAGE_END_TIME = "usageEndTime"; @NotEmpty public static final String USAGE_PHASE = "usagePhase"; - + @ValidInteger public static final String INPUT_FILES_NUMBER = "inputFilesNumber"; - + @ValidLong public static final String INPUT_FILES_SIZE = "inputFilesSize"; - + @ValidInteger public static final String OUTPUT_FILES_NUMBER = "outputFilesNumber"; - + @ValidLong public static final String OUTPUT_FILES_SIZE = "outputFilesSize"; - + @ValidLong public static final String OVERALL_NETWORK_IN = "overallNetworkIn"; - + @ValidLong public static final String OVERALL_NETWORK_OUT = "overallNetworkOut"; - + @ValidInteger public static final String CORES = "cores"; - + @ValidInteger public static final String PROCESSORS = "processors"; diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidIPValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIPValidator.java index ba4e95c..7e7dd8a 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidIPValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIPValidator.java @@ -3,17 +3,16 @@ package org.gcube.accounting.datamodel.validators; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.common.validator.annotations.FieldValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ValidIPValidator implements FieldValidator{ +public class ValidIPValidator implements FieldValidator{ private static Logger logger = LoggerFactory.getLogger(ValidIPValidator.class); - public Class annotation() { - return ValidTime.class; + public Class annotation() { + return ValidLong.class; } private static Pattern IPV4_PATTERN = null; @@ -51,7 +50,7 @@ public class ValidIPValidator implements FieldValidator{ } public String getErrorSuffix() { - return String.format("not instace of %s", UsageRecord.class.getSimpleName()); + return "not valid IP Address"; } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidInteger.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidInteger.java new file mode 100644 index 0000000..1673979 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidInteger.java @@ -0,0 +1,15 @@ +package org.gcube.accounting.datamodel.validators; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.gcube.common.validator.annotations.ValidityChecker; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@ValidityChecker(managed=ValidIntegerValidator.class) +public @interface ValidInteger { + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidTimeValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java similarity index 54% rename from src/main/java/org/gcube/accounting/datamodel/validators/ValidTimeValidator.java rename to src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java index 0e7d980..2aa0309 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidTimeValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java @@ -3,21 +3,21 @@ package org.gcube.accounting.datamodel.validators; import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.common.validator.annotations.FieldValidator; -public class ValidTimeValidator implements FieldValidator{ +public class ValidIntegerValidator implements FieldValidator{ - public Class annotation() { - return ValidTime.class; + public Class annotation() { + return ValidLong.class; } public boolean isValid(Object toValidate) { - if(toValidate instanceof Long){ + if(toValidate instanceof Integer){ return true; } return false; } public String getErrorSuffix() { - return String.format("not instace of %s", UsageRecord.class.getSimpleName()); + return String.format("not instace of %s", Integer.class.getSimpleName()); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidTime.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidLong.java similarity index 80% rename from src/main/java/org/gcube/accounting/datamodel/validators/ValidTime.java rename to src/main/java/org/gcube/accounting/datamodel/validators/ValidLong.java index 4f89d9e..2c0fe00 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidTime.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidLong.java @@ -9,7 +9,7 @@ import org.gcube.common.validator.annotations.ValidityChecker; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) -@ValidityChecker(managed=ValidTimeValidator.class) -public @interface ValidTime { +@ValidityChecker(managed=ValidLongValidator.class) +public @interface ValidLong { } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java new file mode 100644 index 0000000..563d033 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java @@ -0,0 +1,22 @@ +package org.gcube.accounting.datamodel.validators; + +import org.gcube.common.validator.annotations.FieldValidator; + +public class ValidLongValidator implements FieldValidator{ + + public Class annotation() { + return ValidLong.class; + } + + public boolean isValid(Object toValidate) { + if(toValidate instanceof Long){ + return true; + } + return false; + } + + public String getErrorSuffix() { + return String.format("not instace of %s", Long.class.getSimpleName()); + } + +} diff --git a/src/test/java/org/gcube/accounting/datamodel/validators/ValidTimeValidatorTest.java b/src/test/java/org/gcube/accounting/datamodel/validators/ValidTimeValidatorTest.java index 28368b0..d620fdb 100644 --- a/src/test/java/org/gcube/accounting/datamodel/validators/ValidTimeValidatorTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/validators/ValidTimeValidatorTest.java @@ -13,14 +13,14 @@ public class ValidTimeValidatorTest { @Test public void testPrimitiveLong(){ - ValidTimeValidator validTimeValidator = new ValidTimeValidator(); + ValidLongValidator validTimeValidator = new ValidLongValidator(); long myLong = 4; validTimeValidator.isValid(myLong); } @Test public void testClassLong(){ - ValidTimeValidator validTimeValidator = new ValidTimeValidator(); + ValidLongValidator validTimeValidator = new ValidLongValidator(); Long myLong = new Long(4); validTimeValidator.isValid(myLong); }