test: RED - Putting a message in a newly created chat-room
[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         {
45           webTestClient
46               .get()
47               .uri(
48                   "http://localhost:{port}/actuator/health",
49                   port)
50               .exchange()
51               .expectStatus().isOk()
52               .expectBody().jsonPath("$.status").isEqualTo("UP");
53         });
54   }
55
56   @Test
57   @DisplayName("Restored chat-rooms can be listed")
58   void testRestoredChatRoomsCanBeListed()
59   {
60     Awaitility
61         .await()
62         .atMost(Duration.ofSeconds(15))
63         .untilAsserted(() ->
64         {
65           webTestClient
66               .get()
67               .uri(
68                   "http://localhost:{port}/list",
69                   port)
70               .accept(MediaType.APPLICATION_JSON)
71               .exchange()
72               .expectStatus().isOk()
73               .expectBody()
74                 .jsonPath("$.length()").isEqualTo(1)
75                 .jsonPath("$[0].name").isEqualTo("FOO");
76         });
77   }
78
79   @Test
80   @DisplayName("Details as expected for restored chat-room")
81   void testRestoredChatRoomHasExpectedDetails()
82   {
83     Awaitility
84         .await()
85         .atMost(Duration.ofSeconds(15))
86         .untilAsserted(() ->
87         {
88           webTestClient
89               .get()
90               .uri(
91                   "http://localhost:{port}/{chatRoomId}",
92                   port,
93                   EXISTING_CHATROOM)
94               .accept(MediaType.APPLICATION_JSON)
95               .exchange()
96               .expectStatus().isOk()
97               .expectBody().jsonPath("$.name").isEqualTo("FOO");
98         });
99   }
100
101   @Test
102   @DisplayName("Restored message from Ute has expected Text")
103   void testRestoredMessageForUteHasExpectedText()
104   {
105     Awaitility
106         .await()
107         .atMost(Duration.ofSeconds(15))
108         .untilAsserted(() ->
109         {
110           webTestClient
111               .get()
112               .uri(
113                   "http://localhost:{port}/{chatRoomId}/ute/1",
114                   port,
115                   EXISTING_CHATROOM)
116               .accept(MediaType.APPLICATION_JSON)
117               .exchange()
118               .expectStatus().isOk()
119               .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
120         });
121   }
122
123   @Test
124   @DisplayName("Restored message from Peter has expected Text")
125   void testRestoredMessageForPeterHasExpectedText()
126   {
127     Awaitility
128         .await()
129         .atMost(Duration.ofSeconds(15))
130         .untilAsserted(() ->
131         {
132           webTestClient
133               .get()
134               .uri(
135                   "http://localhost:{port}/{chatRoomId}/peter/1",
136                   port,
137                   EXISTING_CHATROOM)
138               .accept(MediaType.APPLICATION_JSON)
139               .exchange()
140               .expectStatus().isOk()
141               .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
142         });
143   }
144
145   @Test
146   @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
147   void testNotFoundForPutMessageToNonExistentChatRoom()
148   {
149     Awaitility
150         .await()
151         .atMost(Duration.ofSeconds(15))
152         .untilAsserted(() ->
153         {
154           webTestClient
155               .put()
156               .uri(
157                   "http://localhost:{port}/{chatRoomId}/otto/66",
158                   port,
159                   NONEXISTENT_CHATROOM)
160               .contentType(MediaType.TEXT_PLAIN)
161               .accept(MediaType.APPLICATION_JSON)
162               .bodyValue("The devil rules route 66")
163               .exchange()
164               .expectStatus().isNotFound()
165               .expectBody()
166                 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
167                 .jsonPath("$.chatroomId").isEqualTo(NONEXISTENT_CHATROOM);
168         });
169   }
170
171   @Test
172   @DisplayName("A message can be put into a newly created chat-room")
173   void testPutMessageInNewChatRoom() throws IOException
174   {
175     Awaitility
176         .await()
177         .atMost(Duration.ofSeconds(15))
178         .untilAsserted(() ->
179         {
180           byte[] result = webTestClient
181               .post()
182               .uri("http://localhost:{port}/create", port)
183               .contentType(MediaType.TEXT_PLAIN)
184               .bodyValue("bar")
185               .accept(MediaType.APPLICATION_JSON)
186               .exchange()
187               .expectStatus().isOk()
188               .expectBody()
189                 .jsonPath("$.id").exists()
190                 .jsonPath("$.name").isEqualTo("bar")
191                 // The hard must not be asserted, because not all implementations ar aware of it
192                 // .jsonPath("$.shard").isEqualTo(Integer.valueOf(2))
193               .returnResult()
194               .getResponseBody();
195           ChatRoomInfoTo chatRoomInfo = objectMapper.readValue(result, ChatRoomInfoTo.class);
196           UUID chatRoomId = chatRoomInfo.getId();
197           webTestClient
198               .put()
199               .uri(
200                   "http://localhost:{port}/{chatRoomId}/nerd/7",
201                   port,
202                   chatRoomId)
203               .contentType(MediaType.TEXT_PLAIN)
204               .accept(MediaType.APPLICATION_JSON)
205               .bodyValue("Hello world!")
206               .exchange()
207               .expectStatus().isOk()
208               .expectBody()
209                 .jsonPath("$.id").isEqualTo(Integer.valueOf(7))
210                 .jsonPath("$.user").isEqualTo("nerd")
211                 .jsonPath("$.text").isEqualTo("Hello world!");
212         });
213   }
214 }