From 1b5c79b6ff8effece0205bbb0498f14b49baa981 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 13 Jan 2023 21:44:02 +0100 Subject: [PATCH] test: Streamlined the config-IT, so that they use the same expectations - Both tests now use the same data and expect the same results. - Moved the actual tests and assertions to `AbstractConfigurationIT`. - Also streamlined `AbstractStorageStrategyIT`, so that it produces the exact same data, that the configuration-tests are expecting. This is not a necessary change to get the refined coniguration-tests running. It just makes it easier, to produce the needed test-data for future refinements. - Also streamlined the naming of the directory, where `InMemoryWithFilesStorageIT` stores its data. --- .../chat/backend/AbstractConfigurationIT.java | 59 +++++++++++++++++++ .../InMemoryWithFilesConfigurationIT.java | 45 +------------- .../InMemoryWithMongoDbConfigurationIT.java | 55 +---------------- .../AbstractStorageStrategyIT.java | 2 +- .../InMemoryWithFilesStorageIT.java | 2 +- .../5c73531c-6fc4-426c-adcb-afc5c140a0f7.json | 25 ++++++++ .../618e89ae-fdc0-4c65-8055-f49908295e8f.json | 19 ------ src/test/resources/data/files/chatrooms.json | 5 +- .../resources/data/mongodb/chatRoomTo.json | 11 ++-- 9 files changed, 97 insertions(+), 126 deletions(-) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java create mode 100644 src/test/resources/data/files/5c73531c-6fc4-426c-adcb-afc5c140a0f7.json delete mode 100644 src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java new file mode 100644 index 00000000..8c309b84 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractConfigurationIT.java @@ -0,0 +1,59 @@ +package de.juplo.kafka.chat.backend; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.testcontainers.shaded.org.awaitility.Awaitility; + +import java.time.Duration; + + +public abstract class AbstractConfigurationIT +{ + @LocalServerPort + int port; + @Autowired + WebTestClient webTestClient; + + @Test + @DisplayName("The app starts, the data is restored and accessible") + void test() + { + Awaitility + .await() + .atMost(Duration.ofSeconds(15)) + .untilAsserted(() -> + { + webTestClient + .get() + .uri("http://localhost:{port}/actuator/health", port) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.status").isEqualTo("UP"); + webTestClient + .get() + .uri("http://localhost:{port}/5c73531c-6fc4-426c-adcb-afc5c140a0f7", port) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.name").isEqualTo("FOO"); + webTestClient + .get() + .uri("http://localhost:{port}/5c73531c-6fc4-426c-adcb-afc5c140a0f7/ute/1", port) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute..."); + webTestClient + .get() + .uri("http://localhost:{port}/5c73531c-6fc4-426c-adcb-afc5c140a0f7/peter/1", port) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!"); + }); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java index 8aa0815e..0065219f 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java @@ -1,54 +1,11 @@ package de.juplo.kafka.chat.backend; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.http.MediaType; -import org.springframework.test.web.reactive.server.WebTestClient; -import org.testcontainers.shaded.org.awaitility.Awaitility; - -import java.time.Duration; @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "chat.backend.storage-directory=target/test-classes/data/files") -class InMemoryWithFilesConfigurationIT +class InMemoryWithFilesConfigurationIT extends AbstractConfigurationIT { - @LocalServerPort - private int port; - @Autowired - private WebTestClient webTestClient; - - @Test - void contextLoads() - { - Awaitility - .await() - .atMost(Duration.ofSeconds(15)) - .untilAsserted(() -> - { - webTestClient - .get() - .uri("http://localhost:{port}/actuator/health", port) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.status").isEqualTo("UP"); - webTestClient - .get() - .uri("http://localhost:{port}/618e89ae-fdc0-4c65-8055-f49908295e8f", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.name").isEqualTo("Peter's Chat-Room"); - webTestClient - .get() - .uri("http://localhost:{port}/618e89ae-fdc0-4c65-8055-f49908295e8f/ute/1478", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.text").isEqualTo("Nachricht Nr. 1478"); - }); - } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java index 4612aa8b..abb0956f 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java @@ -2,23 +2,14 @@ package de.juplo.kafka.chat.backend; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.http.MediaType; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; -import org.springframework.test.web.reactive.server.WebTestClient; import org.testcontainers.containers.BindMode; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.shaded.org.awaitility.Awaitility; - -import java.time.Duration; @SpringBootTest( @@ -29,52 +20,8 @@ import java.time.Duration; "spring.data.mongodb.database=test" }) @Testcontainers @Slf4j -class InMemoryWithMongoDbConfigurationIT +class InMemoryWithMongoDbConfigurationIT extends AbstractConfigurationIT { - @LocalServerPort - private int port; - @Autowired - private WebTestClient webTestClient; - - @Test - @DisplayName("The app starts, the data is restored and accessible") - void test() - { - Awaitility - .await() - .atMost(Duration.ofSeconds(15)) - .untilAsserted(() -> - { - webTestClient - .get() - .uri("http://localhost:{port}/actuator/health", port) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.status").isEqualTo("UP"); - webTestClient - .get() - .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.name").isEqualTo("FOO"); - webTestClient - .get() - .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/ute/1", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute..."); - webTestClient - .get() - .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/peter/1", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!"); - }); - } - private static final int MONGODB_PORT = 27017; @Container diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java index e305c128..84cf7d9b 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java @@ -40,7 +40,7 @@ public abstract class AbstractStorageStrategyIT assertThat(chathome.getChatRooms().toStream()).hasSize(0); - UUID chatRoomId = UUID.randomUUID(); + UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7"); ChatRoom chatroom = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block(); chathome.putChatRoom(chatroom); Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block(); diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java index bd1a5c6a..2c72ea0f 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java @@ -24,7 +24,7 @@ import java.util.function.Supplier; @Slf4j public class InMemoryWithFilesStorageIT extends AbstractStorageStrategyIT { - final static Path path = Paths.get("target","local-json-files"); + final static Path path = Paths.get("target","files"); final Clock clock; final ObjectMapper mapper; diff --git a/src/test/resources/data/files/5c73531c-6fc4-426c-adcb-afc5c140a0f7.json b/src/test/resources/data/files/5c73531c-6fc4-426c-adcb-afc5c140a0f7.json new file mode 100644 index 00000000..af5e6e01 --- /dev/null +++ b/src/test/resources/data/files/5c73531c-6fc4-426c-adcb-afc5c140a0f7.json @@ -0,0 +1,25 @@ +[ { + "id" : 1, + "serial" : 0, + "time" : "2023-01-13T21:46:51.846312215", + "user" : "peter", + "text" : "Hallo, ich heiße Peter!" +}, { + "id" : 1, + "serial" : 1, + "time" : "2023-01-13T21:46:51.847319045", + "user" : "ute", + "text" : "Ich bin Ute..." +}, { + "id" : 2, + "serial" : 2, + "time" : "2023-01-13T21:46:51.847415356", + "user" : "peter", + "text" : "Willst du mit mir gehen?" +}, { + "id" : 1, + "serial" : 3, + "time" : "2023-01-13T21:46:51.847465432", + "user" : "klaus", + "text" : "Ja? Nein? Vielleicht??" +} ] \ No newline at end of file diff --git a/src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json b/src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json deleted file mode 100644 index 0c8abcbe..00000000 --- a/src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json +++ /dev/null @@ -1,19 +0,0 @@ -[ { - "id" : 1477, - "serial" : 0, - "time" : "2023-01-08T00:10:08.817267716", - "user" : "ute", - "text" : "Nachricht Nr. 1477" -}, { - "id" : 1478, - "serial" : 1, - "time" : "2023-01-08T00:10:10.03333094", - "user" : "ute", - "text" : "Nachricht Nr. 1478" -}, { - "id" : 1479, - "serial" : 2, - "time" : "2023-01-08T00:10:11.22582036", - "user" : "ute", - "text" : "Nachricht Nr. 1479" -} ] diff --git a/src/test/resources/data/files/chatrooms.json b/src/test/resources/data/files/chatrooms.json index 5b6c22b5..d0f1b92c 100644 --- a/src/test/resources/data/files/chatrooms.json +++ b/src/test/resources/data/files/chatrooms.json @@ -1,4 +1,5 @@ [ { - "id" : "618e89ae-fdc0-4c65-8055-f49908295e8f", - "name" : "Peter's Chat-Room" + "id" : "5c73531c-6fc4-426c-adcb-afc5c140a0f7", + "name" : "FOO", + "shard" : 0 } ] \ No newline at end of file diff --git a/src/test/resources/data/mongodb/chatRoomTo.json b/src/test/resources/data/mongodb/chatRoomTo.json index c100cebf..5d1bee05 100644 --- a/src/test/resources/data/mongodb/chatRoomTo.json +++ b/src/test/resources/data/mongodb/chatRoomTo.json @@ -1,29 +1,30 @@ { - "_id": "4ace69b7-a79f-481b-ad0d-d756d60b66ec", + "_id": "5c73531c-6fc4-426c-adcb-afc5c140a0f7", "name": "FOO", + "shard": 0, "messages": [ { "_id": "peter--1", "serial": 0, - "time": "2023-01-11T20:53:05.143528961", + "time": "2023-01-13T20:43:16.803382151", "text": "Hallo, ich heiße Peter!" }, { "_id": "ute--1", "serial": 1, - "time": "2023-01-11T20:53:05.144161529", + "time": "2023-01-13T20:43:16.804049969", "text": "Ich bin Ute..." }, { "_id": "peter--2", "serial": 2, - "time": "2023-01-11T20:53:05.144202513", + "time": "2023-01-13T20:43:16.804092782", "text": "Willst du mit mir gehen?" }, { "_id": "klaus--1", "serial": 3, - "time": "2023-01-11T20:53:05.144236865", + "time": "2023-01-13T20:43:16.804122604", "text": "Ja? Nein? Vielleicht??" } ], -- 2.20.1