accounting-lib/src/test/java/org/gcube/documentstore/records/aggregation/AggregationSchedulerTest.java

123 lines
4.1 KiB
Java

/**
*
*/
package org.gcube.documentstore.records.aggregation;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.testutility.StressTestUtility;
import org.gcube.accounting.testutility.TestOperation;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.documentstore.persistence.PersistenceExecutor;
import org.gcube.documentstore.records.Record;
import org.gcube.documentstore.records.aggregation.AggregationScheduler;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AggregationSchedulerTest {
private static final Logger logger = LoggerFactory.getLogger(AggregationSchedulerTest.class);
public static AggregationScheduler getAggregationScheduler(){
return AggregationScheduler.newInstance();
}
public static PersistenceExecutor persistenceExecutor = new PersistenceExecutor(){
@Override
public void persist(Record... records) throws Exception {
for(Record record : records){
logger.debug("Storing : {}", record.toString());
}
}
};
@Test
public void stressTestAggregableURSingleType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
aggregationScheduler.flush(persistenceExecutor);
}
public static final String ALTERNATIVE_SERVICE_CLASS = "AlternativeServiceClass";
@Test
public void stressTestDifferentAggregableURSingleType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
if(i%2==0){
usageRecord.setServiceClass(ALTERNATIVE_SERVICE_CLASS);
}
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
aggregationScheduler.flush(persistenceExecutor);
}
@Test
public void stressTestDifferentAggregableURTwoType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord;
if(i%2==0){
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
}else{
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
}
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
aggregationScheduler.flush(persistenceExecutor);
}
@Test
public void stressTestDifferentAggregableURMultipleType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord;
switch (i%3) {
case 0:
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
break;
case 1:
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
break;
case 2:
usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
break;
default:
usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
}
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
aggregationScheduler.flush(persistenceExecutor);
}
}