Skip to content

Commit

Permalink
PP-839: Add unit test. Add Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinalig committed Mar 12, 2021
1 parent 172c2ce commit d51d6c8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 26 deletions.
27 changes: 1 addition & 26 deletions dpppt-additionalinfo-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<properties>
<java-version>11</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot-version>2.2.6.RELEASE</spring-boot-version>
<spring-boot-version>2.3.9.RELEASE</spring-boot-version>
<itCoverageAgent></itCoverageAgent>
<build-helper-maven-plugin.version>1.9.1</build-helper-maven-plugin.version>
</properties>
Expand Down Expand Up @@ -117,31 +117,6 @@
<artifactId>spring-security-web</artifactId>
</dependency>

<!-- security: jwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.1</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-root</artifactId>
<version>0.11.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.1</version>
<scope>runtime</scope>
</dependency>

<!-- security: pem -->
<dependency>
<groupId>org.bouncycastle</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -39,6 +41,7 @@ private void removeLatestSevenDayAvgForDay(LocalDate day, boolean deleteBefore)
}

@Override
@Transactional(readOnly = true)
public Integer findLatestSevenDayAvgForDay(LocalDate day) {
try {
return jt.queryForObject(
Expand All @@ -49,4 +52,10 @@ public Integer findLatestSevenDayAvgForDay(LocalDate day) {
return null;
}
}

@Override
@Transactional(readOnly = false)
public void removeBefore(LocalDate day) {
removeLatestSevenDayAvgForDay(day, true);
}
}
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* 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)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@Configuration
@Profile("postgres-test")
public class PostgresDataConfig extends WSDevConfig {

@Bean
@Override
public DataSource dataSource() {
Expand Down

0 comments on commit d51d6c8

Please sign in to comment.