2015-06-25 14:30:50 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.datamodel.aggreagtion.aggregationstrategy;
|
|
|
|
|
|
|
|
import org.gcube.accounting.datamodel.TestUsageRecord;
|
|
|
|
import org.gcube.accounting.datamodel.aggregation.aggregationstrategy.ServiceUsageRecordAggregationStrategy;
|
|
|
|
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
|
|
|
|
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 ServiceUsageRecordAggregationStrategyTest {
|
|
|
|
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ServiceUsageRecordAggregationStrategyTest.class);
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void secondAsNotAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
serviceUsageRecord.validate();
|
|
|
|
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
|
|
|
|
|
|
|
|
org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord aggregated =
|
|
|
|
new org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord(serviceUsageRecord);
|
|
|
|
logger.debug("ServiceUsageRecord Converted to Aggregated: {}", aggregated);
|
|
|
|
aggregated.validate();
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord serviceUsageRecord2 = TestUsageRecord.createTestServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
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.getInvocationCount() == 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void secondAsAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
serviceUsageRecord.validate();
|
|
|
|
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
|
|
|
|
|
|
|
|
org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord aggregated =
|
|
|
|
new org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord(serviceUsageRecord);
|
|
|
|
logger.debug("ServiceUsageRecord Converted to Aggregated: {}", aggregated);
|
|
|
|
aggregated.validate();
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord serviceUsageRecord2 = TestUsageRecord.createTestServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
serviceUsageRecord2.validate();
|
|
|
|
logger.debug("ServiceUsageRecord 2 : {}", serviceUsageRecord2);
|
|
|
|
org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord converted2 =
|
|
|
|
new org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord(serviceUsageRecord2);
|
|
|
|
logger.debug("ServiceUsageRecord 2 Converted to Aggregated: {}", converted2);
|
|
|
|
converted2.validate();
|
|
|
|
|
|
|
|
ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated);
|
|
|
|
|
|
|
|
long firstduration = aggregated.getDuration();
|
|
|
|
long secondDuration = converted2.getDuration();
|
|
|
|
|
|
|
|
suras.aggregate(converted2);
|
|
|
|
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
|
|
|
|
aggregated.validate();
|
|
|
|
|
|
|
|
Assert.assertTrue(aggregated.getDuration() == ((firstduration + secondDuration)/2));
|
|
|
|
Assert.assertTrue(aggregated.getInvocationCount() == 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
serviceUsageRecord.validate();
|
|
|
|
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
|
|
|
|
|
|
|
|
org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord aggregated =
|
|
|
|
new org.gcube.accounting.datamodel.aggregation.ServiceUsageRecord(serviceUsageRecord);
|
|
|
|
logger.debug("ServiceUsageRecord Converted to Aggregated: {}", aggregated);
|
|
|
|
aggregated.validate();
|
|
|
|
|
|
|
|
ServiceUsageRecordAggregationStrategy suras = new ServiceUsageRecordAggregationStrategy(aggregated);
|
|
|
|
|
|
|
|
for(int i=2; i<1002; i++){
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord sur = TestUsageRecord.createTestServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
sur.validate();
|
|
|
|
logger.debug("Cycle ServiceUsageRecord {}: {}", i, sur);
|
|
|
|
|
|
|
|
|
|
|
|
long minInvocationTime = aggregated.getMinInvocationTime();
|
|
|
|
long maxInvocationTime = aggregated.getMaxInvocationTime();
|
|
|
|
long oldDuration = aggregated.getDuration();
|
|
|
|
long newDuration = sur.getDuration();
|
|
|
|
|
|
|
|
suras.aggregate(sur);
|
|
|
|
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
|
|
|
|
aggregated.validate();
|
|
|
|
|
|
|
|
Assert.assertTrue(aggregated.getDuration() == ((oldDuration + newDuration)/2));
|
|
|
|
Assert.assertTrue(aggregated.getInvocationCount() == i);
|
|
|
|
|
|
|
|
if(minInvocationTime >= newDuration){
|
|
|
|
Assert.assertTrue(aggregated.getMinInvocationTime() == newDuration);
|
|
|
|
}else{
|
|
|
|
Assert.assertTrue(aggregated.getMinInvocationTime() == minInvocationTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(maxInvocationTime >= newDuration){
|
|
|
|
Assert.assertTrue(aggregated.getMaxInvocationTime() == maxInvocationTime);
|
|
|
|
}else{
|
|
|
|
Assert.assertTrue(aggregated.getMaxInvocationTime() == newDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|