accounting-lib/src/test/java/org/gcube/accounting/aggregation/strategy/ServiceUsageRecordAggregati...

168 lines
7.2 KiB
Java

/**
*
*/
package org.gcube.accounting.aggregation.strategy;
import org.gcube.accounting.aggregation.strategy.ServiceUsageRecordAggregationStrategy;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
import org.gcube.common.scope.api.ScopeProvider;
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 ServiceUsageRecordAggregationStrategyTest {
private static Logger logger = LoggerFactory.getLogger(ServiceUsageRecordAggregationStrategyTest.class);
@Test
public void secondAsNotAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
serviceUsageRecord.validate();
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
org.gcube.accounting.aggregation.ServiceUsageRecord aggregated =
new org.gcube.accounting.aggregation.ServiceUsageRecord(serviceUsageRecord);
logger.debug("ServiceUsageRecord Converted to Aggregated: {}", aggregated);
aggregated.validate();
ServiceUsageRecord serviceUsageRecord2 = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord2.validate();
logger.debug("ServiceUsageRecord 2 : {}", serviceUsageRecord2);
ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated);
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.getOperationCount() == 2);
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.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
}
@Test
public void secondAsAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord.validate();
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
org.gcube.accounting.aggregation.ServiceUsageRecord aggregated =
new org.gcube.accounting.aggregation.ServiceUsageRecord(serviceUsageRecord);
logger.debug("ServiceUsageRecord Converted to Aggregated: {}", aggregated);
aggregated.validate();
ServiceUsageRecord serviceUsageRecord2 = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord2.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
serviceUsageRecord2.validate();
logger.debug("ServiceUsageRecord 2 : {}", serviceUsageRecord2);
org.gcube.accounting.aggregation.ServiceUsageRecord converted =
new org.gcube.accounting.aggregation.ServiceUsageRecord(serviceUsageRecord2);
logger.debug("ServiceUsageRecord 2 Converted to Aggregated: {}", converted);
converted.validate();
ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated);
long firstduration = aggregated.getDuration();
long secondDuration = converted.getDuration();
suras.aggregate(converted);
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
aggregated.validate();
Assert.assertTrue(aggregated.getDuration() == ((firstduration + secondDuration)/2));
Assert.assertTrue(aggregated.getOperationCount() == 2);
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.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
}
protected long durationWeightedAverage(int numberA, long durationA, int numberB, long durationB){
return ((numberA * durationA) + (numberB * durationB)) / (numberA + numberB);
}
@Test
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
serviceUsageRecord.validate();
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
org.gcube.accounting.aggregation.ServiceUsageRecord aggregated =
new org.gcube.accounting.aggregation.ServiceUsageRecord(serviceUsageRecord);
logger.debug("ServiceUsageRecord Converted to Aggregated: {}", aggregated);
aggregated.validate();
ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated);
for(int i=2; i<1002; i++){
ServiceUsageRecord sur = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
sur.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
sur.validate();
logger.debug("Cycle ServiceUsageRecord {}: {}", i, sur);
long minInvocationTime = aggregated.getMinInvocationTime();
long maxInvocationTime = aggregated.getMaxInvocationTime();
long oldDuration = aggregated.getDuration();
long surDuration = sur.getDuration();
suras.aggregate(sur);
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
aggregated.validate();
long avgDuration = durationWeightedAverage(i-1, oldDuration, 1, surDuration);
Assert.assertTrue(aggregated.getDuration() == (avgDuration));
Assert.assertTrue(aggregated.getOperationCount() == i);
if(minInvocationTime >= surDuration){
Assert.assertTrue(aggregated.getMinInvocationTime() == surDuration);
}else{
Assert.assertTrue(aggregated.getMinInvocationTime() == minInvocationTime);
}
if(maxInvocationTime >= surDuration){
Assert.assertTrue(aggregated.getMaxInvocationTime() == maxInvocationTime);
}else{
Assert.assertTrue(aggregated.getMaxInvocationTime() == surDuration);
}
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
}
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
}
}