From 2981691ffc86a14634eb4f42ad0260c7bbffef2c Mon Sep 17 00:00:00 2001 From: Piotr Dec Date: Fri, 24 May 2024 19:37:56 +0200 Subject: [PATCH] test: Integration tests outline --- pom.xml | 36 +++++ .../eu/ztsh/wymiana/config/NbpProperties.java | 8 ++ .../config/RestClientConfiguration.java | 4 +- src/main/resources/application.yaml | 12 ++ .../web/ApplicationIntegrationTests.java | 131 ++++++++++++++++++ src/test/resources/application-it.yaml | 2 + 6 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/ztsh/wymiana/config/NbpProperties.java create mode 100644 src/test/java/eu/ztsh/wymiana/web/ApplicationIntegrationTests.java create mode 100644 src/test/resources/application-it.yaml diff --git a/pom.xml b/pom.xml index 3756469..257ebbb 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,10 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-actuator + @@ -69,6 +73,12 @@ org.springframework.boot spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-webflux + test org.junit.jupiter @@ -109,6 +119,32 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + default-test + + + **/*Tests.java + + + + + integration-tests + test + + test + + + + **/*Test.java + + + + + diff --git a/src/main/java/eu/ztsh/wymiana/config/NbpProperties.java b/src/main/java/eu/ztsh/wymiana/config/NbpProperties.java new file mode 100644 index 0000000..0ed6c42 --- /dev/null +++ b/src/main/java/eu/ztsh/wymiana/config/NbpProperties.java @@ -0,0 +1,8 @@ +package eu.ztsh.wymiana.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("nbp") +public record NbpProperties(String baseurl) { + +} diff --git a/src/main/java/eu/ztsh/wymiana/config/RestClientConfiguration.java b/src/main/java/eu/ztsh/wymiana/config/RestClientConfiguration.java index 74ee47b..f264dbe 100644 --- a/src/main/java/eu/ztsh/wymiana/config/RestClientConfiguration.java +++ b/src/main/java/eu/ztsh/wymiana/config/RestClientConfiguration.java @@ -8,9 +8,9 @@ import org.springframework.web.client.RestClient; public class RestClientConfiguration { @Bean - public RestClient restClient() { + public RestClient restClient(NbpProperties nbpProperties) { return RestClient.builder() - .baseUrl("http://api.nbp.pl") + .baseUrl(nbpProperties.baseurl()) .defaultHeader("Accept", "application/json") .build(); } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index ae6306d..47b720c 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -2,6 +2,9 @@ hsqldb: name: db port: 9090 +nbp: + baseurl: "http://api.nbp.pl" + spring: datasource: username: sa @@ -11,3 +14,12 @@ spring: jpa: hibernate: ddl-auto: create + +management: + endpoints: + jmx: + exposure: + exclude: '*' + web: + exposure: + include: health diff --git a/src/test/java/eu/ztsh/wymiana/web/ApplicationIntegrationTests.java b/src/test/java/eu/ztsh/wymiana/web/ApplicationIntegrationTests.java new file mode 100644 index 0000000..8781c04 --- /dev/null +++ b/src/test/java/eu/ztsh/wymiana/web/ApplicationIntegrationTests.java @@ -0,0 +1,131 @@ +package eu.ztsh.wymiana.web; + +import eu.ztsh.wymiana.WireMockExtension; +import org.junit.jupiter.api.ClassOrderer; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestClassOrder; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration test suite. + * Contrary to the principle of test independence, tests are dependent on one another to create continuous suite. + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("it") +@ExtendWith(WireMockExtension.class) +@TestMethodOrder(MethodOrderer.DisplayName.class) +@TestClassOrder(ClassOrderer.DisplayName.class) +class ApplicationIntegrationTests { + + private final WebTestClient webTestClient; + + @Autowired + public ApplicationIntegrationTests(WebTestClient webTestClient) { + this.webTestClient = webTestClient; + } + + @Nested + @TestMethodOrder(MethodOrderer.DisplayName.class) + @DisplayName("01: Context") + class ContextTests { + + @Test + @DisplayName("01.1: Load context") + void contextLoads() { + assertThat(webTestClient).isNotNull(); + webTestClient.get().uri("/actuator/health").exchange().expectBody().json("{\"status\":\"UP\"}"); + } + + } + + @Nested + @TestMethodOrder(MethodOrderer.DisplayName.class) + @DisplayName("02: User") + class UserTests { + + @Test + @DisplayName("02.1: Create valid user") + void createUserTest() { + + } + + @Test + @DisplayName("02.2: Try to create invalid user") + void createInvalidUserTest() { + + } + + @Test + @DisplayName("02.3: Try to create duplicated user") + void createDuplicatedUserTest() { + + } + + @Test + @DisplayName("02.4: Get valid user") + void getValidUserTest() { + + } + + @Test + @DisplayName("02.5: Try to get non-existing user") + void getNonExistingUserTest() { + + } + + @Test + @DisplayName("02.6: Get user by incorrect PESEL") + void getIncorrectPeselUserTest() { + + } + + } + + @Nested + @TestMethodOrder(MethodOrderer.DisplayName.class) + @DisplayName("03: Exchange") + class ExchangeTests { + + @Test + @DisplayName("03.1: Try to perform invalid money exchange: no data") + void noNbpDataTest() { + + } + + @Test + @DisplayName("03.2: Perform valid money exchange") + void exchangeTest() { + + } + + @Test + @DisplayName("03.3: Try to perform invalid money exchange: not existing user") + void exchangeNotExistingUserTest() { + + } + + @Test + @DisplayName("03.4: Try to perform invalid money exchange: invalid PESEL") + void invalidPeselTest() { + + } + + @Test + @DisplayName("03.5: Try to perform invalid money exchange: insufficient funds") + void insufficientFundsTest() { + + } + + } + +} diff --git a/src/test/resources/application-it.yaml b/src/test/resources/application-it.yaml new file mode 100644 index 0000000..198e366 --- /dev/null +++ b/src/test/resources/application-it.yaml @@ -0,0 +1,2 @@ +nbp: + baseurl: "http://localhost:38080"