From 9ceb6ccbca787189640b46a78b3299482c38d5f9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 5 Jun 2015 16:50:07 +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@115249 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../accounting/datamodel/RawUsageRecord.java | 16 +++--- .../annotations/DeprecatedField.java | 16 ++++++ .../annotations/DeprecationAction.java | 17 ++++++ .../annotations/ValidityChecker.java | 2 +- .../implementations/JobUsageRecord.java | 6 +- .../implementations/PortletUsageRecord.java | 25 +++++++-- .../implementations/ServiceUsageRecord.java | 21 +++---- .../StorageUsageUsageRecord.java | 18 +++--- .../implementations/TaskUsageRecord.java | 26 +++++---- .../validators/ConvertToOperationResult.java | 26 +++++++++ .../datamodel/validators/FieldValidator.java | 15 +++-- .../datamodel/validators/Ignore.java | 31 +++++++++++ .../NotEmptyIfNotNullValidator.java | 26 +-------- .../validators/NotEmptyValidator.java | 22 +++----- .../validators/NotNullValidator.java | 23 ++------ .../validators/ValidIPValidator.java | 27 ++------- .../validators/ValidIntegerValidator.java | 25 ++------- .../validators/ValidLongValidator.java | 33 ++++------- .../ValidOperationResultValidator.java | 55 +++++++++++-------- .../exception/ExcludeException.java | 29 ++++++++++ .../InternallyReservedKeywordException.java | 3 + .../validators/ValidTimeValidatorTest.java | 15 +++-- 22 files changed, 282 insertions(+), 195 deletions(-) create mode 100644 src/main/java/org/gcube/accounting/datamodel/annotations/DeprecatedField.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/annotations/DeprecationAction.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validators/ConvertToOperationResult.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validators/Ignore.java create mode 100644 src/main/java/org/gcube/accounting/exception/ExcludeException.java diff --git a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java index 8a2831d..ce6ba23 100644 --- a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java @@ -72,7 +72,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { */ protected Map resourceSpecificProperties; - protected final Map>> validation; + protected final Map> validation; protected void initializeValidation() { logger.debug("Initializing Field Validators"); @@ -84,12 +84,12 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { } catch (Exception e) { continue; } - List> fieldValidators = new ArrayList>(); + List fieldValidators = new ArrayList(); validation.put(keyString, fieldValidators); for (Annotation annotation : field.getAnnotations()){ if (annotation.annotationType().isAnnotationPresent(ValidityChecker.class)){ - Class> managedClass = ((ValidityChecker)annotation.annotationType().getAnnotation(ValidityChecker.class)).managed(); - FieldValidator validator; + Class managedClass = ((ValidityChecker)annotation.annotationType().getAnnotation(ValidityChecker.class)).managed(); + FieldValidator validator; try { validator = managedClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { @@ -104,7 +104,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { protected RawUsageRecord(){ this.resourceSpecificProperties = new HashMap(); - this.validation = new HashMap>>(); + this.validation = new HashMap>(); initializeValidation(); this.resourceSpecificProperties.put(ID, UUID.randomUUID().toString()); this.resourceSpecificProperties.put(RESOURCE_TYPE, this.getClass().getSimpleName()); @@ -113,7 +113,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { } protected RawUsageRecord(Map properties) throws InvalidValueException { - this.validation = new HashMap>>(); + this.validation = new HashMap>(); initializeValidation(); setResourceSpecificProperties(properties); } @@ -391,9 +391,9 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { protected Serializable validateField(String key, Serializable serializable) throws InvalidValueException { Serializable checkedValue = serializable; - List> fieldValidators = validation.get(key); + List fieldValidators = validation.get(key); if(fieldValidators!=null){ - for(FieldValidator fieldValidator : fieldValidators){ + for(FieldValidator fieldValidator : fieldValidators){ checkedValue = fieldValidator.validate(checkedValue); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/annotations/DeprecatedField.java b/src/main/java/org/gcube/accounting/datamodel/annotations/DeprecatedField.java new file mode 100644 index 0000000..763868c --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/annotations/DeprecatedField.java @@ -0,0 +1,16 @@ +package org.gcube.accounting.datamodel.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface DeprecatedField { + + Class action(); + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/annotations/DeprecationAction.java b/src/main/java/org/gcube/accounting/datamodel/annotations/DeprecationAction.java new file mode 100644 index 0000000..830f71a --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/annotations/DeprecationAction.java @@ -0,0 +1,17 @@ +/** + * + */ +package org.gcube.accounting.datamodel.annotations; + +import java.io.Serializable; + +import org.gcube.accounting.datamodel.UsageRecord; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + */ +public interface DeprecationAction { + + public Serializable manage(String key, Serializable manage, UsageRecord usageRecord); + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/annotations/ValidityChecker.java b/src/main/java/org/gcube/accounting/datamodel/annotations/ValidityChecker.java index 3319e2d..65bf309 100644 --- a/src/main/java/org/gcube/accounting/datamodel/annotations/ValidityChecker.java +++ b/src/main/java/org/gcube/accounting/datamodel/annotations/ValidityChecker.java @@ -20,6 +20,6 @@ import org.gcube.accounting.datamodel.validators.FieldValidator; @Inherited public @interface ValidityChecker { - Class> managed(); + Class managed(); } 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 5caf8e3..0f0fd67 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java @@ -7,10 +7,12 @@ import java.util.Calendar; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.annotations.DeprecatedField; import org.gcube.accounting.datamodel.annotations.NotEmpty; import org.gcube.accounting.datamodel.annotations.Required; import org.gcube.accounting.datamodel.annotations.ValidInteger; import org.gcube.accounting.datamodel.annotations.ValidLong; +import org.gcube.accounting.datamodel.validators.ConvertToOperationResult; import org.gcube.accounting.exception.InvalidValueException; /** @@ -38,8 +40,10 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord public static final String JOB_START_TIME = "jobStartTime"; @Required @ValidLong public static final String JOB_END_TIME = "jobEndTime"; - @NotEmpty + + @DeprecatedField(action=ConvertToOperationResult.class) @NotEmpty //TODO protected static final String JOB_STATUS = "jobStatus"; + @ValidInteger public static final String VMS_USED = "vmsUsed"; @ValidLong 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 b7155d7..4cdaaf8 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java @@ -5,7 +5,11 @@ package org.gcube.accounting.datamodel.implementations; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.annotations.DeprecatedField; import org.gcube.accounting.datamodel.annotations.NotEmpty; +import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull; +import org.gcube.accounting.datamodel.annotations.Required; +import org.gcube.accounting.datamodel.validators.Ignore; import org.gcube.accounting.exception.InvalidValueException; /** @@ -19,24 +23,37 @@ public class PortletUsageRecord extends RawUsageRecord implements SingleUsageRec */ private static final long serialVersionUID = 8339324883678974869L; - @NotEmpty - public static final String USER_ID = "userId"; - @NotEmpty + @DeprecatedField(action=Ignore.class) @NotEmptyIfNotNull + protected static final String USER_ID = "userId"; + + @Required @NotEmpty + public static final String PORTLET_ID = "portletId"; + @Required @NotEmpty public static final String OPERATION_ID = "operationId"; + // TODO More public PortletUsageRecord(){ super(); } - + @Deprecated public String getUserId() { return (String) this.resourceSpecificProperties.get(USER_ID); } + @Deprecated public void setUserId(String userId) throws InvalidValueException { setResourceSpecificProperty(USER_ID, userId); } + public String getPortletId() { + return (String) this.resourceSpecificProperties.get(PORTLET_ID); + } + + public void setPortletId(String portletId) throws InvalidValueException { + setResourceSpecificProperty(PORTLET_ID, portletId); + } + public String getOperationId() { return (String) this.resourceSpecificProperties.get(OPERATION_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 e6a1ff6..4e67899 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java @@ -5,9 +5,12 @@ package org.gcube.accounting.datamodel.implementations; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.annotations.DeprecatedField; import org.gcube.accounting.datamodel.annotations.NotEmpty; +import org.gcube.accounting.datamodel.annotations.Required; import org.gcube.accounting.datamodel.annotations.ValidIP; import org.gcube.accounting.datamodel.annotations.ValidInteger; +import org.gcube.accounting.datamodel.validators.Ignore; import org.gcube.accounting.exception.InvalidValueException; /** @@ -23,20 +26,18 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec @ValidIP public static final String CALLER_IP = "callerIP"; - @NotEmpty + @Required @NotEmpty public static final String CALLER_SCOPE = "callerScope"; - @NotEmpty + @Required @NotEmpty public static final String REF_HOST = "refHost"; - @NotEmpty + @Required @NotEmpty public static final String REF_VM = "refVM"; - @NotEmpty + @DeprecatedField(action=Ignore.class) @NotEmpty public static final String DOMAIN = "domain"; - //@Aggregated @ValidInteger - public static final String INVOCATION_COUNT = "invocationCount"; - //@Aggregated + protected static final String INVOCATION_COUNT = "invocationCount"; @ValidInteger - public static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime"; + protected static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime"; @NotEmpty public static final String SERVICE_CLASS = "serviceClass"; @NotEmpty @@ -78,11 +79,11 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec setResourceSpecificProperty(REF_VM, refVM); } - public String getDomain() { + protected String getDomain() { return (String) this.resourceSpecificProperties.get(DOMAIN); } - public void setDomain(String domain) throws InvalidValueException { + protected void setDomain(String domain) throws InvalidValueException { setResourceSpecificProperty(DOMAIN, domain); } 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 0043870..75a0e3a 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java @@ -3,20 +3,21 @@ */ package org.gcube.accounting.datamodel.implementations; +import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.annotations.NotEmpty; import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull; +import org.gcube.accounting.datamodel.annotations.Required; import org.gcube.accounting.datamodel.annotations.ValidIP; import org.gcube.accounting.datamodel.annotations.ValidInteger; import org.gcube.accounting.datamodel.annotations.ValidLong; -import org.gcube.accounting.datamodel.implementations.aggregated.StorageStatusUsageRecord; import org.gcube.accounting.exception.InvalidValueException; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class StorageUsageUsageRecord extends StorageStatusUsageRecord implements SingleUsageRecord { +public class StorageUsageUsageRecord extends RawUsageRecord implements SingleUsageRecord { /** * Generated Serial Version UID @@ -24,13 +25,11 @@ public class StorageUsageUsageRecord extends StorageStatusUsageRecord implements private static final long serialVersionUID = 1381025822586583326L; @NotEmptyIfNotNull - public static final String OBJECT_URI = "objectURI"; - @NotEmpty - public static final String OPERATION_TYPE = "operationType"; - @ValidIP - public static final String CALLER_IP = "callerIP"; - @NotEmpty public static final String PROVIDER_ID = "providerId"; + @NotEmptyIfNotNull + public static final String OBJECT_URI = "objectURI"; + @Required @NotEmpty + public static final String OPERATION_TYPE = "operationType"; @NotEmpty public static final String QUALIFIER = "qualifier"; @NotEmpty @@ -39,7 +38,8 @@ public class StorageUsageUsageRecord extends StorageStatusUsageRecord implements public static final String DATA_VOLUME = "dataVolume"; @ValidInteger public static final String DATA_COUNT = "dataCount"; - + @ValidIP + public static final String CALLER_IP = "callerIP"; public StorageUsageUsageRecord(){ super(); 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 bd5a016..56a851d 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java @@ -7,9 +7,13 @@ import java.util.Calendar; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.annotations.DeprecatedField; import org.gcube.accounting.datamodel.annotations.NotEmpty; +import org.gcube.accounting.datamodel.annotations.Required; import org.gcube.accounting.datamodel.annotations.ValidInteger; import org.gcube.accounting.datamodel.annotations.ValidLong; +import org.gcube.accounting.datamodel.validators.ConvertToOperationResult; +import org.gcube.accounting.datamodel.validators.Ignore; import org.gcube.accounting.exception.InvalidValueException; /** @@ -27,19 +31,19 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord completed, failed }; - @NotEmpty + @Required @NotEmpty public static final String JOB_ID = "jobId"; - @NotEmpty + @Required @NotEmpty public static final String REF_HOST = "refHost"; - @NotEmpty + @Required @NotEmpty public static final String REF_VM = "refVM"; - @NotEmpty - public static final String DOMAIN = "domain"; - @ValidLong + @DeprecatedField(action=Ignore.class) + protected static final String DOMAIN = "domain"; + @Required @ValidLong public static final String USAGE_START_TIME = "usageStartTime"; - @ValidLong + @Required @ValidLong public static final String USAGE_END_TIME = "usageEndTime"; - @NotEmpty + @DeprecatedField(action=ConvertToOperationResult.class) @NotEmpty public static final String USAGE_PHASE = "usagePhase"; @ValidInteger public static final String INPUT_FILES_NUMBER = "inputFilesNumber"; @@ -94,13 +98,15 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord setResourceSpecificProperty(REF_VM, refVM); } - public String getDomain() { + /* + private String getDomain() { return (String) this.resourceSpecificProperties.get(DOMAIN); } - public void setDomain(String domain) throws InvalidValueException { + private void setDomain(String domain) throws InvalidValueException { setResourceSpecificProperty(DOMAIN, domain); } + */ public Calendar getUsageStartTime() { long millis = (Long) this.resourceSpecificProperties.get(USAGE_START_TIME); diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ConvertToOperationResult.java b/src/main/java/org/gcube/accounting/datamodel/validators/ConvertToOperationResult.java new file mode 100644 index 0000000..09bf4ab --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ConvertToOperationResult.java @@ -0,0 +1,26 @@ +/** + * + */ +package org.gcube.accounting.datamodel.validators; + +import java.io.Serializable; + +import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.annotations.DeprecationAction; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class ConvertToOperationResult implements DeprecationAction { + + /** + * {@inheritDoc} + */ + @Override + public Serializable manage(String key, Serializable manage, UsageRecord usageRecord) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/FieldValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/FieldValidator.java index a6dd573..ed10f66 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/FieldValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/FieldValidator.java @@ -4,21 +4,20 @@ package org.gcube.accounting.datamodel.validators; import java.io.Serializable; -import java.lang.annotation.Annotation; import org.gcube.accounting.exception.InvalidValueException; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ - * */ -public interface FieldValidator { - - Class annotation(); - - public boolean isValid(Serializable toValidate); +public interface FieldValidator { + /** + * Validate (and convert if needed) the provided property + * @param toValidate the property to validate + * @return the validated (and converted property) + * @throws InvalidValueException if the validation (or conversion) fails + */ public Serializable validate(Serializable toValidate) throws InvalidValueException; - String getErrorSuffix(); } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/Ignore.java b/src/main/java/org/gcube/accounting/datamodel/validators/Ignore.java new file mode 100644 index 0000000..70004e1 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validators/Ignore.java @@ -0,0 +1,31 @@ +/** + * + */ +package org.gcube.accounting.datamodel.validators; + +import java.io.Serializable; + +import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.annotations.DeprecationAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class Ignore implements DeprecationAction { + + private static Logger logger = LoggerFactory.getLogger(Ignore.class); + + /** + * {@inheritDoc} + */ + @Override + public Serializable manage(String key, Serializable manage, UsageRecord usageRecord) { + logger.warn("The field {} is deprecated for the {}. Anyway the field will be included in the document", + key, usageRecord.getClass().getSimpleName()); + return manage; + } + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/NotEmptyIfNotNullValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/NotEmptyIfNotNullValidator.java index 1e69a92..346a115 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/NotEmptyIfNotNullValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/NotEmptyIfNotNullValidator.java @@ -3,16 +3,9 @@ package org.gcube.accounting.datamodel.validators; import java.io.Serializable; import java.util.Map; -import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull; -import org.gcube.accounting.exception.InvalidValueException; +public class NotEmptyIfNotNullValidator extends NotEmptyValidator { -public class NotEmptyIfNotNullValidator implements FieldValidator{ - - public Class annotation() { - return NotEmptyIfNotNull.class; - } - - public boolean isValid(Serializable toValidate) { + protected boolean isValid(Serializable toValidate) { if (toValidate == null) return true; if (toValidate.getClass().isArray() ){ return ((Object[])toValidate).length>0; @@ -25,19 +18,4 @@ public class NotEmptyIfNotNullValidator implements FieldValidator{ - - public Class annotation() { - return NotEmpty.class; - } - - public boolean isValid(Serializable toValidate) { +public class NotEmptyValidator implements FieldValidator{ + + private static final String ERROR = "Is Empty"; + + protected boolean isValid(Serializable toValidate) { if (toValidate == null) return false; if (toValidate.getClass().isArray() ){ return ((Object[])toValidate).length>0; @@ -25,19 +22,16 @@ public class NotEmptyValidator implements FieldValidator{ } else return true; } + @Override public Serializable validate(Serializable toValidate) throws InvalidValueException { try{ if(isValid(toValidate)){ return toValidate; } }catch(Exception e){ - throw new InvalidValueException(getErrorSuffix(), e.getCause()); + throw new InvalidValueException(ERROR, e.getCause()); } - throw new InvalidValueException(getErrorSuffix()); - } - - public String getErrorSuffix() { - return "is empty"; + throw new InvalidValueException(ERROR); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/NotNullValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/NotNullValidator.java index 22f0974..93bcd84 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/NotNullValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/NotNullValidator.java @@ -2,29 +2,18 @@ package org.gcube.accounting.datamodel.validators; import java.io.Serializable; -import org.gcube.accounting.datamodel.annotations.NotNull; import org.gcube.accounting.exception.InvalidValueException; - -public class NotNullValidator implements FieldValidator{ - - public Class annotation() { - return NotNull.class; - } - - public boolean isValid(Serializable toValidate) { - return toValidate!=null; - } +public class NotNullValidator implements FieldValidator { + private static final String ERROR = "Is Null"; + + @Override public Serializable validate(Serializable toValidate) throws InvalidValueException { - if(isValid(toValidate)){ + if(toValidate!=null){ return toValidate; } - throw new InvalidValueException(getErrorSuffix()); - } - - public String getErrorSuffix() { - return "is null"; + throw new InvalidValueException(ERROR); } } 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 45ec218..d78f161 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidIPValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIPValidator.java @@ -4,19 +4,16 @@ import java.io.Serializable; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.gcube.accounting.datamodel.annotations.ValidLong; import org.gcube.accounting.exception.InvalidValueException; 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 ValidLong.class; - } - + private static final String ERROR = "Not valid IP Address"; + private static Pattern IPV4_PATTERN = null; private static Pattern IPV6_PATTERN = null; @@ -44,28 +41,16 @@ public class ValidIPValidator implements FieldValidator{ return ipV6Matcher.matches(); } - public boolean isValid(Serializable toValidate) { - try { - validate(toValidate); - } catch (InvalidValueException e) { - return false; - } - return true; - } - + @Override public Serializable validate(Serializable toValidate) throws InvalidValueException { try { if(isIpAddress((String) toValidate)){ return (String) toValidate; } }catch (Exception e) { - throw new InvalidValueException(getErrorSuffix(), e.getCause()); + throw new InvalidValueException(ERROR, e.getCause()); } - throw new InvalidValueException(getErrorSuffix()); - } - - public String getErrorSuffix() { - return "not valid IP Address"; + throw new InvalidValueException(ERROR); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java index 0036a5f..e8b9173 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidIntegerValidator.java @@ -2,25 +2,14 @@ package org.gcube.accounting.datamodel.validators; import java.io.Serializable; -import org.gcube.accounting.datamodel.annotations.ValidLong; import org.gcube.accounting.exception.InvalidValueException; -public class ValidIntegerValidator implements FieldValidator{ - - public Class annotation() { - return ValidLong.class; - } - - public boolean isValid(Serializable toValidate) { - try { - validate(toValidate); - } catch (InvalidValueException e) { - return false; - } - return true; - } +public class ValidIntegerValidator implements FieldValidator { + private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName()); + + @Override public Serializable validate(Serializable toValidate) throws InvalidValueException { if(toValidate instanceof Integer){ return toValidate; @@ -30,11 +19,7 @@ public class ValidIntegerValidator implements FieldValidator{ return integerObj; } - throw new InvalidValueException(getErrorSuffix()); - } - - public String getErrorSuffix() { - return String.format("not instace of %s", Integer.class.getSimpleName()); + throw new InvalidValueException(ERROR); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java index 773903e..d40f0f7 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidLongValidator.java @@ -2,38 +2,27 @@ package org.gcube.accounting.datamodel.validators; import java.io.Serializable; -import org.gcube.accounting.datamodel.annotations.ValidLong; import org.gcube.accounting.exception.InvalidValueException; -public class ValidLongValidator implements FieldValidator{ +public class ValidLongValidator implements FieldValidator { - public Class annotation() { - return ValidLong.class; - } - - public boolean isValid(Serializable toValidate) { - try { - validate(toValidate); - } catch (InvalidValueException e) { - return false; - } - return true; - } + private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName()); + @Override public Serializable validate(Serializable toValidate) throws InvalidValueException { if(toValidate instanceof Long){ return toValidate; } - Long longObj = Long.getLong((String) toValidate); - if(longObj!=null){ - return longObj; + try { + Long longObj = Long.getLong((String) toValidate); + if(longObj!=null){ + return longObj; + } + }catch (Exception e) { + throw new InvalidValueException(ERROR, e.getCause()); } - return false; - } - - public String getErrorSuffix() { - return String.format("not instace of %s", Long.class.getSimpleName()); + throw new InvalidValueException(ERROR); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java index 88d23f9..21d26ff 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java @@ -3,38 +3,49 @@ package org.gcube.accounting.datamodel.validators; import java.io.Serializable; import org.gcube.accounting.datamodel.UsageRecord.OperationResult; -import org.gcube.accounting.datamodel.annotations.ValidOperationResult; import org.gcube.accounting.exception.InvalidValueException; -public class ValidOperationResultValidator implements FieldValidator{ +public class ValidOperationResultValidator implements FieldValidator { - public Class annotation() { - return ValidOperationResult.class; - } - - public boolean isValid(Serializable toValidate) { - try { - validate(toValidate); - } catch (InvalidValueException e) { - return false; - } - return true; - } + private static final String ERROR = String.format("Not Instance of %s", OperationResult.class.getSimpleName()); + @Override public Serializable validate(Serializable toValidate) throws InvalidValueException { if(toValidate instanceof OperationResult){ return toValidate; } - Integer integerObj = Integer.getInteger((String) toValidate); - if(integerObj!=null){ - return integerObj; + + try { + if(toValidate instanceof String){ + try{ + OperationResult operationResult = OperationResult.valueOf((String) toValidate); + if(operationResult !=null){ + return operationResult; + } + } catch(Exception e){ + // Trying another way + } + + try{ + Integer integer = Integer.getInteger((String) toValidate); + if(integer!=null){ + toValidate = integer; + } + } catch(Exception e){ + // Trying another way + } + } + + if(toValidate instanceof Integer){ + return OperationResult.values()[(Integer) toValidate]; + } + + }catch(Exception e){ + throw new InvalidValueException(ERROR, e.getCause()); } - throw new InvalidValueException(getErrorSuffix()); - } - - public String getErrorSuffix() { - return String.format("not instace of %s", OperationResult.class.getSimpleName()); + throw new InvalidValueException(ERROR); } + } diff --git a/src/main/java/org/gcube/accounting/exception/ExcludeException.java b/src/main/java/org/gcube/accounting/exception/ExcludeException.java new file mode 100644 index 0000000..bdfb700 --- /dev/null +++ b/src/main/java/org/gcube/accounting/exception/ExcludeException.java @@ -0,0 +1,29 @@ +/** + * + */ +package org.gcube.accounting.exception; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class ExcludeException extends Exception { + + /** + * Generated serial Version UID + */ + private static final long serialVersionUID = -4512534842977294L; + + public ExcludeException() { + super(); + } + + public ExcludeException(String message) { + super(message); + } + + public ExcludeException(String message, Throwable cause) { + super(message, cause); + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/exception/InternallyReservedKeywordException.java b/src/main/java/org/gcube/accounting/exception/InternallyReservedKeywordException.java index 2b3b660..7ffd097 100644 --- a/src/main/java/org/gcube/accounting/exception/InternallyReservedKeywordException.java +++ b/src/main/java/org/gcube/accounting/exception/InternallyReservedKeywordException.java @@ -22,4 +22,7 @@ public class InternallyReservedKeywordException extends Exception { super(message); } + public InternallyReservedKeywordException(String message, Throwable cause) { + super(message, cause); + } } 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 d620fdb..7ba29a1 100644 --- a/src/test/java/org/gcube/accounting/datamodel/validators/ValidTimeValidatorTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/validators/ValidTimeValidatorTest.java @@ -3,6 +3,7 @@ */ package org.gcube.accounting.datamodel.validators; +import org.gcube.accounting.exception.InvalidValueException; import org.junit.Test; /** @@ -12,17 +13,23 @@ import org.junit.Test; public class ValidTimeValidatorTest { @Test - public void testPrimitiveLong(){ + public void testPrimitiveLong() throws InvalidValueException{ ValidLongValidator validTimeValidator = new ValidLongValidator(); long myLong = 4; - validTimeValidator.isValid(myLong); + validTimeValidator.validate(myLong); } @Test - public void testClassLong(){ + public void testClassLong() throws InvalidValueException{ ValidLongValidator validTimeValidator = new ValidLongValidator(); Long myLong = new Long(4); - validTimeValidator.isValid(myLong); + validTimeValidator.validate(myLong); + } + + @Test(expected=InvalidValueException.class) + public void testWrongValue() throws InvalidValueException { + ValidLongValidator validTimeValidator = new ValidLongValidator(); + validTimeValidator.validate("test"); } }