WIP
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / AbstractConfigurationIT.java
1 package de.juplo.kafka.chat.backend;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
5 import lombok.extern.slf4j.Slf4j;
6 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.boot.test.web.server.LocalServerPort;
11 import org.springframework.http.MediaType;
12 import org.springframework.test.web.reactive.server.WebTestClient;
13 import org.testcontainers.shaded.org.awaitility.Awaitility;
14
15 import java.io.IOException;
16 import java.time.Duration;
17 import java.util.UUID;
18
19 import static org.hamcrest.Matchers.endsWith;
20
21
22 @Slf4j
23 public abstract class AbstractConfigurationIT
24 {
25   final static String EXISTING_CHATROOM = "5c73531c-6fc4-426c-adcb-afc5c140a0f7";
26   String NONEXISTENT_CHATROOM = "7f59ec77-832e-4a17-8d22-55ef46242c17";
27
28
29   @LocalServerPort
30   int port;
31   @Autowired
32   WebTestClient webTestClient;
33   @Autowired
34   ObjectMapper objectMapper;
35
36
37   @BeforeEach
38   void waitForApp()
39   {
40     Awaitility
41         .await()
42         .atMost(Duration.ofSeconds(15))
43         .untilAsserted(() ->
44             webTestClient
45                 .get()
46                 .uri(
47                     "http://localhost:{port}/actuator/health",
48                     port)
49                 .exchange()
50                 .expectStatus().isOk()
51                 .expectBody().jsonPath("$.status").isEqualTo("UP"));
52   }
53
54   @Test
55   @DisplayName("Restored chat-rooms can be listed")
56   void testRestoredChatRoomsCanBeListed()
57   {
58     Awaitility
59         .await()
60         .atMost(Duration.ofSeconds(15))
61         .untilAsserted(() ->
62             webTestClient
63                 .get()
64                 .uri(
65                     "http://localhost:{port}/list",
66                     port)
67                 .accept(MediaType.APPLICATION_JSON)
68                 .exchange()
69                 .expectStatus().isOk()
70                 .expectBody()
71                 .jsonPath("$.length()").isEqualTo(1)
72                 .jsonPath("$[0].name").isEqualTo("FOO"));
73   }
74
75   @Test
76   @DisplayName("Details as expected for restored chat-room")
77   void testRestoredChatRoomHasExpectedDetails()
78   {
79     Awaitility
80         .await()
81         .atMost(Duration.ofSeconds(15))
82         .untilAsserted(() ->
83             webTestClient
84                 .get()
85                 .uri(
86                     "http://localhost:{port}/{chatRoomId}",
87                     port,
88                     EXISTING_CHATROOM)
89                 .accept(MediaType.APPLICATION_JSON)
90                 .exchange()
91                 .expectStatus().isOk()
92                 .expectBody().jsonPath("$.name").isEqualTo("FOO"));
93   }
94
95   @Test
96   @DisplayName("Restored message from Ute has expected Text")
97   void testRestoredMessageForUteHasExpectedText()
98   {
99     Awaitility
100         .await()
101         .atMost(Duration.ofSeconds(15))
102         .untilAsserted(() ->
103             webTestClient
104                 .get()
105                 .uri(
106                     "http://localhost:{port}/{chatRoomId}/ute/1",
107                     port,
108                     EXISTING_CHATROOM)
109                 .accept(MediaType.APPLICATION_JSON)
110                 .exchange()
111                 .expectStatus().isOk()
112                 .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute..."));
113   }
114
115   @Test
116   @DisplayName("Restored message from Peter has expected Text")
117   void testRestoredMessageForPeterHasExpectedText()
118   {
119     Awaitility
120         .await()
121         .atMost(Duration.ofSeconds(15))
122         .untilAsserted(() ->
123             webTestClient
124                 .get()
125                 .uri(
126                     "http://localhost:{port}/{chatRoomId}/peter/1",
127                     port,
128                     EXISTING_CHATROOM)
129                 .accept(MediaType.APPLICATION_JSON)
130                 .exchange()
131                 .expectStatus().isOk()
132                 .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!"));
133   }
134
135   @Test
136   @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
137   void testNotFoundForPutMessageToNonExistentChatRoom()
138   {
139     Awaitility
140         .await()
141         .atMost(Duration.ofSeconds(15))
142         .untilAsserted(() ->
143             webTestClient
144                 .put()
145                 .uri(
146                     "http://localhost:{port}/{chatRoomId}/otto/66",
147                     port,
148                     NONEXISTENT_CHATROOM)
149                 .contentType(MediaType.TEXT_PLAIN)
150                 .accept(MediaType.APPLICATION_JSON)
151                 .bodyValue("The devil rules route 66")
152                 .exchange()
153                 .expectStatus().isNotFound()
154                 .expectBody()
155                 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
156                 .jsonPath("$.chatroomId").isEqualTo(NONEXISTENT_CHATROOM));
157   }
158
159   @Test
160   @DisplayName("A message can be put into a newly created chat-room")
161   void testPutMessageInNewChatRoom() throws IOException
162   {
163     Awaitility
164         .await()
165         .atMost(Duration.ofSeconds(15))
166         .untilAsserted(() ->
167         {
168           byte[] result = webTestClient
169               .post()
170               .uri("http://localhost:{port}/create", port)
171               .contentType(MediaType.TEXT_PLAIN)
172               .bodyValue("bar")
173               .accept(MediaType.APPLICATION_JSON)
174               .exchange()
175               .expectStatus().isOk()
176               .expectBody()
177               .jsonPath("$.id").exists()
178               .jsonPath("$.name").isEqualTo("bar")
179               .jsonPath("$.shard").isEqualTo(Integer.valueOf(2))
180               .returnResult()
181               .getResponseBody();
182           ChatRoomInfoTo chatRoomInfo = objectMapper.readValue(result, ChatRoomInfoTo.class);
183           UUID chatRoomId = chatRoomInfo.getId();
184           webTestClient
185               .put()
186               .uri(
187                   "http://localhost:{port}/{chatRoomId}/nerd/7",
188                   port,
189                   chatRoomId)
190               .contentType(MediaType.TEXT_PLAIN)
191               .accept(MediaType.APPLICATION_JSON)
192               .bodyValue("Hello world!")
193               .exchange()
194               .expectStatus().isOk()
195               .expectBody()
196               .jsonPath("$.id").isEqualTo(Integer.valueOf(7))
197               .jsonPath("$.user").isEqualTo("nerd")
198               .jsonPath("$.text").isEqualTo("Hello world!");
199         });
200   }
201 }