refactor: DRY für shard-selection
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / InMemoryWithMongoDbStorageIT.java
1 package de.juplo.kafka.chat.backend;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.junit.jupiter.api.BeforeEach;
5 import org.junit.jupiter.api.DisplayName;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.boot.test.web.server.LocalServerPort;
10 import org.springframework.http.MediaType;
11 import org.springframework.test.context.DynamicPropertyRegistry;
12 import org.springframework.test.context.DynamicPropertySource;
13 import org.springframework.test.web.reactive.server.WebTestClient;
14 import org.testcontainers.containers.BindMode;
15 import org.testcontainers.containers.GenericContainer;
16 import org.testcontainers.containers.output.Slf4jLogConsumer;
17 import org.testcontainers.junit.jupiter.Container;
18 import org.testcontainers.junit.jupiter.Testcontainers;
19 import org.testcontainers.shaded.org.awaitility.Awaitility;
20
21 import java.time.Duration;
22
23
24 @SpringBootTest(
25                 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
26                 properties = {
27                                 "chat.backend.storage=mongodb",
28                                 "spring.data.mongodb.host=localhost",
29                                 "spring.data.mongodb.database=test" })
30 @Testcontainers
31 @Slf4j
32 class InMemoryWithMongoDbStorageIT
33 {
34         @LocalServerPort
35         private int port;
36         @Autowired
37         private WebTestClient webTestClient;
38
39         @Test
40         @DisplayName("The app starts, the data is restored and accessible")
41         void test()
42         {
43                 Awaitility
44                                 .await()
45                                 .atMost(Duration.ofSeconds(15))
46                                 .untilAsserted(() ->
47                                 {
48                                         webTestClient
49                                                         .get()
50                                                         .uri("http://localhost:{port}/actuator/health", port)
51                                                         .exchange()
52                                                         .expectStatus().isOk()
53                                                         .expectBody().jsonPath("$.status").isEqualTo("UP");
54                                         webTestClient
55                                                         .get()
56                                                         .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec", port)
57                                                         .accept(MediaType.APPLICATION_JSON)
58                                                         .exchange()
59                                                         .expectStatus().isOk()
60                                                         .expectBody().jsonPath("$.name").isEqualTo("FOO");
61                                         webTestClient
62                                                         .get()
63                                                         .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/ute/1", port)
64                                                         .accept(MediaType.APPLICATION_JSON)
65                                                         .exchange()
66                                                         .expectStatus().isOk()
67                                                         .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
68                                         webTestClient
69                                                         .get()
70                                                         .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/peter/1", port)
71                                                         .accept(MediaType.APPLICATION_JSON)
72                                                         .exchange()
73                                                         .expectStatus().isOk()
74                                                         .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
75                                 });
76         }
77
78         private static final int MONGODB_PORT = 27017;
79
80         @Container
81         private static final GenericContainer CONTAINER =
82                         new GenericContainer("mongo:6")
83                                         .withClasspathResourceMapping(
84                                                         "data/mongodb",
85                                                         "/docker-entrypoint-initdb.d",
86                                                         BindMode.READ_ONLY)
87                                         .withExposedPorts(MONGODB_PORT);
88
89         @DynamicPropertySource
90         static void addMongoPortProperty(DynamicPropertyRegistry registry)
91         {
92                 registry.add("spring.data.mongodb.port", () -> CONTAINER.getMappedPort(27017));
93         }
94
95         @BeforeEach
96         void setUpLogging()
97         {
98                 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
99                 CONTAINER.followOutput(logConsumer);
100         }
101 }