You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
10 KiB
Java

package org.gcube.accounting.aggregator.plugin;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.aggregator.ContextTest;
import org.gcube.accounting.aggregator.aggregation.AggregationType;
import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPlugin.ElaborationType;
import org.gcube.accounting.aggregator.utility.Utility;
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AccountingAggregatorPluginTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(AccountingAggregatorPluginTest.class);
public static final String ROOT_PROD = "/d4science.research-infrastructures.eu";
private void aggregate(String recordType, AggregationType aggregationType, Calendar aggregationStartCalendar,
Calendar aggregationEndCalendar) throws Exception {
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, aggregationType.name());
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.AGGREGATE.name());
inputs.put(AccountingAggregatorPlugin.RECORD_TYPE_INPUT_PARAMETER, recordType);
inputs.put(AccountingAggregatorPlugin.RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER,
aggregationStartCalendar == null);
inputs.put(AccountingAggregatorPlugin.FORCE_EARLY_AGGREGATION, false);
inputs.put(AccountingAggregatorPlugin.FORCE_RERUN, false);
inputs.put(AccountingAggregatorPlugin.FORCE_RESTART, false);
if (aggregationStartCalendar != null) {
String aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT
.format(aggregationStartCalendar.getTime());
logger.trace("{} : {}", AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER,
aggregationStartDate);
inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
}
if (aggregationStartCalendar != null && aggregationEndCalendar != null) {
String aggregationEndDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT
.format(aggregationEndCalendar.getTime());
logger.trace("{} : {}", AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER,
aggregationEndDate);
inputs.put(AccountingAggregatorPlugin.AGGREGATION_END_DATE_INPUT_PARAMETER, aggregationEndDate);
}
AccountingAggregatorPlugin plugin = new AccountingAggregatorPlugin();
logger.debug("Going to launch {} with inputs {}", plugin.getName(), inputs);
if (aggregationStartCalendar != null && aggregationEndCalendar != null) {
while (aggregationStartCalendar.before(aggregationEndCalendar)) {
plugin.launch(inputs);
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
plugin = new AccountingAggregatorPlugin();
aggregationStartCalendar.add(aggregationType.getCalendarField(), 1);
String aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT
.format(aggregationStartCalendar.getTime());
inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
}
} else {
plugin.launch(inputs);
}
}
@JsonIgnore
@Test
public void aggregateJobUsageRecord() throws Exception {
ContextTest.setContextByName(ROOT_DEV_SCOPE);
// ContextTest.setContextByName(ROOT_PROD);
String recordType = JobUsageRecord.class.newInstance().getRecordType();
boolean allAgregationTypes = true;
if (!allAgregationTypes) {
AggregationType aggregationType = AggregationType.DAILY;
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 10);
// Calendar aggregationEndCalendar =
// Utility.getEndCalendarFromStartCalendar(aggregationType,
// aggregationStartCalendar, 1);
Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 11);
aggregate(recordType, aggregationType, aggregationStartCalendar, aggregationEndCalendar);
} else {
for (AggregationType at : AggregationType.values()) {
aggregate(recordType, at, null, null);
}
}
}
@JsonIgnore
@Test
public void aggregateStorageStatusRecord() throws Exception {
ContextTest.setContextByName(ROOT_DEV_SCOPE);
// ContextTest.setContextByName(ROOT_PROD);
String recordType = StorageStatusRecord.class.newInstance().getRecordType();
boolean allAgregationTypes = false;
if (!allAgregationTypes) {
AggregationType aggregationType = AggregationType.DAILY;
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 10);
// Calendar aggregationEndCalendar =
// Utility.getEndCalendarFromStartCalendar(aggregationType,
// aggregationStartCalendar, 1);
Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 11);
aggregate(recordType, aggregationType, aggregationStartCalendar, aggregationEndCalendar);
} else {
for (AggregationType at : AggregationType.values()) {
aggregate(recordType, at, null, null);
}
}
}
@JsonIgnore
@Test
public void aggregateStorageUsageRecord() throws Exception {
ContextTest.setContextByName(ROOT_DEV_SCOPE);
// ContextTest.setContextByName(ROOT_PROD);
String recordType = StorageUsageRecord.class.newInstance().getRecordType();
boolean allAgregationTypes = false;
if (!allAgregationTypes) {
AggregationType aggregationType = AggregationType.DAILY;
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 10);
// Calendar aggregationEndCalendar =
// Utility.getEndCalendarFromStartCalendar(aggregationType,
// aggregationStartCalendar, 1);
Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 11);
aggregate(recordType, aggregationType, aggregationStartCalendar, aggregationEndCalendar);
} else {
for (AggregationType at : AggregationType.values()) {
aggregate(recordType, at, null, null);
}
}
}
@JsonIgnore
@Test
public void aggregateService() throws Exception {
ContextTest.setContextByName(ROOT_DEV_SCOPE);
// ContextTest.setContextByName(ROOT_PROD);
String recordType = ServiceUsageRecord.class.newInstance().getRecordType();
boolean allAgregationTypes = true;
if (!allAgregationTypes) {
AggregationType aggregationType = AggregationType.DAILY;
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 10);
// Calendar aggregationEndCalendar =
// Utility.getEndCalendarFromStartCalendar(aggregationType,
// aggregationStartCalendar, 1);
Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2021, Calendar.NOVEMBER, 11);
aggregate(recordType, aggregationType, aggregationStartCalendar, aggregationEndCalendar);
} else {
for (AggregationType at : AggregationType.values()) {
aggregate(recordType, at, null, null);
}
}
}
@JsonIgnore
@Test
public void aggregateAll() throws Exception {
ContextTest.setContextByName(ROOT_DEV_SCOPE);
// ContextTest.setContextByName(ROOT_PROD);
Set<String> allRecordTypes = new HashSet<>();
allRecordTypes.add(ServiceUsageRecord.class.newInstance().getRecordType());
allRecordTypes.add(JobUsageRecord.class.newInstance().getRecordType());
allRecordTypes.add(StorageUsageRecord.class.newInstance().getRecordType());
allRecordTypes.add(StorageStatusRecord.class.newInstance().getRecordType());
for (AggregationType at : AggregationType.values()) {
for(String recordType : allRecordTypes) {
aggregate(recordType, at, null, null);
}
}
}
@JsonIgnore
@Test
public void testRecovery() throws Exception {
ContextTest.setContextByName(ROOT_DEV_SCOPE);
// ContextTest.setContextByName(ROOT_PROD);
Map<String, Object> inputs = new HashMap<String, Object>();
AggregationType aggregationType = AggregationType.DAILY;
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.RECOVERY.name());
inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, aggregationType.name());
// Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2017, Calendar.MAY, 1);
// String aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(aggregationStartCalendar.getTime());
// logger.trace("{} : {}",AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
// inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
inputs.put(AccountingAggregatorPlugin.FORCE_RESTART, false);
// Calendar aggregationEndCalendar = Utility.getEndCalendarFromStartCalendar(AggregationType.MONTHLY, aggregationStartCalendar, 1);
// Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 1);
// String aggregationEndDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(aggregationEndCalendar.getTime());
// logger.trace("{} : {}", AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationEndDate);
// inputs.put(AccountingAggregatorPlugin.AGGREGATION_END_DATE_INPUT_PARAMETER, aggregationEndDate);
AccountingAggregatorPlugin plugin = new AccountingAggregatorPlugin();
logger.debug("Going to launch {} with inputs {}", plugin.getName(), inputs);
plugin.launch(inputs);
// while(aggregationStartCalendar.before(aggregationEndCalendar)) {
// plugin.launch(inputs); aggregationStartCalendar.add(Calendar.MONTH, 1);
// aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(aggregationStartCalendar.getTime());
// inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
// }
}
}