From 0bd207402063f099bd44e97209316e8a36307848 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 10 Jun 2015 15:36:50 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Fixing data model for backward compatibility git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115302 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../datamodel/BasicUsageRecord.java | 651 ++++++++++++++++++ .../accounting/datamodel/RawUsageRecord.java | 650 ++--------------- .../implementations/JobUsageRecord.java | 4 +- .../implementations/PortletUsageRecord.java | 4 +- .../implementations/ServiceUsageRecord.java | 4 +- .../implementations/StorageUsageRecord.java | 4 +- .../implementations/TaskUsageRecord.java | 4 +- .../aggregated/StorageStatusUsageRecord.java | 4 +- .../messaging/ResourceAccounting.java | 4 +- .../persistence/CouchDBPersistence.java | 4 +- .../datamodel/RawUsageRecordTest.java | 6 +- 11 files changed, 742 insertions(+), 597 deletions(-) create mode 100644 src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java diff --git a/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java new file mode 100644 index 0000000..7724413 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java @@ -0,0 +1,651 @@ +/** + * + */ +package org.gcube.accounting.datamodel; + +import java.io.Serializable; +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +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 java.util.UUID; + +import org.gcube.accounting.datamodel.decorators.AggregatedField; +import org.gcube.accounting.datamodel.decorators.ComputedField; +import org.gcube.accounting.datamodel.decorators.FieldAction; +import org.gcube.accounting.datamodel.decorators.FieldDecorator; +import org.gcube.accounting.datamodel.decorators.RequiredField; +import org.gcube.accounting.datamodel.deprecationmanagement.annotations.DeprecatedWarning; +import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; +import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull; +import org.gcube.accounting.datamodel.validations.annotations.ValidLong; +import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult; +import org.gcube.accounting.datamodel.validations.validators.NotEmptyIfNotNullValidator; +import org.gcube.accounting.exception.InvalidValueException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public abstract class BasicUsageRecord implements UsageRecord, Serializable { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = -2060728578456796388L; + + private static Logger logger = LoggerFactory.getLogger(BasicUsageRecord.class); + + @RequiredField @NotEmpty + public static final String ID = "id"; + @RequiredField @NotEmpty + public static final String CREATOR_ID = "creatorId"; + @RequiredField @NotEmpty + public static final String CONSUMER_ID = "consumerId"; + @RequiredField @ValidLong + public static final String CREATION_TIME = "creationTime"; + + @AggregatedField @ValidLong + protected static final String START_TIME = "startTime"; + @AggregatedField @ValidLong + protected static final String END_TIME = "endTime"; + + @RequiredField @NotEmpty + protected static final String USAGE_RECORD_TYPE = "usageRecordType"; + + @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 + protected static final String AGGREGATED_ID = "aggregatedId"; + + @Target(ElementType.FIELD) + @Retention(RetentionPolicy.RUNTIME) + @FieldDecorator(managed=MoveToAggregatedUsageRecordIdAction.class) + protected @interface MoveToAggregatedUsageRecordId { } + protected class MoveToAggregatedUsageRecordIdAction implements FieldAction { + @Override + public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException { + NotEmptyIfNotNullValidator neinnv = new NotEmptyIfNotNullValidator(); + value = neinnv.validate(key, value, usageRecord); + usageRecord.setAggregatedUsageRecordId((String) value); + return value; + } + } + + + @RequiredField @ValidOperationResult + public static final String OPERATION_RESULT = "operationResult"; + + /** resource-specific properties */ + protected Map resourceProperties; + + protected Map> validation; + protected Set requiredFields; + protected Set aggregatedFields; + protected Set computedFields; + + protected void initializeValidation() { + logger.debug("Initializing Field Validators"); + List fields = Arrays.asList(this.getClass().getDeclaredFields()); + for(Field field : fields){ + String keyString; + try { + keyString = (String) field.get(null); + } catch (Exception e) { + continue; + } + List fieldValidators = new ArrayList(); + validation.put(keyString, fieldValidators); + for (Annotation annotation : field.getAnnotations()){ + if (annotation.annotationType().isAnnotationPresent(FieldDecorator.class)){ + Class managedClass = ((FieldDecorator)annotation.annotationType().getAnnotation(FieldDecorator.class)).managed(); + FieldAction validator; + try { + validator = managedClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + continue; + } + fieldValidators.add(validator); + } + if(annotation.annotationType().isAssignableFrom(RequiredField.class)){ + requiredFields.add(keyString); + } + if(annotation.annotationType().isAssignableFrom(AggregatedField.class)){ + aggregatedFields.add(keyString); + } + if(annotation.annotationType().isAssignableFrom(ComputedField.class)){ + computedFields.add(keyString); + } + } + } + } + + /** + * Initialize variable + */ + private void init() { + this.validation = new HashMap>(); + this.requiredFields = new HashSet(); + initializeValidation(); + } + + public BasicUsageRecord(){ + init(); + this.resourceProperties = new HashMap(); + this.resourceProperties.put(ID, UUID.randomUUID().toString()); + this.resourceProperties.put(USAGE_RECORD_TYPE, this.getClass().getSimpleName()); + Calendar calendar = Calendar.getInstance(); + this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis()); + } + + public BasicUsageRecord(Map properties) throws InvalidValueException { + init(); + setResourceProperties(properties); + } + + /** + * {@inheritDoc} + */ + @Override + public String getId() { + return (String) this.resourceProperties.get(ID); + } + + /** + * {@inheritDoc} + */ + @Override + public void setId(String id) throws InvalidValueException { + setResourceProperty(ID, id); + } + + /** + * {@inheritDoc} + */ + @Override + public String getCreatorId() { + return (String) this.resourceProperties.get(CREATOR_ID); + } + + /** + * {@inheritDoc} + */ + @Override + public void setCreatorId(String creatorId) throws InvalidValueException { + setResourceProperty(CREATOR_ID, creatorId); + } + + /** + * {@inheritDoc} + */ + @Override + public String getConsumerId() { + return (String) this.resourceProperties.get(CONSUMER_ID); + } + + /** + * {@inheritDoc} + */ + @Override + public void setConsumerId(String consumerId) throws InvalidValueException { + setResourceProperty(CONSUMER_ID, consumerId); + } + + protected Calendar timestampStringToCalendar(long millis){ + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(millis); + return calendar; + } + + /** + * {@inheritDoc} + */ + @Override + public Calendar getCreationTime() { + long millis = (Long) this.resourceProperties.get(CREATION_TIME); + return timestampStringToCalendar(millis); + } + + /** + * {@inheritDoc} + */ + @Override + public void setCreationTime(Calendar creationTime) throws InvalidValueException { + setResourceProperty(CREATION_TIME, creationTime.getTimeInMillis()); + } + + @Override + @Deprecated + public Date getCreateTime() { + long millis = (Long) this.resourceProperties.get(CREATION_TIME); + return timestampStringToCalendar(millis).getTime(); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public void setCreateTime(Date createTime) throws InvalidValueException { + /* + Calendar calendar = Calendar.getInstance(); + calendar.setTime(createTime); + setCreationTime(calendar); + */ + logger.warn("The method is deprecated. Please modify your code as soon as possible"); + } + + /** + * {@inheritDoc} + */ + @Override + public Calendar getStartTime() { + long millis = (Long) this.resourceProperties.get(START_TIME); + return timestampStringToCalendar(millis); + } + + /** + * Set the left end of the time interval covered by this {#UsageRecord} + * @param startTime Start Time + * @throws InvalidValueException + */ + protected void setStartTime(Calendar startTime) throws InvalidValueException { + setResourceProperty(START_TIME, startTime.getTimeInMillis()); + } + + /* * + * {@inheritDoc} + * / + @Override + @Deprecated + public void setStartTime(Date startTime) throws InvalidValueException { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(startTime); + setStartTime(calendar); + } + */ + + /** + * {@inheritDoc} + */ + @Override + public Calendar getEndTime() { + long millis = (Long) this.resourceProperties.get(END_TIME); + return timestampStringToCalendar(millis); + } + + /** + * Set the right end of the time interval covered by this {#UsageRecord} + * @param endTime End Time + * @throws InvalidValueException + */ + protected void setEndTime(Calendar endTime) throws InvalidValueException { + setResourceProperty(END_TIME, endTime.getTimeInMillis()); + } + + /* * + * {@inheritDoc} + * / + @Override + @Deprecated + public void setEndTime(Date endTime) throws InvalidValueException { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(endTime); + setEndTime(calendar); + } + */ + + protected String getUsageRecordType() { + //return (String) this.resourceSpecificProperties.get(RESOURCE_TYPE); + return this.getClass().getSimpleName(); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public String getResourceType(){ + return (String) this.resourceProperties.get(USAGE_RECORD_TYPE); + } + + /** + * {@inheritDoc} + */ + @Deprecated + public void setResourceType(String resourceType){ + logger.warn("The method is deprecated. Please modify your code as soon as possible"); + } + + /** + * {@inheritDoc} + */ + @Override + public String getResourceScope() { + return (String) this.resourceProperties.get(RESOURCE_SCOPE); + } + + /** + * {@inheritDoc} + */ + @Override + public void setResourceScope(String scope) throws InvalidValueException { + setResourceProperty(RESOURCE_SCOPE, scope); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public String getResourceOwner() { + return (String) this.resourceProperties.get(RESOURCE_OWNER); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public void setResourceOwner(String owner) throws InvalidValueException { + setResourceProperty(RESOURCE_OWNER, owner); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public String getAggregatedId() { + return (String) this.resourceProperties.get(AGGREGATED_ID); + } + + /** + * {@inheritDoc} + */ + @Override@ + Deprecated + public void setAggregatedId(String aggregatedId) throws InvalidValueException { + setResourceProperty(AGGREGATED_ID, aggregatedId); + } + + /** + * {@inheritDoc} + */ + @Override + public String getAggregatedUsageRecordId() { + return (String) this.resourceProperties.get(AGGREGATED_USAGE_RECORD_ID); + } + + /** + * {@inheritDoc} + */ + @Override + public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException { + setResourceProperty(AGGREGATED_USAGE_RECORD_ID, aggregatedId); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public Map getResourceSpecificProperties() { + return getResourceProperties(); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public void setResourceSpecificProperties(Map properties) throws InvalidValueException { + setResourceProperties(properties); + } + + + /** + * {@inheritDoc} + */ + @Override + public Map getResourceProperties() { + return this.resourceProperties; + } + + /** + * {@inheritDoc} + */ + @Override + public void setResourceProperties(Map properties) throws InvalidValueException { + validateProperties(properties); + this.resourceProperties = new HashMap(properties); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public Serializable getResourceSpecificProperty(String key) { + return getResourceProperty(key); + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public void setResourceSpecificProperty(String key, Serializable value) throws InvalidValueException { + setResourceProperty(key, value); + } + + /** + * {@inheritDoc} + */ + @Override + public Serializable getResourceProperty(String key) { + return this.resourceProperties.get(key); + } + + /** + * {@inheritDoc} + */ + @Override + public void setResourceProperty(String key, Serializable value) throws InvalidValueException { + Serializable checkedValue = validateField(key, value); + if(checkedValue == null){ + this.resourceProperties.remove(key); + }else{ + this.resourceProperties.put(key, checkedValue); + } + } + + protected Serializable validateField(String key, Serializable serializable) throws InvalidValueException { + if(key == null){ + throw new InvalidValueException("The key of property to set cannot be null"); + } + Serializable checkedValue = serializable; + List fieldValidators = validation.get(key); + if(fieldValidators!=null){ + for(FieldAction fieldValidator : fieldValidators){ + if(aggregatedFields.contains(key)){ + // TODO + } + if(computedFields.contains(key)){ + logger.debug("{} is a computed field. To be calculated all the required fields to calcutalate it MUST be set. " + + "In any case the provided value is ignored."); + } + checkedValue = fieldValidator.validate(key, checkedValue, this); + } + } + return checkedValue; + } + + protected void validateProperties(Map properties) throws InvalidValueException{ + for(String key : properties.keySet()){ + Serializable serializable = properties.get(key); + validateField(key, serializable); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void validate() throws InvalidValueException { + validateProperties(this.resourceProperties); + Set notPresentProperties = new HashSet(); + for(String key : this.requiredFields){ + if(!this.resourceProperties.containsKey(key)){ + notPresentProperties.add(key); + } + } + if(!notPresentProperties.isEmpty()){ + String pluralManagement = notPresentProperties.size() == 1 ? "y" : "ies"; + throw new InvalidValueException(String.format("The Usage Record does not contain the following required propert%s %s", pluralManagement, notPresentProperties.toString())); + } + } + + @Override + public String toString(){ + return resourceProperties.toString(); + } + + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public String getFullyQualifiedConsumerId() { + return getConsumerId(); + } + + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public void setFullyQualifiedConsumerId(String fqcid) { } + + /** + * {@inheritDoc} + */ + @Override + public OperationResult getOperationResult(){ + return OperationResult.valueOf((String) this.resourceProperties.get(OPERATION_RESULT)); + } + + /** + * {@inheritDoc} + * @throws InvalidValueException + */ + @Override + public void setOperationResult(OperationResult operationResult) throws InvalidValueException { + setResourceProperty(OPERATION_RESULT, operationResult); + } + + + /** + * Compare this UsageRecord instance with the one provided as argument + * @param usageRecord the Usage Record to compare + * @return 0 is and only if the UsageRecord provided as parameter + * contains all and ONLY the parameters contained in this instance. + * If the number of parameters differs, the methods return the difference + * between the number of parameter in this instance and the ones in the + * UsageRecord provided as parameter. + * If the size is the same but the UsageRecord provided as parameter does + * not contains all parameters in this instance, -1 is returned. + */ + @Override + public int compareTo(UsageRecord usageRecord) { + Set> thisSet = this.resourceProperties.entrySet(); + Set> usageRecordSet = usageRecord.getResourceProperties().entrySet(); + if(thisSet.size() != usageRecordSet.size()){ + return thisSet.size() - usageRecordSet.size(); + } + if(usageRecordSet.containsAll(thisSet)){ + return 0; + } + 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 + * the right UsageRecord class and return it. If the type implementation + * does not exist or the validation of one or more field validation fails + * an exception is thrown + * @param usageRecordMap + * @return the instance of the UsageRecord class. + * @throws Exception if fails + */ + public static UsageRecord getUsageRecord(Map usageRecordMap) throws Exception { + String className = (String) usageRecordMap.get(USAGE_RECORD_TYPE); + boolean aggregated = false; + try { + aggregated = (Boolean) usageRecordMap.get(AGGREGATED); + }catch(Exception e){} + + 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}; + + UsageRecord usageRecord = usageRecordConstructor.newInstance(usageRecordArguments); + + logger.debug("Created Usage Record : {}", usageRecord); + + return usageRecord; + } + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java index 94d9c41..62968ad 100644 --- a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java @@ -4,36 +4,23 @@ package org.gcube.accounting.datamodel; import java.io.Serializable; -import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Calendar; import java.util.Date; 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 java.util.UUID; -import org.gcube.accounting.datamodel.decorators.AggregatedField; -import org.gcube.accounting.datamodel.decorators.ComputedField; import org.gcube.accounting.datamodel.decorators.FieldAction; import org.gcube.accounting.datamodel.decorators.FieldDecorator; -import org.gcube.accounting.datamodel.decorators.RequiredField; import org.gcube.accounting.datamodel.deprecationmanagement.annotations.DeprecatedWarning; -import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; -import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull; -import org.gcube.accounting.datamodel.validations.annotations.ValidLong; -import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult; -import org.gcube.accounting.datamodel.validations.validators.NotEmptyIfNotNullValidator; +import org.gcube.accounting.datamodel.implementations.JobUsageRecord; +import org.gcube.accounting.datamodel.implementations.PortletUsageRecord; +import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord; +import org.gcube.accounting.datamodel.implementations.StorageUsageRecord; +import org.gcube.accounting.datamodel.implementations.TaskUsageRecord; import org.gcube.accounting.exception.InvalidValueException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,284 +29,96 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public abstract class RawUsageRecord implements UsageRecord, Serializable { +@Deprecated +public class RawUsageRecord extends BasicUsageRecord{ + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = 1203390363640634895L; + private static Logger logger = LoggerFactory.getLogger(RawUsageRecord.class); - @RequiredField @NotEmpty - public static final String ID = "id"; - @RequiredField @NotEmpty - public static final String CREATOR_ID = "creatorId"; - @RequiredField @NotEmpty - public static final String CONSUMER_ID = "consumerId"; - @RequiredField @ValidLong - public static final String CREATION_TIME = "creationTime"; - @AggregatedField @ValidLong - protected static final String START_TIME = "startTime"; - @AggregatedField @ValidLong - protected static final String END_TIME = "endTime"; - - @DeprecatedWarning - protected static final String RESOURCE_TYPE = "resourceType"; - @RequiredField @NotEmpty - protected static final String USAGE_RECORD_TYPE = "UsageRecordType"; - - @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 - protected static final String AGGREGATED_ID = "aggregatedId"; + @MoveToCreationTime + protected static final String CREATE_TIME = "createTime"; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) - @FieldDecorator(managed=MoveToAggregatedUsageRecordIdAction.class) - protected @interface MoveToAggregatedUsageRecordId { } - protected class MoveToAggregatedUsageRecordIdAction implements FieldAction { + @FieldDecorator(managed=MoveToCreationTimeAction.class) + protected @interface MoveToCreationTime { } + protected class MoveToCreationTimeAction implements FieldAction { @Override public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException { - NotEmptyIfNotNullValidator neinnv = new NotEmptyIfNotNullValidator(); - value = neinnv.validate(key, value, usageRecord); - usageRecord.setAggregatedUsageRecordId((String) value); + if(value instanceof Date){ + Calendar calendar = Calendar.getInstance(); + calendar.setTime((Date) value); + value = calendar; + } + if(value instanceof Long){ + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis((Long) value); + value = calendar; + } + + if(value instanceof Calendar){ + usageRecord.setCreationTime((Calendar) value); + }else{ + throw new InvalidValueException(); + } + return value; } } + @DeprecatedWarning + protected static final String RESOURCE_TYPE = "resourceType"; + @Target(ElementType.FIELD) + @Retention(RetentionPolicy.RUNTIME) + @FieldDecorator(managed=MoveToUsageRecordTypeAction.class) + protected @interface MoveToUsageRecordType { } + private final static Map resourceTypeMapping; - @RequiredField @ValidOperationResult - public static final String OPERATION_RESULT = "operationResult"; + private final static String JOB = "job"; + private final static String TASK = "task"; + private final static String PORTLET = "portlet"; + private final static String SERVICE = "service"; + private final static String STORAGE_USAGE = "storage-usage"; - /** - * Generated Serial Version UID - */ - private static final long serialVersionUID = -2060728578456796388L; + static { + resourceTypeMapping = new HashMap(); + resourceTypeMapping.put(JOB, JobUsageRecord.class.getSimpleName()); + resourceTypeMapping.put(TASK, TaskUsageRecord.class.getSimpleName()); + resourceTypeMapping.put(PORTLET, PortletUsageRecord.class.getSimpleName()); + resourceTypeMapping.put(SERVICE, ServiceUsageRecord.class.getSimpleName()); + resourceTypeMapping.put(STORAGE_USAGE, StorageUsageRecord.class.getSimpleName()); + + } - /** resource-specific properties */ - protected Map resourceProperties; - - protected Map> validation; - protected Set requiredFields; - protected Set aggregatedFields; - protected Set computedFields; - - protected void initializeValidation() { - logger.debug("Initializing Field Validators"); - List fields = Arrays.asList(this.getClass().getDeclaredFields()); - for(Field field : fields){ - String keyString; - try { - keyString = (String) field.get(null); - } catch (Exception e) { - continue; - } - List fieldValidators = new ArrayList(); - validation.put(keyString, fieldValidators); - for (Annotation annotation : field.getAnnotations()){ - if (annotation.annotationType().isAnnotationPresent(FieldDecorator.class)){ - Class managedClass = ((FieldDecorator)annotation.annotationType().getAnnotation(FieldDecorator.class)).managed(); - FieldAction validator; - try { - validator = managedClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - continue; - } - fieldValidators.add(validator); - } - if(annotation.annotationType().isAssignableFrom(RequiredField.class)){ - requiredFields.add(keyString); - } - if(annotation.annotationType().isAssignableFrom(AggregatedField.class)){ - aggregatedFields.add(keyString); - } - if(annotation.annotationType().isAssignableFrom(ComputedField.class)){ - computedFields.add(keyString); - } + protected class MoveToUsageRecordTypeAction implements FieldAction { + + @Override + public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException { + if(value instanceof String){ + String newValue = resourceTypeMapping.get(value); + if(newValue == null){ + throw new InvalidValueException(); + } + resourceProperties.put(USAGE_RECORD_TYPE, newValue); + }else{ + throw new InvalidValueException(); } + return value; } } - /** - * Initialize variable - */ - private void init() { - this.validation = new HashMap>(); - this.requiredFields = new HashSet(); - initializeValidation(); - } - public RawUsageRecord(){ - init(); - this.resourceProperties = new HashMap(); - this.resourceProperties.put(ID, UUID.randomUUID().toString()); - this.resourceProperties.put(USAGE_RECORD_TYPE, this.getClass().getSimpleName()); - Calendar calendar = Calendar.getInstance(); - this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis()); + super(); + this.resourceProperties.remove(USAGE_RECORD_TYPE); } - + public RawUsageRecord(Map properties) throws InvalidValueException { - init(); - setResourceProperties(properties); - } - - /** - * {@inheritDoc} - */ - @Override - public String getId() { - return (String) this.resourceProperties.get(ID); - } - - /** - * {@inheritDoc} - */ - @Override - public void setId(String id) throws InvalidValueException { - setResourceProperty(ID, id); - } - - /** - * {@inheritDoc} - */ - @Override - public String getCreatorId() { - return (String) this.resourceProperties.get(CREATOR_ID); - } - - /** - * {@inheritDoc} - */ - @Override - public void setCreatorId(String creatorId) throws InvalidValueException { - setResourceProperty(CREATOR_ID, creatorId); - } - - /** - * {@inheritDoc} - */ - @Override - public String getConsumerId() { - return (String) this.resourceProperties.get(CONSUMER_ID); - } - - /** - * {@inheritDoc} - */ - @Override - public void setConsumerId(String consumerId) throws InvalidValueException { - setResourceProperty(CONSUMER_ID, consumerId); - } - - protected Calendar timestampStringToCalendar(long millis){ - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(millis); - return calendar; - } - - /** - * {@inheritDoc} - */ - @Override - public Calendar getCreationTime() { - long millis = (Long) this.resourceProperties.get(CREATION_TIME); - return timestampStringToCalendar(millis); - } - - /** - * {@inheritDoc} - */ - @Override - public void setCreationTime(Calendar creationTime) throws InvalidValueException { - setResourceProperty(CREATION_TIME, creationTime.getTimeInMillis()); - } - - @Override - @Deprecated - public Date getCreateTime() { - long millis = (Long) this.resourceProperties.get(CREATION_TIME); - return timestampStringToCalendar(millis).getTime(); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public void setCreateTime(Date createTime) throws InvalidValueException { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(createTime); - setCreationTime(calendar); - } - - /** - * {@inheritDoc} - */ - @Override - public Calendar getStartTime() { - long millis = (Long) this.resourceProperties.get(START_TIME); - return timestampStringToCalendar(millis); - } - - /** - * Set the left end of the time interval covered by this {#UsageRecord} - * @param startTime Start Time - * @throws InvalidValueException - */ - protected void setStartTime(Calendar startTime) throws InvalidValueException { - setResourceProperty(START_TIME, startTime.getTimeInMillis()); - } - - /* * - * {@inheritDoc} - * / - @Override - @Deprecated - public void setStartTime(Date startTime) throws InvalidValueException { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(startTime); - setStartTime(calendar); - } - */ - - /** - * {@inheritDoc} - */ - @Override - public Calendar getEndTime() { - long millis = (Long) this.resourceProperties.get(END_TIME); - return timestampStringToCalendar(millis); - } - - /** - * Set the right end of the time interval covered by this {#UsageRecord} - * @param endTime End Time - * @throws InvalidValueException - */ - protected void setEndTime(Calendar endTime) throws InvalidValueException { - setResourceProperty(END_TIME, endTime.getTimeInMillis()); - } - - /* * - * {@inheritDoc} - * / - @Override - @Deprecated - public void setEndTime(Date endTime) throws InvalidValueException { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(endTime); - setEndTime(calendar); - } - */ - - protected String getUsageRecordType() { - //return (String) this.resourceSpecificProperties.get(RESOURCE_TYPE); - return this.getClass().getSimpleName(); + super(properties); } /** @@ -343,312 +142,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable { } } - /** - * {@inheritDoc} - */ - @Override - public String getResourceScope() { - return (String) this.resourceProperties.get(RESOURCE_SCOPE); - } - - /** - * {@inheritDoc} - */ - @Override - public void setResourceScope(String scope) throws InvalidValueException { - setResourceProperty(RESOURCE_SCOPE, scope); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public String getResourceOwner() { - return (String) this.resourceProperties.get(RESOURCE_OWNER); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public void setResourceOwner(String owner) throws InvalidValueException { - setResourceProperty(RESOURCE_OWNER, owner); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public String getAggregatedId() { - return (String) this.resourceProperties.get(AGGREGATED_ID); - } - - /** - * {@inheritDoc} - */ - @Override@ - Deprecated - public void setAggregatedId(String aggregatedId) throws InvalidValueException { - setResourceProperty(AGGREGATED_ID, aggregatedId); - } - - /** - * {@inheritDoc} - */ - @Override - public String getAggregatedUsageRecordId() { - return (String) this.resourceProperties.get(AGGREGATED_USAGE_RECORD_ID); - } - - /** - * {@inheritDoc} - */ - @Override - public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException { - setResourceProperty(AGGREGATED_USAGE_RECORD_ID, aggregatedId); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public Map getResourceSpecificProperties() { - return getResourceProperties(); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public void setResourceSpecificProperties(Map properties) throws InvalidValueException { - setResourceProperties(properties); - } - - - /** - * {@inheritDoc} - */ - @Override - public Map getResourceProperties() { - return this.resourceProperties; - } - - /** - * {@inheritDoc} - */ - @Override - public void setResourceProperties(Map properties) throws InvalidValueException { - validateProperties(properties); - this.resourceProperties = new HashMap(properties); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public Serializable getResourceSpecificProperty(String key) { - return getResourceProperty(key); - } - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public void setResourceSpecificProperty(String key, Serializable value) throws InvalidValueException { - setResourceProperty(key, value); - } - - /** - * {@inheritDoc} - */ - @Override - public Serializable getResourceProperty(String key) { - return this.resourceProperties.get(key); - } - - /** - * {@inheritDoc} - */ - @Override - public void setResourceProperty(String key, Serializable value) throws InvalidValueException { - Serializable checkedValue = validateField(key, value); - if(checkedValue == null){ - this.resourceProperties.remove(key); - }else{ - this.resourceProperties.put(key, checkedValue); - } - } - - protected Serializable validateField(String key, Serializable serializable) throws InvalidValueException { - if(key == null){ - throw new InvalidValueException("The key of property to set cannot be null"); - } - Serializable checkedValue = serializable; - List fieldValidators = validation.get(key); - if(fieldValidators!=null){ - for(FieldAction fieldValidator : fieldValidators){ - if(aggregatedFields.contains(key)){ - // TODO - } - if(computedFields.contains(key)){ - logger.debug("{} is a computed field. To be calculated all the required fields to calcutalate it MUST be set. " - + "In any case the provided value is ignored."); - } - checkedValue = fieldValidator.validate(key, checkedValue, this); - } - } - return checkedValue; - } - - protected void validateProperties(Map properties) throws InvalidValueException{ - for(String key : properties.keySet()){ - Serializable serializable = properties.get(key); - validateField(key, serializable); - } - } - - /** - * {@inheritDoc} - */ - @Override - public void validate() throws InvalidValueException { - validateProperties(this.resourceProperties); - Set notPresentProperties = new HashSet(); - for(String key : this.requiredFields){ - if(!this.resourceProperties.containsKey(key)){ - notPresentProperties.add(key); - } - } - if(!notPresentProperties.isEmpty()){ - String pluralManagement = notPresentProperties.size() == 1 ? "y" : "ies"; - throw new InvalidValueException(String.format("The Usage Record does not contain the following required propert%s %s", pluralManagement, notPresentProperties.toString())); - } - } - - @Override - public String toString(){ - return resourceProperties.toString(); - } - - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public String getFullyQualifiedConsumerId() { - return getConsumerId(); - } - - - /** - * {@inheritDoc} - */ - @Override - @Deprecated - public void setFullyQualifiedConsumerId(String fqcid) { } - - /** - * {@inheritDoc} - */ - @Override - public OperationResult getOperationResult(){ - return OperationResult.valueOf((String) this.resourceProperties.get(OPERATION_RESULT)); - } - - /** - * {@inheritDoc} - * @throws InvalidValueException - */ - @Override - public void setOperationResult(OperationResult operationResult) throws InvalidValueException { - setResourceProperty(OPERATION_RESULT, operationResult); - } - /** - * Compare this UsageRecord instance with the one provided as argument - * @param usageRecord the Usage Record to compare - * @return 0 is and only if the UsageRecord provided as parameter - * contains all and ONLY the parameters contained in this instance. - * If the number of parameters differs, the methods return the difference - * between the number of parameter in this instance and the ones in the - * UsageRecord provided as parameter. - * If the size is the same but the UsageRecord provided as parameter does - * not contains all parameters in this instance, -1 is returned. - */ - @Override - public int compareTo(UsageRecord usageRecord) { - Set> thisSet = this.resourceProperties.entrySet(); - Set> usageRecordSet = usageRecord.getResourceProperties().entrySet(); - if(thisSet.size() != usageRecordSet.size()){ - return thisSet.size() - usageRecordSet.size(); - } - if(usageRecordSet.containsAll(thisSet)){ - return 0; - } - 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 - * the right UsageRecord class and return it. If the type implementation - * does not exist or the validation of one or more field validation fails - * an exception is thrown - * @param usageRecordMap - * @return the instance of the UsageRecord class. - * @throws Exception if fails - */ - public static UsageRecord getUsageRecord(Map usageRecordMap) throws Exception { - String className = (String) usageRecordMap.get(USAGE_RECORD_TYPE); - boolean aggregated = false; - try { - aggregated = (Boolean) usageRecordMap.get(AGGREGATED); - }catch(Exception e){} - - 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}; - - UsageRecord usageRecord = usageRecordConstructor.newInstance(usageRecordArguments); - - logger.debug("Created Usage Record : {}", usageRecord); - - return usageRecord; - } - } 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 a1eedad..2ef4015 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/JobUsageRecord.java @@ -11,7 +11,7 @@ import java.lang.annotation.Target; import java.util.Calendar; import java.util.Map; -import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.decorators.ComputedField; @@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord { +public class JobUsageRecord extends BasicUsageRecord implements SingleUsageRecord { private static Logger logger = LoggerFactory.getLogger(JobUsageRecord.class); 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 081410d..aa375a3 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/PortletUsageRecord.java @@ -10,7 +10,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.Map; -import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.decorators.FieldAction; @@ -26,7 +26,7 @@ import org.gcube.accounting.exception.InvalidValueException; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class PortletUsageRecord extends RawUsageRecord implements SingleUsageRecord { +public class PortletUsageRecord extends BasicUsageRecord implements SingleUsageRecord { /** * Generated Serial Version UID 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 1437227..9552033 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/ServiceUsageRecord.java @@ -6,7 +6,7 @@ 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.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.decorators.AggregatedField; import org.gcube.accounting.datamodel.decorators.RequiredField; @@ -20,7 +20,7 @@ import org.gcube.accounting.exception.InvalidValueException; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRecord { +public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageRecord { /** * Generated Serial Version UID diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageRecord.java index 3b5c8ef..3c5c280 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageRecord.java @@ -6,7 +6,7 @@ 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.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.decorators.RequiredField; import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; @@ -21,7 +21,7 @@ import org.gcube.accounting.exception.InvalidValueException; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class StorageUsageRecord extends RawUsageRecord implements SingleUsageRecord { +public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageRecord { /** * Generated Serial Version UID 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 cd5dbab..4c49733 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/TaskUsageRecord.java @@ -7,7 +7,7 @@ import java.io.Serializable; import java.util.Calendar; import java.util.Map; -import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.decorators.RequiredField; import org.gcube.accounting.datamodel.deprecationmanagement.annotations.DeprecatedWarning; @@ -21,7 +21,7 @@ import org.gcube.accounting.exception.InvalidValueException; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord { +public class TaskUsageRecord extends BasicUsageRecord implements SingleUsageRecord { /** * Generated Serial Version UID 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 bf09056..9f55ae2 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 @@ -3,7 +3,7 @@ */ package org.gcube.accounting.datamodel.implementations.aggregated; -import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.BasicUsageRecord; /* import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; import org.gcube.accounting.datamodel.validations.annotations.ValidInteger; @@ -15,7 +15,7 @@ import org.gcube.accounting.exception.InvalidValueException; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ */ @Deprecated -public class StorageStatusUsageRecord extends RawUsageRecord { +public class StorageStatusUsageRecord extends BasicUsageRecord { /** * Generated Serial Version UID diff --git a/src/main/java/org/gcube/accounting/messaging/ResourceAccounting.java b/src/main/java/org/gcube/accounting/messaging/ResourceAccounting.java index c59aa17..e0848d6 100644 --- a/src/main/java/org/gcube/accounting/messaging/ResourceAccounting.java +++ b/src/main/java/org/gcube/accounting/messaging/ResourceAccounting.java @@ -1,6 +1,6 @@ package org.gcube.accounting.messaging; -import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.persistence.Persistence; /** @@ -19,7 +19,7 @@ public class ResourceAccounting { } @Deprecated - public void sendAccountingMessage(RawUsageRecord message){ + public void sendAccountingMessage(BasicUsageRecord message){ persistence.account(message); } diff --git a/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java b/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java index 3f928e4..46d2920 100644 --- a/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java @@ -17,7 +17,7 @@ import org.ektorp.http.StdHttpClient; import org.ektorp.http.StdHttpClient.Builder; import org.ektorp.impl.StdCouchDbConnector; import org.ektorp.impl.StdCouchDbInstance; -import org.gcube.accounting.datamodel.RawUsageRecord; +import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.UsageRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -109,7 +109,7 @@ public class CouchDBPersistence extends Persistence { ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") Map result = mapper.convertValue(jsonNode, Map.class); - UsageRecord usageRecord = RawUsageRecord.getUsageRecord(result); + UsageRecord usageRecord = BasicUsageRecord.getUsageRecord(result); return usageRecord; } diff --git a/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java b/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java index 3171415..99fa847 100644 --- a/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java @@ -24,7 +24,7 @@ public class RawUsageRecordTest { @Test public void testCompareToEqualsObject() throws Exception { UsageRecord usageRecord = Persistence.createTestUsageRecord(); - UsageRecord ur = RawUsageRecord.getUsageRecord(usageRecord.getResourceProperties()); + UsageRecord ur = BasicUsageRecord.getUsageRecord(usageRecord.getResourceProperties()); Assert.assertEquals(0, usageRecord.compareTo(ur)); Assert.assertEquals(0, ur.compareTo(usageRecord)); } @@ -32,7 +32,7 @@ public class RawUsageRecordTest { @Test public void testCompareToComparedAddedProperty() throws Exception { UsageRecord usageRecord = Persistence.createTestUsageRecord(); - UsageRecord ur = RawUsageRecord.getUsageRecord(usageRecord.getResourceProperties()); + UsageRecord ur = BasicUsageRecord.getUsageRecord(usageRecord.getResourceProperties()); for(int i=1; i<31; i++){ ur.setResourceProperty(Integer.toString(i), i); Assert.assertEquals(-i, usageRecord.compareTo(ur)); @@ -43,7 +43,7 @@ public class RawUsageRecordTest { @Test public void testCompareToDifferentForAddedProperties() throws Exception { UsageRecord usageRecord = Persistence.createTestUsageRecord(); - UsageRecord ur = RawUsageRecord.getUsageRecord(usageRecord.getResourceProperties()); + UsageRecord ur = BasicUsageRecord.getUsageRecord(usageRecord.getResourceProperties()); usageRecord.setResourceProperty(Integer.toString(1), 2); ur.setResourceProperty(Integer.toString(2), 2); Assert.assertEquals(1, usageRecord.compareTo(ur));