From 6b6c41e950d28298533dc042daebbe11f039b172 Mon Sep 17 00:00:00 2001 From: lmeinen Date: Fri, 25 Jun 2021 14:04:17 +0200 Subject: [PATCH] Implemented unit test & added logging --- .../data/AppTokenDataServiceTest.java | 30 +++++++++++++++++++ .../verifier/data/config/TestConfig.java | 7 +++++ .../ws/config/WSSchedulingConfig.java | 7 +++++ 3 files changed, 44 insertions(+) create mode 100644 ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/AppTokenDataServiceTest.java diff --git a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/AppTokenDataServiceTest.java b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/AppTokenDataServiceTest.java new file mode 100644 index 00000000..77d5bf0c --- /dev/null +++ b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/AppTokenDataServiceTest.java @@ -0,0 +1,30 @@ +package ch.admin.bag.covidcertificate.backend.verifier.data; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.sql.DataSource; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; + +class AppTokenDataServiceTest extends BaseDataServiceTest { + + @Autowired AppTokenDataService appTokenDataService; + @Autowired DataSource dataSource; + + @Test + void getAppTokensTest() { + var appTokens = appTokenDataService.getAppTokens(); + assertTrue(appTokens.isEmpty()); + final var insert = new SimpleJdbcInsert(dataSource).withTableName("t_app_tokens"); + final var params = new MapSqlParameterSource(); + params.addValue("api_key", "4d1d5663-b4ef-46a5-85b6-3d1d376429da"); + params.addValue("description", "local"); + insert.execute(params); + appTokens = appTokenDataService.getAppTokens(); + assertEquals(1, appTokens.size()); + } +} diff --git a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/config/TestConfig.java b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/config/TestConfig.java index ad03f825..6024ead8 100644 --- a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/config/TestConfig.java +++ b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-data/src/test/java/ch/admin/bag/covidcertificate/backend/verifier/data/config/TestConfig.java @@ -10,7 +10,9 @@ package ch.admin.bag.covidcertificate.backend.verifier.data.config; +import ch.admin.bag.covidcertificate.backend.verifier.data.AppTokenDataService; import ch.admin.bag.covidcertificate.backend.verifier.data.VerifierDataService; +import ch.admin.bag.covidcertificate.backend.verifier.data.impl.JdbcAppTokenDataServiceImpl; import ch.admin.bag.covidcertificate.backend.verifier.data.impl.JdbcVerifierDataServiceImpl; import javax.sql.DataSource; import org.springframework.boot.test.context.TestConfiguration; @@ -25,4 +27,9 @@ public class TestConfig { public VerifierDataService verifierDataService(DataSource dataSource) { return new JdbcVerifierDataServiceImpl(dataSource); } + + @Bean + public AppTokenDataService appTokenDataService(DataSource dataSource) { + return new JdbcAppTokenDataServiceImpl(dataSource); + } } diff --git a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-ws/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/ws/config/WSSchedulingConfig.java b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-ws/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/ws/config/WSSchedulingConfig.java index b2604d0c..b3b06f6d 100644 --- a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-ws/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/ws/config/WSSchedulingConfig.java +++ b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-ws/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/ws/config/WSSchedulingConfig.java @@ -2,8 +2,11 @@ import ch.admin.bag.covidcertificate.backend.verifier.data.AppTokenDataService; import ch.admin.bag.covidcertificate.backend.verifier.ws.config.model.ApiKeyConfig; +import ch.admin.bag.covidcertificate.backend.verifier.ws.controller.RevocationListController; import java.util.HashMap; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @@ -15,6 +18,8 @@ public class WSSchedulingConfig { private final ApiKeyConfig apiKeyConfig; private final AppTokenDataService appTokenDataService; + private static final Logger logger = LoggerFactory.getLogger(WSSchedulingConfig.class); + public WSSchedulingConfig(ApiKeyConfig apiKeyConfig, AppTokenDataService appTokenDataService) { this.apiKeyConfig = apiKeyConfig; this.appTokenDataService = appTokenDataService; @@ -22,7 +27,9 @@ public WSSchedulingConfig(ApiKeyConfig apiKeyConfig, AppTokenDataService appToke // Call method every 5 minutes starting at 0am, of every day @Scheduled(cron = "${ws.authentication.cron:0 0/5 0 ? * *}") + @Scheduled(fixedRate = 30000, initialDelay = 10000) public void updateAppTokens() { + logger.info("Updating app tokens"); final var appTokens = appTokenDataService.getAppTokens(); final Map apiKeys = new HashMap<>(); for (var token : appTokens) {