/** * */ package org.gcube.accounting.aggregator.plugin; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import org.gcube.accounting.aggregator.aggregation.AggregationType; import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPlugin; import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPlugin.ElaborationType; import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPluginDeclaration; import org.gcube.accounting.aggregator.utility.Utility; import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; import org.gcube.testutility.ScopedTest; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin; import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.quartz.CronExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AggregatorAccountingPluginSmartExecutorSchedulerTest extends ScopedTest { private static Logger logger = LoggerFactory.getLogger(AggregatorAccountingPluginSmartExecutorSchedulerTest.class); private SmartExecutorProxy proxy; @Before public void before() throws Exception { proxy = ExecutorPlugin.getExecutorProxy(AccountingAggregatorPluginDeclaration.NAME).build(); Assert.assertNotNull(proxy); } private String getPersistEndTimeParameter() { Calendar persistEndTime = Calendar.getInstance(); persistEndTime.set(Calendar.HOUR_OF_DAY, 18); persistEndTime.set(Calendar.MINUTE, 00); String persistEndTimeParameter = AccountingAggregatorPlugin.RECOVERY_END_TIME_DATE_FORMAT .format(persistEndTime.getTime()); return persistEndTimeParameter; } private Map getRecoveryInputs() throws Exception { Map inputs = new HashMap(); inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.RECOVERY.name()); String persistEndTimeParameter = getPersistEndTimeParameter(); inputs.put(AccountingAggregatorPlugin.PERSIST_END_TIME_INPUT_PARAMETER, persistEndTimeParameter); return inputs; } private Map getAggregateInputs() throws Exception { Map inputs = new HashMap(); inputs.put(AccountingAggregatorPlugin.AGGREGATION_TYPE_INPUT_PARAMETER, AggregationType.DAILY.name()); inputs.put(AccountingAggregatorPlugin.ELABORATION_TYPE_INPUT_PARAMETER, ElaborationType.AGGREGATE.name()); String persistEndTimeParameter = getPersistEndTimeParameter(); inputs.put(AccountingAggregatorPlugin.PERSIST_END_TIME_INPUT_PARAMETER, persistEndTimeParameter); inputs.put(AccountingAggregatorPlugin.RECORD_TYPE_INPUT_PARAMETER, ServiceUsageRecord.class.newInstance().getRecordType()); // Start Aggregation Date Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 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.RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER, true); return inputs; } private void launch(Scheduling scheduling, Map inputs) throws Exception { LaunchParameter launchParameter = new LaunchParameter(AccountingAggregatorPluginDeclaration.NAME, inputs); launchParameter.setScheduling(scheduling); try { String uuidString = proxy.launch(launchParameter); logger.debug("Launched with UUID : {}", uuidString); } catch (Exception e) { logger.error("Error while launching {}", e); throw e; } } @Test public void cronExpPreviousMustBeTerminated() throws Exception { // Every 15 minutes CronExpression cronExpression = new CronExpression("0 0/15 * 1/1 * ? *"); Scheduling scheduling = new Scheduling(cronExpression, true); scheduling.setGlobal(true); Map inputs = getAggregateInputs(); launch(scheduling, inputs); } @Test public void recovery() throws Exception { // Every Day at 8:00 CronExpression cronExpression = new CronExpression("0 0 8 1/1 * ? *"); Scheduling scheduling = new Scheduling(cronExpression, true); scheduling.setGlobal(true); Map inputs = getRecoveryInputs(); launch(scheduling, inputs); } @Test public void unSchedule() throws Exception { // proxy.unSchedule("", true); } @Test public void stop() throws Exception { proxy.stop(AccountingAggregatorPluginDeclaration.NAME); } }