test: RED - Added a test for a put to a non-existent 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 org.junit.jupiter.api.DisplayName;
4 import org.junit.jupiter.api.Test;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.boot.test.web.server.LocalServerPort;
7 import org.springframework.http.MediaType;
8 import org.springframework.test.web.reactive.server.WebTestClient;
9 import org.testcontainers.shaded.org.awaitility.Awaitility;
10
11 import java.time.Duration;
12
13 import static org.hamcrest.Matchers.endsWith;
14
15
16 public abstract class AbstractConfigurationIT
17 {
18   @LocalServerPort
19   int port;
20   @Autowired
21   WebTestClient webTestClient;
22
23
24   @Test
25   @DisplayName("The app starts, the data is restored and accessible")
26   void testAppStartsDataIsRestoredAndAccessible()
27   {
28     String chatRoomId = "5c73531c-6fc4-426c-adcb-afc5c140a0f7";
29
30     Awaitility
31         .await()
32         .atMost(Duration.ofSeconds(15))
33         .untilAsserted(() ->
34         {
35           webTestClient
36               .get()
37               .uri(
38                   "http://localhost:{port}/actuator/health",
39                   port)
40               .exchange()
41               .expectStatus().isOk()
42               .expectBody().jsonPath("$.status").isEqualTo("UP");
43           webTestClient
44               .get()
45               .uri(
46                   "http://localhost:{port}/list",
47                   port)
48               .accept(MediaType.APPLICATION_JSON)
49               .exchange()
50               .expectStatus().isOk()
51               .expectBody()
52                 .jsonPath("$.length()").isEqualTo(1)
53                 .jsonPath("$[0].name").isEqualTo("FOO");
54           webTestClient
55               .get()
56               .uri("http://localhost:{port}/{chatRoomId}",
57                   port,
58                   chatRoomId)
59               .accept(MediaType.APPLICATION_JSON)
60               .exchange()
61               .expectStatus().isOk()
62               .expectBody().jsonPath("$.name").isEqualTo("FOO");
63           webTestClient
64               .get()
65               .uri(
66                   "http://localhost:{port}/{chatRoomId}/ute/1",
67                   port,
68                   chatRoomId)
69               .accept(MediaType.APPLICATION_JSON)
70               .exchange()
71               .expectStatus().isOk()
72               .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
73           webTestClient
74               .get()
75               .uri(
76                   "http://localhost:{port}/{chatRoomId}/peter/1",
77                   port,
78                   chatRoomId)
79               .accept(MediaType.APPLICATION_JSON)
80               .exchange()
81               .expectStatus().isOk()
82               .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
83         });
84   }
85
86   @Test
87   @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
88   void testNotFoundForPutMessageToNonExistentChatRoom()
89   {
90     String otherChatRoomId = "7f59ec77-832e-4a17-8d22-55ef46242c17";
91
92     Awaitility
93         .await()
94         .atMost(Duration.ofSeconds(15))
95         .untilAsserted(() ->
96           webTestClient
97               .put()
98               .uri(
99                   "http://localhost:{port}/{chatRoomId}/otto/66",
100                   port,
101                   otherChatRoomId)
102               .contentType(MediaType.TEXT_PLAIN)
103               .accept(MediaType.APPLICATION_JSON)
104               .bodyValue("The devil rules route 66")
105               .exchange()
106               .expectStatus().isNotFound()
107               .expectBody()
108               .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
109               .jsonPath("$.chatroomId").isEqualTo(otherChatRoomId));
110   }
111 }