From 9752f74c95bf1a11e297c31cd98f290452c0189f Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 5 Nov 2021 14:39:18 +0100 Subject: [PATCH] Added test --- .../persistence/PostgreSQLConnectorTest.java | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 src/test/java/org/gcube/accounting/aggregator/persistence/PostgreSQLConnectorTest.java diff --git a/src/test/java/org/gcube/accounting/aggregator/persistence/PostgreSQLConnectorTest.java b/src/test/java/org/gcube/accounting/aggregator/persistence/PostgreSQLConnectorTest.java new file mode 100644 index 0000000..d15eb8d --- /dev/null +++ b/src/test/java/org/gcube/accounting/aggregator/persistence/PostgreSQLConnectorTest.java @@ -0,0 +1,155 @@ +package org.gcube.accounting.aggregator.persistence; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import org.gcube.accounting.aggregator.ContextTest; +import org.gcube.accounting.aggregator.aggregation.AggregationInfo; +import org.gcube.accounting.aggregator.aggregation.AggregationType; +import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPlugin; +import org.gcube.accounting.aggregator.status.AggregationState; +import org.gcube.accounting.aggregator.status.AggregationStatus; +import org.gcube.accounting.aggregator.utility.Utility; +import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; +import org.gcube.documentstore.records.DSMapper; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PostgreSQLConnectorTest extends ContextTest { + + private static Logger logger = LoggerFactory.getLogger(PostgreSQLConnectorTest.class); + + protected PostgreSQLConnector postgreSQLConnector; + + public PostgreSQLConnectorTest() throws Exception { + postgreSQLConnector = PostgreSQLConnector.getPostgreSQLConnector(); + } + + @Test + public void getLastTest() throws Exception { + AggregationStatus aggregationStatus = postgreSQLConnector.getLast(ServiceUsageRecord.class.getSimpleName(), AggregationType.DAILY, null, null); + logger.debug("Last : {}", DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + } + + @Test + public void getUnterminatedTest() throws Exception{ + List aggregationStatuses = postgreSQLConnector.getUnterminated(ServiceUsageRecord.class.getSimpleName(), AggregationType.DAILY, null, null); + for(AggregationStatus aggregationStatus : aggregationStatuses){ + logger.debug("Unterminated : {}", DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + } + } + + @Test + public void getLastTestWithConstraint() throws Exception { + Calendar aggregationStart = Utility.getAggregationStartCalendar(2017, Calendar.JANUARY, 1); + Calendar aggregationEnd = Utility.getAggregationStartCalendar(2017, Calendar.JANUARY, 31); + + AggregationStatus aggregationStatus = postgreSQLConnector.getLast(ServiceUsageRecord.class.getSimpleName(), AggregationType.DAILY, aggregationStart.getTime(), aggregationEnd.getTime()); + logger.info("Last : {}", DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + } + + @Test + public void getUnterminatedTestWithConstraint() throws Exception{ + Calendar aggregationStart = Utility.getAggregationStartCalendar(2017, Calendar.APRIL, 1); + Calendar aggregationEnd = Utility.getAggregationStartCalendar(2017, Calendar.APRIL, 30); + + List aggregationStatuses = postgreSQLConnector.getUnterminated(ServiceUsageRecord.class.getSimpleName(), AggregationType.DAILY, aggregationStart.getTime(), aggregationEnd.getTime()); + for(AggregationStatus aggregationStatus : aggregationStatuses){ + logger.info("Unterminated : {}", DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + } + } + + @Test + public void getAggregationStatusTest() throws Exception{ + Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 15); + AggregationStatus aggregationStatus = postgreSQLConnector.getAggregationStatus(ServiceUsageRecord.class.getSimpleName(), AggregationType.DAILY, aggregationStartCalendar.getTime()); + logger.debug("{}", DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + } + + @Test + public void aggregationStatusTest() throws Exception { + int toRemove = -36; + + Calendar today = Utility.getUTCCalendarInstance(); + today.add(Calendar.DAY_OF_YEAR, toRemove); + + String aggregationStartDateString = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(today.getTime()); + Date aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.parse(aggregationStartDateString); + + Calendar tomorrow = Utility.getUTCCalendarInstance(); + tomorrow.add(Calendar.DAY_OF_YEAR, toRemove+1); + String aggregationEndDateString = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(tomorrow.getTime()); + Date aggregationEndDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.parse(aggregationEndDateString); + + + AggregationInfo aggregation = new AggregationInfo(ServiceUsageRecord.class.newInstance().getRecordType(), AggregationType.DAILY, aggregationStartDate, aggregationEndDate); + String aggregationString = DSMapper.getObjectMapper().writeValueAsString(aggregation); + logger.debug("{} : {}", AggregationInfo.class.getSimpleName(), aggregationString); + + AggregationStatus aggregationStatus = new AggregationStatus(aggregation); + aggregationStatus.setContext("TEST_CONTEXT"); + + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + // Set to true just for one test and restore to false + boolean sync = true; + + Calendar startedStart = Utility.getUTCCalendarInstance(); + aggregationStatus.setAggregationState(AggregationState.STARTED, startedStart, sync); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + aggregationStatus.setRecordNumbers(100, 72, 0); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + Calendar aggregatedStart = Utility.getUTCCalendarInstance(); + aggregationStatus.setAggregationState(AggregationState.AGGREGATED, aggregatedStart, sync); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + Calendar addedStart = Utility.getUTCCalendarInstance(); + aggregationStatus.setAggregationState(AggregationState.ADDED, addedStart, sync); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + Calendar deletedStart = Utility.getUTCCalendarInstance(); + aggregationStatus.setAggregationState(AggregationState.DELETED, deletedStart, sync); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + Calendar completedStart = Utility.getUTCCalendarInstance(); + aggregationStatus.setAggregationState(AggregationState.COMPLETED, completedStart, sync); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + } + + @Test + public void createStartedElaboration() throws Exception { + + Calendar start = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 15); + String aggregationStartDateString = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(start.getTime()); + Date aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.parse(aggregationStartDateString); + + Calendar end = Utility.getUTCCalendarInstance(); + end.setTime(aggregationStartDate); + end.add(Calendar.DAY_OF_MONTH, 1); + String aggregationEndDateString = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(end.getTime()); + Date aggregationEndDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.parse(aggregationEndDateString); + + + AggregationInfo aggregation = new AggregationInfo(ServiceUsageRecord.class.newInstance().getRecordType(), AggregationType.DAILY, aggregationStartDate, aggregationEndDate); + String aggregationString = DSMapper.getObjectMapper().writeValueAsString(aggregation); + logger.debug("{} : {}", AggregationInfo.class.getSimpleName(), aggregationString); + + AggregationStatus aggregationStatus = new AggregationStatus(aggregation); + aggregationStatus.setContext("TEST_CONTEXT"); + + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + // Set to true just for one test and restore to false + boolean sync = true; + + Calendar startedStart = Utility.getUTCCalendarInstance(); + aggregationStatus.setAggregationState(AggregationState.STARTED, startedStart, sync); + logger.debug("{} : {}", AggregationStatus.class.getSimpleName(), DSMapper.getObjectMapper().writeValueAsString(aggregationStatus)); + + } +}