From 10918da7391621e37d9863edc1d6e8f8b147b2c0 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 26 Jun 2015 10:40:24 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Implementing tests for Aggregation Strategy git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115585 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../datamodel/BasicUsageRecord.java | 2 +- .../datamodel/aggregation/JobUsageRecord.java | 8 +- .../aggregation/PortletUsageRecord.java | 10 +- .../aggregation/ServiceUsageRecord.java | 7 +- .../aggregation/StorageUsageRecord.java | 11 +- ...iceUsageRecordAggregationStrategyTest.java | 20 +-- ...ageUsageRecordAggregationStrategyTest.java | 124 ++++++++++++++++++ 7 files changed, 158 insertions(+), 24 deletions(-) create mode 100644 src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/StorageUsageRecordAggregationStrategyTest.java diff --git a/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java index 0069027..ae48d70 100644 --- a/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java @@ -159,7 +159,7 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable { /** * Initialize variable */ - private void init() { + protected void init() { this.validation = new HashMap>(); this.requiredFields = new HashSet(); this.aggregatedFields = new HashSet(); diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java index 2734ec1..1da9688 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java @@ -20,14 +20,18 @@ public class JobUsageRecord extends org.gcube.accounting.datamodel.implementatio */ private static final long serialVersionUID = -3376423316219914682L; + @Override + protected void init(){ + super.init(); + this.resourceProperties.put(AGGREGATED, true); + } + public JobUsageRecord(){ super(); - this.resourceProperties.put(AGGREGATED, true); } public JobUsageRecord(Map properties) throws InvalidValueException{ super(properties); - this.resourceProperties.put(AGGREGATED, true); } /** diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java index 1390707..7eb48e6 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java @@ -20,15 +20,19 @@ public class PortletUsageRecord extends org.gcube.accounting.datamodel.implement * Generated Serial version UID */ private static final long serialVersionUID = 7445526162102677455L; - + + @Override + protected void init(){ + super.init(); + this.resourceProperties.put(AGGREGATED, true); + } + public PortletUsageRecord(){ super(); - this.resourceProperties.put(AGGREGATED, true); } public PortletUsageRecord(Map properties) throws InvalidValueException{ super(properties); - this.resourceProperties.put(AGGREGATED, true); } /** diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java index 373bcbe..c77ce50 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java @@ -36,18 +36,18 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement @RequiredField @ValidLong @AggregatedField protected static final String MIN_INVOCATION_TIME = "minInvocationTime"; - private void init(){ + @Override + protected void init(){ + super.init(); this.resourceProperties.put(AGGREGATED, true); } public ServiceUsageRecord(){ super(); - init(); } protected ServiceUsageRecord(Map properties) throws InvalidValueException{ super(properties); - init(); } public ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{ @@ -60,7 +60,6 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement this.setCreationTime(creationTime); this.setStartTime(creationTime); this.setEndTime(creationTime); - init(); } public int getInvocationCount() { diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java index f2a40f5..d1b14e2 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java @@ -33,19 +33,22 @@ public class StorageUsageRecord extends org.gcube.accounting.datamodel.implement @RequiredField @ValidInteger @AggregatedField public static final String OPERATION_COUNT = "operationCount"; + @Override + protected void init(){ + super.init(); + this.resourceProperties.put(AGGREGATED, true); + } + public StorageUsageRecord(){ super(); - this.resourceProperties.put(AGGREGATED, true); } public StorageUsageRecord(Map properties) throws InvalidValueException{ super(properties); - this.resourceProperties.put(AGGREGATED, true); } - protected StorageUsageRecord(org.gcube.accounting.datamodel.implementations.StorageUsageRecord usageRecord) throws InvalidValueException{ + public StorageUsageRecord(org.gcube.accounting.datamodel.implementations.StorageUsageRecord usageRecord) throws InvalidValueException{ super(usageRecord.getResourceProperties()); - // TODO } public int getOperationCount() { diff --git a/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/ServiceUsageRecordAggregationStrategyTest.java b/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/ServiceUsageRecordAggregationStrategyTest.java index d1a5c13..2baba56 100644 --- a/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/ServiceUsageRecordAggregationStrategyTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/ServiceUsageRecordAggregationStrategyTest.java @@ -39,22 +39,22 @@ public class ServiceUsageRecordAggregationStrategyTest { logger.debug("ServiceUsageRecord 2 : {}", serviceUsageRecord2); ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated); - long firstduration = serviceUsageRecord.getDuration(); + long firstDuration = serviceUsageRecord.getDuration(); long secondDuration = serviceUsageRecord2.getDuration(); suras.aggregate(serviceUsageRecord2); logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated); aggregated.validate(); - Assert.assertTrue(aggregated.getDuration() == ((firstduration + secondDuration)/2)); + Assert.assertTrue(aggregated.getDuration() == ((firstDuration + secondDuration)/2)); Assert.assertTrue(aggregated.getInvocationCount() == 2); - if(firstduration >= secondDuration){ - Assert.assertTrue(aggregated.getMaxInvocationTime() == firstduration); + if(firstDuration >= secondDuration){ + Assert.assertTrue(aggregated.getMaxInvocationTime() == firstDuration); Assert.assertTrue(aggregated.getMinInvocationTime() == secondDuration); }else{ Assert.assertTrue(aggregated.getMaxInvocationTime() == secondDuration); - Assert.assertTrue(aggregated.getMinInvocationTime() == firstduration); + Assert.assertTrue(aggregated.getMinInvocationTime() == firstDuration); } @@ -75,17 +75,17 @@ public class ServiceUsageRecordAggregationStrategyTest { ServiceUsageRecord serviceUsageRecord2 = TestUsageRecord.createTestServiceUsageRecord(); serviceUsageRecord2.validate(); logger.debug("ServiceUsageRecord 2 : {}", serviceUsageRecord2); - org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord converted2 = + org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord converted = new org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord(serviceUsageRecord2); - logger.debug("ServiceUsageRecord 2 Converted to Aggregated: {}", converted2); - converted2.validate(); + logger.debug("ServiceUsageRecord 2 Converted to Aggregated: {}", converted); + converted.validate(); ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated); long firstduration = aggregated.getDuration(); - long secondDuration = converted2.getDuration(); + long secondDuration = converted.getDuration(); - suras.aggregate(converted2); + suras.aggregate(converted); logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated); aggregated.validate(); diff --git a/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/StorageUsageRecordAggregationStrategyTest.java b/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/StorageUsageRecordAggregationStrategyTest.java new file mode 100644 index 0000000..d35544e --- /dev/null +++ b/src/test/java/org/gcube/accounting/datamodel/aggreagtion/aggregationstrategy/StorageUsageRecordAggregationStrategyTest.java @@ -0,0 +1,124 @@ +/** + * + */ +package org.gcube.accounting.datamodel.aggreagtion.aggregationstrategy; + +import org.gcube.accounting.datamodel.TestUsageRecord; +import org.gcube.accounting.datamodel.aggregation.aggregationstrategy.StorageUsageRecordAggregationStrategy; +import org.gcube.accounting.datamodel.implementations.StorageUsageRecord; +import org.gcube.accounting.exception.InvalidValueException; +import org.gcube.accounting.exception.NotAggregatableRecordsExceptions; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class StorageUsageRecordAggregationStrategyTest { + + + private static Logger logger = LoggerFactory.getLogger(StorageUsageRecordAggregationStrategyTest.class); + + @Test + public void secondAsNotAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions { + + StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecord(); + storageUsageRecord.validate(); + logger.debug("StorageUsageRecord : {}", storageUsageRecord); + + org.gcube.accounting.datamodel.aggregation.StorageUsageRecord aggregated = + new org.gcube.accounting.datamodel.aggregation.StorageUsageRecord(storageUsageRecord); + logger.debug("StorageUsageRecord Converted to Aggregated: {}", aggregated); + aggregated.validate(); + + StorageUsageRecord storageUsageRecord2 = TestUsageRecord.createTestStorageUsageRecord(); + storageUsageRecord2.validate(); + logger.debug("StorageUsageRecord 2 : {}", storageUsageRecord2); + StorageUsageRecordAggregationStrategy suras = new StorageUsageRecordAggregationStrategy(aggregated); + + long firstDataVolume = storageUsageRecord.getDataVolume(); + long secondDataVolume = storageUsageRecord2.getDataVolume(); + + suras.aggregate(storageUsageRecord2); + logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated); + aggregated.validate(); + + Assert.assertTrue(aggregated.getDataVolume() == ((firstDataVolume + secondDataVolume)/2)); + Assert.assertTrue(aggregated.getOperationCount() == 2); + + } + + @Test + public void secondAsAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions { + + StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecord(); + storageUsageRecord.validate(); + logger.debug("StorageUsageRecord : {}", storageUsageRecord); + + org.gcube.accounting.datamodel.aggregation.StorageUsageRecord aggregated = + new org.gcube.accounting.datamodel.aggregation.StorageUsageRecord(storageUsageRecord); + logger.debug("StorageUsageRecord Converted to Aggregated: {}", aggregated); + aggregated.validate(); + + StorageUsageRecord storageUsageRecord2 = TestUsageRecord.createTestStorageUsageRecord(); + storageUsageRecord2.validate(); + logger.debug("StorageUsageRecord 2 : {}", storageUsageRecord2); + org.gcube.accounting.datamodel.aggregation.StorageUsageRecord converted = + new org.gcube.accounting.datamodel.aggregation.StorageUsageRecord(storageUsageRecord2); + logger.debug("StorageUsageRecord Converted to Aggregated: {}", converted); + converted.validate(); + + + StorageUsageRecordAggregationStrategy suras = new StorageUsageRecordAggregationStrategy(aggregated); + + long firstDataVolume = aggregated.getDataVolume(); + long secondDataVolume = converted.getDataVolume(); + + suras.aggregate(converted); + logger.debug("Resulting Aggregated StorageUsageRecord: {}", aggregated); + aggregated.validate(); + + Assert.assertTrue(aggregated.getDataVolume() == ((firstDataVolume + secondDataVolume)/2)); + Assert.assertTrue(aggregated.getOperationCount() == 2); + + } + + @Test + public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions { + + StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecord(); + storageUsageRecord.validate(); + logger.debug("StorageUsageRecord : {}", storageUsageRecord); + + org.gcube.accounting.datamodel.aggregation.StorageUsageRecord aggregated = + new org.gcube.accounting.datamodel.aggregation.StorageUsageRecord(storageUsageRecord); + logger.debug("StorageUsageRecord Converted to Aggregated: {}", aggregated); + aggregated.validate(); + + StorageUsageRecordAggregationStrategy suras = new StorageUsageRecordAggregationStrategy(aggregated); + + for(int i=2; i<1002; i++){ + + StorageUsageRecord sur = TestUsageRecord.createTestStorageUsageRecord(); + sur.validate(); + logger.debug("Cycle StorageUsageRecord {}: {}", i, sur); + + long oldDataVolume = aggregated.getDataVolume(); + long newDataVolume = sur.getDataVolume(); + + suras.aggregate(sur); + logger.debug("Resulting Aggregated StorageUsageRecord : {}", aggregated); + aggregated.validate(); + + Assert.assertTrue(aggregated.getDataVolume() == ((oldDataVolume + newDataVolume)/2)); + Assert.assertTrue(aggregated.getOperationCount() == i); + + } + + logger.debug("Resulting Aggregated StorageUsageRecord: {}", aggregated); + } + +}