2017-07-07 12:51:20 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-08-03 15:13:57 +02:00
|
|
|
package org.gcube.accounting.aggregator.plugin;
|
2017-07-07 12:51:20 +02:00
|
|
|
|
2017-08-01 15:56:08 +02:00
|
|
|
import java.util.Calendar;
|
2017-07-07 12:51:20 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2017-08-01 15:56:08 +02:00
|
|
|
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.ServiceUsageRecord;
|
2017-10-10 16:36:33 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
2019-02-28 16:28:11 +01:00
|
|
|
import org.gcube.testutility.ContextTest;
|
2017-07-07 12:51:20 +02:00
|
|
|
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
|
|
|
import org.gcube.vremanagement.executor.api.types.Scheduling;
|
2019-02-28 16:28:11 +01:00
|
|
|
import org.gcube.vremanagement.executor.client.SmartExecutorClient;
|
|
|
|
import org.gcube.vremanagement.executor.client.SmartExecutorClientFactory;
|
|
|
|
import org.gcube.vremanagement.executor.json.SEMapper;
|
2017-07-07 12:51:20 +02:00
|
|
|
import org.junit.Assert;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.quartz.CronExpression;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2019-02-28 16:28:11 +01:00
|
|
|
public class AggregatorAccountingPluginSmartExecutorSchedulerTest extends ContextTest {
|
2017-07-07 12:51:20 +02:00
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(AggregatorAccountingPluginSmartExecutorSchedulerTest.class);
|
|
|
|
|
2019-02-28 16:28:11 +01:00
|
|
|
private SmartExecutorClient smartExecutorClient;
|
2017-08-01 15:56:08 +02:00
|
|
|
|
2017-07-07 12:51:20 +02:00
|
|
|
@Before
|
2017-08-01 15:56:08 +02:00
|
|
|
public void before() throws Exception {
|
2019-02-28 16:28:11 +01:00
|
|
|
setContext("/gcube");
|
|
|
|
smartExecutorClient = SmartExecutorClientFactory.create(AccountingAggregatorPluginDeclaration.NAME);
|
|
|
|
Assert.assertNotNull(smartExecutorClient);
|
2017-07-07 12:51:20 +02:00
|
|
|
|
|
|
|
}
|
2017-10-10 16:36:33 +02:00
|
|
|
|
|
|
|
private void launch(Scheduling scheduling, Map<String, Object> inputs) throws Exception {
|
2017-07-07 12:51:20 +02:00
|
|
|
|
2017-10-10 16:36:33 +02:00
|
|
|
LaunchParameter launchParameter = new LaunchParameter(AccountingAggregatorPluginDeclaration.NAME, inputs);
|
|
|
|
launchParameter.setScheduling(scheduling);
|
2017-08-01 15:56:08 +02:00
|
|
|
|
2017-10-10 16:36:33 +02:00
|
|
|
try {
|
2019-02-28 16:28:11 +01:00
|
|
|
String uuidString = smartExecutorClient.launch(SEMapper.marshal(launchParameter));
|
2017-10-10 16:36:33 +02:00
|
|
|
logger.debug("Launched with UUID : {}", uuidString);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error while launching {}", e);
|
|
|
|
throw e;
|
|
|
|
}
|
2017-07-07 12:51:20 +02:00
|
|
|
}
|
2017-10-10 16:36:33 +02:00
|
|
|
|
2017-08-01 15:56:08 +02:00
|
|
|
private Map<String, Object> getAggregateInputs() throws Exception {
|
2017-07-07 12:51:20 +02:00
|
|
|
Map<String, Object> inputs = new HashMap<String, Object>();
|
2017-08-01 15:56:08 +02:00
|
|
|
|
2018-04-12 15:01:07 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, AggregationType.MONTHLY.name());
|
|
|
|
|
2017-08-01 15:56:08 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.AGGREGATE.name());
|
2018-04-12 15:01:07 +02:00
|
|
|
|
|
|
|
|
2017-09-22 10:47:37 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.PERSIST_START_TIME_INPUT_PARAMETER, Utility.getPersistTimeParameter(3, 0));
|
|
|
|
inputs.put(AccountingAggregatorPlugin.PERSIST_END_TIME_INPUT_PARAMETER, Utility.getPersistTimeParameter(19, 00));
|
2018-04-12 15:01:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
inputs.put(AccountingAggregatorPlugin.RECORD_TYPE_INPUT_PARAMETER, StorageUsageRecord.class.newInstance().getRecordType());
|
|
|
|
|
|
|
|
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2016, Calendar.FEBRUARY, 1);
|
2017-10-10 16:36:33 +02:00
|
|
|
String aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(aggregationStartCalendar.getTime());
|
|
|
|
logger.trace("{} : {}", AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
|
2017-08-01 15:56:08 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
|
2017-10-10 16:36:33 +02:00
|
|
|
|
2018-04-12 15:01:07 +02:00
|
|
|
inputs.put(AccountingAggregatorPlugin.RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER, true);
|
2017-08-01 15:56:08 +02:00
|
|
|
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
2017-07-07 12:51:20 +02:00
|
|
|
@Test
|
2017-09-04 10:54:10 +02:00
|
|
|
public void aggregate() throws Exception {
|
2017-09-22 10:47:37 +02:00
|
|
|
// Every 5 minutes
|
|
|
|
CronExpression cronExpression = new CronExpression("0 0/5 * 1/1 * ? *");
|
2017-09-04 15:43:32 +02:00
|
|
|
Scheduling scheduling = new Scheduling(cronExpression, true);
|
2017-08-04 11:09:55 +02:00
|
|
|
scheduling.setGlobal(false);
|
2017-08-01 15:56:08 +02:00
|
|
|
|
|
|
|
Map<String, Object> inputs = getAggregateInputs();
|
2017-09-22 10:47:37 +02:00
|
|
|
|
2017-09-04 15:43:32 +02:00
|
|
|
launch(null, inputs);
|
2017-08-01 15:56:08 +02:00
|
|
|
|
2017-07-07 12:51:20 +02:00
|
|
|
}
|
2017-10-10 16:36:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> getMonthlyAggregateInputs() throws Exception {
|
|
|
|
Map<String, Object> inputs = new HashMap<String, Object>();
|
|
|
|
inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, AggregationType.MONTHLY.name());
|
|
|
|
|
|
|
|
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.AGGREGATE.name());
|
|
|
|
|
|
|
|
inputs.put(AccountingAggregatorPlugin.PERSIST_START_TIME_INPUT_PARAMETER, Utility.getPersistTimeParameter(3, 0));
|
|
|
|
inputs.put(AccountingAggregatorPlugin.PERSIST_END_TIME_INPUT_PARAMETER, Utility.getPersistTimeParameter(19, 00));
|
|
|
|
|
|
|
|
inputs.put(AccountingAggregatorPlugin.RECORD_TYPE_INPUT_PARAMETER,
|
2018-11-30 14:50:11 +01:00
|
|
|
ServiceUsageRecord.class.newInstance().getRecordType());
|
2017-10-10 16:36:33 +02:00
|
|
|
|
2018-11-30 14:50:11 +01:00
|
|
|
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2017, Calendar.SEPTEMBER, 1);
|
2017-10-10 16:36:33 +02:00
|
|
|
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);
|
|
|
|
/*
|
|
|
|
Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 30);
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
|
|
|
|
inputs.put(AccountingAggregatorPlugin.RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER, true);
|
|
|
|
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
2018-11-30 14:50:11 +01:00
|
|
|
// @Test
|
2017-10-10 16:36:33 +02:00
|
|
|
public void aggregateMonthly() throws Exception {
|
2018-11-30 14:50:11 +01:00
|
|
|
CronExpression cronExpression = new CronExpression("0 0 10 1/1 * ? *");
|
2017-10-10 16:36:33 +02:00
|
|
|
Scheduling scheduling = new Scheduling(cronExpression, true);
|
|
|
|
scheduling.setGlobal(false);
|
|
|
|
|
2019-02-28 16:28:11 +01:00
|
|
|
ContextTest.setContextByName("/gcube");
|
2017-10-10 16:36:33 +02:00
|
|
|
Map<String, Object> inputs = getMonthlyAggregateInputs();
|
|
|
|
launch(scheduling, inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> getRecoveryInputs() throws Exception {
|
|
|
|
Map<String, Object> inputs = new HashMap<String, Object>();
|
|
|
|
inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.RECOVERY.name());
|
|
|
|
|
|
|
|
inputs.put(AccountingAggregatorPlugin.PERSIST_START_TIME_INPUT_PARAMETER, Utility.getPersistTimeParameter(3, 0));
|
|
|
|
inputs.put(AccountingAggregatorPlugin.PERSIST_END_TIME_INPUT_PARAMETER, Utility.getPersistTimeParameter(19, 0));
|
|
|
|
|
|
|
|
|
|
|
|
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2017, Calendar.AUGUST, 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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Calendar aggregationEndCalendar = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 22);
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
|
|
|
|
return inputs;
|
|
|
|
}
|
2017-07-07 12:51:20 +02:00
|
|
|
|
2018-11-30 14:50:11 +01:00
|
|
|
// @Test
|
2017-08-01 15:56:08 +02:00
|
|
|
public void recovery() throws Exception {
|
|
|
|
// Every Day at 8:00
|
2017-10-10 16:36:33 +02:00
|
|
|
CronExpression cronExpression = new CronExpression("0 0/5 3-19 1/1 * ? *");
|
2017-08-04 11:09:55 +02:00
|
|
|
Scheduling scheduling = new Scheduling(cronExpression, true);
|
|
|
|
scheduling.setGlobal(false);
|
2017-08-01 15:56:08 +02:00
|
|
|
|
|
|
|
Map<String, Object> inputs = getRecoveryInputs();
|
|
|
|
|
2017-10-10 16:36:33 +02:00
|
|
|
launch(scheduling, inputs);
|
2017-08-01 15:56:08 +02:00
|
|
|
|
|
|
|
}
|
2017-07-07 12:51:20 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void unSchedule() throws Exception {
|
2019-02-28 16:28:11 +01:00
|
|
|
smartExecutorClient.delete("d06fc539-af83-4dae-a274-c721b4710fcd", true);
|
2017-07-07 12:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void stop() throws Exception {
|
2019-02-28 16:28:11 +01:00
|
|
|
smartExecutorClient.delete("91178b6e-811f-410d-9cff-ef7b4a7a2475", true);
|
2017-07-07 12:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|