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(
57                   "http://localhost:{port}/{chatRoomId}",
58                   port,
59                   chatRoomId)
60               .accept(MediaType.APPLICATION_JSON)
61               .exchange()
62               .expectStatus().isOk()
63               .expectBody().jsonPath("$.name").isEqualTo("FOO");
64           webTestClient
65               .get()
66               .uri(
67                   "http://localhost:{port}/{chatRoomId}/ute/1",
68                   port,
69                   chatRoomId)
70               .accept(MediaType.APPLICATION_JSON)
71               .exchange()
72               .expectStatus().isOk()
73               .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
74           webTestClient
75               .get()
76               .uri(
77                   "http://localhost:{port}/{chatRoomId}/peter/1",
78                   port,
79                   chatRoomId)
80               .accept(MediaType.APPLICATION_JSON)
81               .exchange()
82               .expectStatus().isOk()
83               .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
84         });
85   }
86
87   @Test
88   @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
89   void testNotFoundForPutMessageToNonExistentChatRoom()
90   {
91     String otherChatRoomId = "7f59ec77-832e-4a17-8d22-55ef46242c17";
92
93     Awaitility
94         .await()
95         .atMost(Duration.ofSeconds(15))
96         .untilAsserted(() ->
97         {
98           webTestClient
99               .put()
100               .uri(
101                   "http://localhost:{port}/{chatRoomId}/otto/66",
102                   port,
103                   otherChatRoomId)
104               .contentType(MediaType.TEXT_PLAIN)
105               .accept(MediaType.APPLICATION_JSON)
106               .bodyValue("The devil rules route 66")
107               .exchange()
108               .expectStatus().isNotFound()
109               .expectBody()
110                 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
111                 .jsonPath("$.chatroomId").isEqualTo(otherChatRoomId);
112         });
113   }
114 }