From d51d6c817586357a5d2a1803262a9584c5d921e8 Mon Sep 17 00:00:00 2001 From: alig Date: Fri, 12 Mar 2021 07:16:16 +0100 Subject: [PATCH] PP-839: Add unit test. Add Cleanup. --- dpppt-additionalinfo-backend/pom.xml | 27 +--------- .../backend/ws/config/SchedulerConfig.java | 21 ++++++++ .../backend/ws/data/HistoryDataService.java | 6 +++ .../ws/data/JdbcHistoryDataServiceImpl.java | 9 ++++ .../migration/pgsql_cluster/afterMigrate.sql | 12 +++++ .../backend/ws/HistoryDataServiceTest.java | 50 +++++++++++++++++++ .../backend/ws/config/PostgresDataConfig.java | 1 + 7 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 dpppt-additionalinfo-backend/src/main/resources/db/migration/pgsql_cluster/afterMigrate.sql create mode 100644 dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/HistoryDataServiceTest.java diff --git a/dpppt-additionalinfo-backend/pom.xml b/dpppt-additionalinfo-backend/pom.xml index 79f964a..db0d438 100644 --- a/dpppt-additionalinfo-backend/pom.xml +++ b/dpppt-additionalinfo-backend/pom.xml @@ -19,7 +19,7 @@ 11 UTF-8 - 2.2.6.RELEASE + 2.3.9.RELEASE 1.9.1 @@ -117,31 +117,6 @@ spring-security-web - - - io.jsonwebtoken - jjwt-api - 0.11.1 - - - io.jsonwebtoken - jjwt-root - 0.11.1 - pom - - - io.jsonwebtoken - jjwt-jackson - 0.11.1 - runtime - - - io.jsonwebtoken - jjwt-impl - 0.11.1 - runtime - - org.bouncycastle diff --git a/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/config/SchedulerConfig.java b/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/config/SchedulerConfig.java index 9d8bcbb..dfb9604 100644 --- a/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/config/SchedulerConfig.java +++ b/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/config/SchedulerConfig.java @@ -1,7 +1,11 @@ package org.dpppt.additionalinfo.backend.ws.config; +import java.time.LocalDate; import java.util.TimeZone; import org.dpppt.additionalinfo.backend.ws.controller.DppptAdditionalInfoController; +import org.dpppt.additionalinfo.backend.ws.data.HistoryDataService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @@ -15,6 +19,9 @@ public class SchedulerConfig implements SchedulingConfigurer { @Autowired DppptAdditionalInfoController dppptAdditionalInfoController; + @Autowired HistoryDataService historyDataService; + + protected final Logger logger = LoggerFactory.getLogger(getClass()); @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { @@ -29,5 +36,19 @@ public void run() { } }, new CronTrigger("0 0 * * * ?", TimeZone.getTimeZone("Europe/Zurich")))); + + // remove old data every night + taskRegistrar.addCronTask( + new CronTask( + new Runnable() { + + @Override + public void run() { + LocalDate day = LocalDate.now().minusDays(30); + logger.info("Remove history data before: " + day.toString()); + historyDataService.removeBefore(day); + } + }, + new CronTrigger("0 0 4 * * ?", TimeZone.getTimeZone("Europe/Zurich")))); } } diff --git a/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/HistoryDataService.java b/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/HistoryDataService.java index bd7ef46..31beb71 100644 --- a/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/HistoryDataService.java +++ b/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/HistoryDataService.java @@ -28,4 +28,10 @@ public interface HistoryDataService { * @return */ Integer findLatestSevenDayAvgForDay(LocalDate day); + + /** + * remote all data before the given day + * @param day + */ + void removeBefore(LocalDate day); } diff --git a/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/JdbcHistoryDataServiceImpl.java b/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/JdbcHistoryDataServiceImpl.java index 0c6bd43..db2a74d 100644 --- a/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/JdbcHistoryDataServiceImpl.java +++ b/dpppt-additionalinfo-backend/src/main/java/org/dpppt/additionalinfo/backend/ws/data/JdbcHistoryDataServiceImpl.java @@ -6,6 +6,7 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.transaction.annotation.Transactional; public class JdbcHistoryDataServiceImpl implements HistoryDataService { private final NamedParameterJdbcTemplate jt; @@ -20,6 +21,7 @@ public JdbcHistoryDataServiceImpl(DataSource dataSource) { } @Override + @Transactional(readOnly = false) public void upsertLatestSevenDayAvgForDay(Integer latestSevenDayAvg, LocalDate day) { MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("latest_seven_day_avg", latestSevenDayAvg); @@ -39,6 +41,7 @@ private void removeLatestSevenDayAvgForDay(LocalDate day, boolean deleteBefore) } @Override + @Transactional(readOnly = true) public Integer findLatestSevenDayAvgForDay(LocalDate day) { try { return jt.queryForObject( @@ -49,4 +52,10 @@ public Integer findLatestSevenDayAvgForDay(LocalDate day) { return null; } } + + @Override + @Transactional(readOnly = false) + public void removeBefore(LocalDate day) { + removeLatestSevenDayAvgForDay(day, true); + } } diff --git a/dpppt-additionalinfo-backend/src/main/resources/db/migration/pgsql_cluster/afterMigrate.sql b/dpppt-additionalinfo-backend/src/main/resources/db/migration/pgsql_cluster/afterMigrate.sql new file mode 100644 index 0000000..f255514 --- /dev/null +++ b/dpppt-additionalinfo-backend/src/main/resources/db/migration/pgsql_cluster/afterMigrate.sql @@ -0,0 +1,12 @@ +create or replace procedure reassign_objects_ownership() LANGUAGE 'plpgsql' +as $BODY$ +BEGIN + IF EXISTS(SELECT FROM pg_catalog.pg_roles where rolname = current_user || '_role_full') THEN + execute format('reassign owned by %s to %s_role_full', user, current_database()); + END IF; +END $BODY$; + + +call reassign_objects_ownership(); + +drop procedure reassign_objects_ownership(); \ No newline at end of file diff --git a/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/HistoryDataServiceTest.java b/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/HistoryDataServiceTest.java new file mode 100644 index 0000000..e3ccf7c --- /dev/null +++ b/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/HistoryDataServiceTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020 Ubique Innovation AG + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * SPDX-License-Identifier: MPL-2.0 + */ + +package org.dpppt.additionalinfo.backend.ws; + +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDate; + +import org.dpppt.additionalinfo.backend.ws.data.HistoryDataService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@SpringBootTest +@RunWith(SpringJUnit4ClassRunner.class) +@ComponentScan(basePackages = {"org.dpppt.additionalinfo.backend.ws.config"}) +@ActiveProfiles("postgres-test") +public class HistoryDataServiceTest { + + @Autowired HistoryDataService historyDataService; + + @Test + public void testUpsertFindAndDelete() { + LocalDate now = LocalDate.now(); + for (int i = 0; i < 100; i++) { + historyDataService.upsertLatestSevenDayAvgForDay(10, now.minusDays(i)); + } + for (int i = 0; i < 100; i++) { + assertEquals(10, historyDataService.findLatestSevenDayAvgForDay(now.minusDays(i))); + } + historyDataService.removeBefore(now.minusDays(10)); + + assertNull(historyDataService.findLatestSevenDayAvgForDay(now.minusDays(11))); + assertEquals(10, historyDataService.findLatestSevenDayAvgForDay(now.minusDays(10))); + } + +} diff --git a/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/config/PostgresDataConfig.java b/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/config/PostgresDataConfig.java index b781897..4335529 100644 --- a/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/config/PostgresDataConfig.java +++ b/dpppt-additionalinfo-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/config/PostgresDataConfig.java @@ -20,6 +20,7 @@ @Configuration @Profile("postgres-test") public class PostgresDataConfig extends WSDevConfig { + @Bean @Override public DataSource dataSource() {