2017-09-05 17:13:50 +02:00
|
|
|
package org.gcube.accounting.aggregator.plugin;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.HashMap;
|
2021-11-11 14:53:03 +01:00
|
|
|
import java.util.HashSet;
|
2017-09-05 17:13:50 +02:00
|
|
|
import java.util.Map;
|
2021-11-11 14:53:03 +01:00
|
|
|
import java.util.Set;
|
2021-04-08 11:30:13 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-09-05 17:13:50 +02:00
|
|
|
|
2021-11-05 14:25:45 +01:00
|
|
|
import org.gcube.accounting.aggregator.ContextTest;
|
2017-09-05 17:13:50 +02:00
|
|
|
import org.gcube.accounting.aggregator.aggregation.AggregationType;
|
|
|
|
import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPlugin.ElaborationType;
|
|
|
|
import org.gcube.accounting.aggregator.utility.Utility;
|
2021-04-08 11:30:13 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
|
2018-02-15 15:00:49 +01:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
2021-04-15 14:58:12 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
2021-04-08 11:30:13 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
2021-11-08 14:57:35 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
|
2017-10-10 12:25:13 +02:00
|
|
|
import org.junit.Test;
|
2017-09-05 17:13:50 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-03-11 15:33:39 +01:00
|
|
|
public class AccountingAggregatorPluginTest extends ContextTest {
|
2017-09-05 17:13:50 +02:00
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(AccountingAggregatorPluginTest.class);
|
|
|
|
|
2020-04-17 17:34:13 +02:00
|
|
|
public static final String ROOT_PROD = "/d4science.research-infrastructures.eu";
|
2021-11-11 14:53:03 +01:00
|
|
|
|
|
|
|
private void aggregate(String recordType, AggregationType aggregationType, Calendar aggregationStartCalendar,
|
|
|
|
Calendar aggregationEndCalendar) throws Exception {
|
|
|
|
Map<String, Object> inputs = new HashMap<String, Object>();
|
|
|
|
|
2021-04-08 11:30:13 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, aggregationType.name());
|
2021-11-11 14:53:03 +01:00
|
|
|
|
2021-04-08 11:30:13 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.AGGREGATE.name());
|
2021-11-11 14:53:03 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-08 11:30:13 +02:00
|
|
|
AccountingAggregatorPlugin plugin = new AccountingAggregatorPlugin();
|
|
|
|
logger.debug("Going to launch {} with inputs {}", plugin.getName(), inputs);
|
2021-11-11 14:53:03 +01:00
|
|
|
|
|
|
|
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 {
|
2021-04-08 11:30:13 +02:00
|
|
|
plugin.launch(inputs);
|
|
|
|
}
|
2021-11-11 14:53:03 +01:00
|
|
|
|
2021-04-08 11:30:13 +02:00
|
|
|
}
|
2021-11-11 14:53:03 +01:00
|
|
|
|
2021-11-08 14:57:35 +01:00
|
|
|
@JsonIgnore
|
2021-04-15 14:58:12 +02:00
|
|
|
@Test
|
2021-11-11 14:53:03 +01:00
|
|
|
public void aggregateJobUsageRecord() throws Exception {
|
2021-11-08 14:57:35 +01:00
|
|
|
ContextTest.setContextByName(ROOT_DEV_SCOPE);
|
|
|
|
// ContextTest.setContextByName(ROOT_PROD);
|
2021-11-11 14:53:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-04-15 14:58:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 14:53:03 +01:00
|
|
|
|
2021-11-08 14:57:35 +01:00
|
|
|
@JsonIgnore
|
2021-04-08 11:30:13 +02:00
|
|
|
@Test
|
2021-11-11 14:53:03 +01:00
|
|
|
public void aggregateStorageStatusRecord() throws Exception {
|
2021-11-08 14:57:35 +01:00
|
|
|
ContextTest.setContextByName(ROOT_DEV_SCOPE);
|
|
|
|
// ContextTest.setContextByName(ROOT_PROD);
|
2021-11-11 14:53:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-04-08 11:30:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 14:53:03 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:37:56 +01:00
|
|
|
@JsonIgnore
|
2021-11-08 14:57:35 +01:00
|
|
|
@Test
|
|
|
|
public void aggregateService() throws Exception {
|
|
|
|
ContextTest.setContextByName(ROOT_DEV_SCOPE);
|
2021-11-11 14:53:03 +01:00
|
|
|
// 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);
|
|
|
|
}
|
2020-03-11 15:33:39 +01:00
|
|
|
}
|
2017-09-05 17:13:50 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 14:57:35 +01:00
|
|
|
@JsonIgnore
|
2020-03-16 15:12:34 +01:00
|
|
|
@Test
|
2021-11-11 14:53:03 +01:00
|
|
|
public void aggregateAll() throws Exception {
|
2020-04-17 17:34:13 +02:00
|
|
|
ContextTest.setContextByName(ROOT_DEV_SCOPE);
|
2020-03-11 15:33:39 +01:00
|
|
|
// ContextTest.setContextByName(ROOT_PROD);
|
2021-11-11 14:53:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 15:33:39 +01:00
|
|
|
|
2021-11-11 14:53:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
2017-09-05 17:13:50 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.RECOVERY.name());
|
2021-11-11 14:53:03 +01:00
|
|
|
inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, aggregationType.name());
|
|
|
|
|
2021-03-25 12:38:28 +01:00
|
|
|
|
2021-11-11 14:53:03 +01:00
|
|
|
// 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);
|
|
|
|
|
2017-10-10 12:25:13 +02:00
|
|
|
|
2020-04-17 17:34:13 +02:00
|
|
|
// Calendar aggregationEndCalendar = Utility.getEndCalendarFromStartCalendar(AggregationType.MONTHLY, aggregationStartCalendar, 1);
|
2021-11-11 14:53:03 +01:00
|
|
|
// 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);
|
2021-04-08 11:30:13 +02:00
|
|
|
|
2020-09-30 12:12:22 +02:00
|
|
|
AccountingAggregatorPlugin plugin = new AccountingAggregatorPlugin();
|
2021-11-11 14:53:03 +01:00
|
|
|
logger.debug("Going to launch {} with inputs {}", plugin.getName(), inputs);
|
|
|
|
|
2017-09-05 17:13:50 +02:00
|
|
|
plugin.launch(inputs);
|
2021-11-11 14:53:03 +01:00
|
|
|
|
2017-09-05 17:13:50 +02:00
|
|
|
|
2021-11-11 14:53:03 +01:00
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
|
2017-09-05 17:13:50 +02:00
|
|
|
}
|
2021-11-11 14:53:03 +01:00
|
|
|
|
2017-09-05 17:13:50 +02:00
|
|
|
}
|