refactor: Splitted test in `AbstractConfigurationIT` into smaler pieces
[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.BeforeEach;
4 import org.junit.jupiter.api.DisplayName;
5 import org.junit.jupiter.api.Test;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.web.server.LocalServerPort;
8 import org.springframework.http.MediaType;
9 import org.springframework.test.web.reactive.server.WebTestClient;
10 import org.testcontainers.shaded.org.awaitility.Awaitility;
11
12 import java.time.Duration;
13
14 import static org.hamcrest.Matchers.endsWith;
15
16
17 public abstract class AbstractConfigurationIT
18 {
19   final static String EXISTING_CHATROOM = "5c73531c-6fc4-426c-adcb-afc5c140a0f7";
20
21
22   @LocalServerPort
23   int port;
24   @Autowired
25   WebTestClient webTestClient;
26
27
28   @BeforeEach
29   void waitForApp()
30   {
31     Awaitility
32         .await()
33         .atMost(Duration.ofSeconds(15))
34         .untilAsserted(() ->
35         {
36           webTestClient
37               .get()
38               .uri(
39                   "http://localhost:{port}/actuator/health",
40                   port)
41               .exchange()
42               .expectStatus().isOk()
43               .expectBody().jsonPath("$.status").isEqualTo("UP");
44         });
45   }
46
47   @Test
48   @DisplayName("Restored chat-rooms can be listed")
49   void testRestoredChatRoomsCanBeListed()
50   {
51     Awaitility
52         .await()
53         .atMost(Duration.ofSeconds(15))
54         .untilAsserted(() ->
55         {
56           webTestClient
57               .get()
58               .uri(
59                   "http://localhost:{port}/list",
60                   port)
61               .accept(MediaType.APPLICATION_JSON)
62               .exchange()
63               .expectStatus().isOk()
64               .expectBody()
65                 .jsonPath("$.length()").isEqualTo(1)
66                 .jsonPath("$[0].name").isEqualTo("FOO");
67         });
68   }
69
70   @Test
71   @DisplayName("Details as expected for restored chat-room")
72   void testRestoredChatRoomHasExpectedDetails()
73   {
74     Awaitility
75         .await()
76         .atMost(Duration.ofSeconds(15))
77         .untilAsserted(() ->
78         {
79           webTestClient
80               .get()
81               .uri(
82                   "http://localhost:{port}/{chatRoomId}",
83                   port,
84                   EXISTING_CHATROOM)
85               .accept(MediaType.APPLICATION_JSON)
86               .exchange()
87               .expectStatus().isOk()
88               .expectBody().jsonPath("$.name").isEqualTo("FOO");
89         });
90   }
91
92   @Test
93   @DisplayName("Restored message from Ute has expected Text")
94   void testRestoredMessageForUteHasExpectedText()
95   {
96     Awaitility
97         .await()
98         .atMost(Duration.ofSeconds(15))
99         .untilAsserted(() ->
100         {
101           webTestClient
102               .get()
103               .uri(
104                   "http://localhost:{port}/{chatRoomId}/ute/1",
105                   port,
106                   EXISTING_CHATROOM)
107               .accept(MediaType.APPLICATION_JSON)
108               .exchange()
109               .expectStatus().isOk()
110               .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
111         });
112   }
113
114   @Test
115   @DisplayName("Restored message from Peter has expected Text")
116   void testRestoredMessageForPeterHasExpectedText()
117   {
118     Awaitility
119         .await()
120         .atMost(Duration.ofSeconds(15))
121         .untilAsserted(() ->
122         {
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
136   @Test
137   @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
138   void testNotFoundForPutMessageToNonExistentChatRoom()
139   {
140     String otherChatRoomId = "7f59ec77-832e-4a17-8d22-55ef46242c17";
141
142     Awaitility
143         .await()
144         .atMost(Duration.ofSeconds(15))
145         .untilAsserted(() ->
146         {
147           webTestClient
148               .put()
149               .uri(
150                   "http://localhost:{port}/{chatRoomId}/otto/66",
151                   port,
152                   otherChatRoomId)
153               .contentType(MediaType.TEXT_PLAIN)
154               .accept(MediaType.APPLICATION_JSON)
155               .bodyValue("The devil rules route 66")
156               .exchange()
157               .expectStatus().isNotFound()
158               .expectBody()
159                 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
160                 .jsonPath("$.chatroomId").isEqualTo(otherChatRoomId);
161         });
162   }
163 }