test: Added integration-test `InMemoryWithMongoDbStorageIT`
authorKai Moritz <kai@juplo.de>
Wed, 11 Jan 2023 22:07:42 +0000 (23:07 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 20:59:37 +0000 (21:59 +0100)
src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java
src/test/resources/application.yml
src/test/resources/data/mongodb/0001.sh [new file with mode: 0644]
src/test/resources/data/mongodb/chatRoomTo.json [new file with mode: 0644]

diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java
new file mode 100644 (file)
index 0000000..1a93841
--- /dev/null
@@ -0,0 +1,101 @@
+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(
+               webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
+               properties = {
+                               "chat.backend.storage=mongodb",
+                               "spring.data.mongodb.host=localhost",
+                               "spring.data.mongodb.database=test" })
+@Testcontainers
+@Slf4j
+class InMemoryWithMongoDbStorageIT
+{
+       @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
+       private static final GenericContainer CONTAINER =
+                       new GenericContainer("mongo:6")
+                                       .withClasspathResourceMapping(
+                                                       "data/mongodb",
+                                                       "/docker-entrypoint-initdb.d",
+                                                       BindMode.READ_ONLY)
+                                       .withExposedPorts(MONGODB_PORT);
+
+       @DynamicPropertySource
+       static void addMongoPortProperty(DynamicPropertyRegistry registry)
+       {
+               registry.add("spring.data.mongodb.port", () -> CONTAINER.getMappedPort(27017));
+       }
+
+       @BeforeEach
+       void setUpLogging()
+       {
+               Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
+               CONTAINER.followOutput(logConsumer);
+       }
+}
index d094808..ae92c9e 100644 (file)
@@ -7,6 +7,7 @@ import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
 import de.juplo.kafka.chat.backend.persistence.storage.mongodb.ChatRoomRepository;
 import de.juplo.kafka.chat.backend.persistence.storage.mongodb.MongoDbStorageStrategy;
 import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -15,10 +16,10 @@ import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.context.ApplicationContextInitializer;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
-import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 import org.springframework.test.context.support.TestPropertySourceUtils;
+import org.testcontainers.containers.BindMode;
 import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.junit.jupiter.Container;
@@ -82,7 +83,6 @@ public class InMemoryWithMongoDbStorageStrategyIT extends AbstractStorageStrateg
   @Container
   private static final GenericContainer CONTAINER =
       new GenericContainer("mongo:6")
-          .withEnv("MONGO_INITDB_DATABASE", "test")
           .withExposedPorts(MONGODB_PORT);
 
   public static class DataSourceInitializer
@@ -96,8 +96,13 @@ public class InMemoryWithMongoDbStorageStrategyIT extends AbstractStorageStrateg
           "spring.data.mongodb.host=localhost",
           "spring.data.mongodb.port=" + CONTAINER.getMappedPort(MONGODB_PORT),
           "spring.data.mongodb.database=test");
+    }
+  }
 
-      Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
-      CONTAINER.followOutput(logConsumer);    }
+  @BeforeEach
+  void setUpLogging()
+  {
+    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
+    CONTAINER.followOutput(logConsumer);
   }
 }
index c26dd3e..96b0cb3 100644 (file)
@@ -1,3 +1,4 @@
 logging:
   level:
+    root: INFO
     de.juplo.kafka.chat.backend: DEBUG
diff --git a/src/test/resources/data/mongodb/0001.sh b/src/test/resources/data/mongodb/0001.sh
new file mode 100644 (file)
index 0000000..014a8be
--- /dev/null
@@ -0,0 +1,2 @@
+#!/bin/bash
+mongoimport --collection=chatRoomTo --db=test /docker-entrypoint-initdb.d/chatRoomTo.json
diff --git a/src/test/resources/data/mongodb/chatRoomTo.json b/src/test/resources/data/mongodb/chatRoomTo.json
new file mode 100644 (file)
index 0000000..c100ceb
--- /dev/null
@@ -0,0 +1,31 @@
+{
+  "_id": "4ace69b7-a79f-481b-ad0d-d756d60b66ec",
+  "name": "FOO",
+  "messages": [
+    {
+      "_id": "peter--1",
+      "serial": 0,
+      "time": "2023-01-11T20:53:05.143528961",
+      "text": "Hallo, ich heiße Peter!"
+    },
+    {
+      "_id": "ute--1",
+      "serial": 1,
+      "time": "2023-01-11T20:53:05.144161529",
+      "text": "Ich bin Ute..."
+    },
+    {
+      "_id": "peter--2",
+      "serial": 2,
+      "time": "2023-01-11T20:53:05.144202513",
+      "text": "Willst du mit mir gehen?"
+    },
+    {
+      "_id": "klaus--1",
+      "serial": 3,
+      "time": "2023-01-11T20:53:05.144236865",
+      "text": "Ja? Nein? Vielleicht??"
+    }
+  ],
+  "_class": "de.juplo.kafka.chat.backend.persistence.storage.mongodb.ChatRoomTo"
+}