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           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   }
44
45   @Test
46   @DisplayName("Restored chat-rooms can be listed")
47   void testRestoredChatRoomsCanBeListed()
48   {
49     webTestClient
50         .get()
51         .uri(
52             "http://localhost:{port}/list",
53             port)
54         .accept(MediaType.APPLICATION_JSON)
55         .exchange()
56         .expectStatus().isOk()
57         .expectBody()
58         .jsonPath("$.length()").isEqualTo(1)
59         .jsonPath("$[0].name").isEqualTo("FOO");
60   }
61
62   @Test
63   @DisplayName("Details as expected for restored chat-room")
64   void testRestoredChatRoomHasExpectedDetails()
65   {
66     webTestClient
67         .get()
68         .uri(
69             "http://localhost:{port}/{chatRoomId}",
70             port,
71             EXISTING_CHATROOM)
72         .accept(MediaType.APPLICATION_JSON)
73         .exchange()
74         .expectStatus().isOk()
75         .expectBody().jsonPath("$.name").isEqualTo("FOO");
76   }
77
78   @Test
79   @DisplayName("Restored message from Ute has expected Text")
80   void testRestoredMessageForUteHasExpectedText()
81   {
82     webTestClient
83         .get()
84         .uri(
85             "http://localhost:{port}/{chatRoomId}/ute/1",
86             port,
87             EXISTING_CHATROOM)
88         .accept(MediaType.APPLICATION_JSON)
89         .exchange()
90         .expectStatus().isOk()
91         .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
92   }
93
94   @Test
95   @DisplayName("Restored message from Peter has expected Text")
96   void testRestoredMessageForPeterHasExpectedText()
97   {
98     webTestClient
99         .get()
100         .uri(
101             "http://localhost:{port}/{chatRoomId}/peter/1",
102             port,
103             EXISTING_CHATROOM)
104         .accept(MediaType.APPLICATION_JSON)
105         .exchange()
106         .expectStatus().isOk()
107         .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
108   }
109
110   @Test
111   @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
112   void testNotFoundForPutMessageToNonExistentChatRoom()
113   {
114     String otherChatRoomId = "7f59ec77-832e-4a17-8d22-55ef46242c17";
115
116     Awaitility
117         .await()
118         .atMost(Duration.ofSeconds(15))
119         .untilAsserted(() ->
120             webTestClient
121                 .put()
122                 .uri(
123                     "http://localhost:{port}/{chatRoomId}/otto/66",
124                     port,
125                     otherChatRoomId)
126                 .contentType(MediaType.TEXT_PLAIN)
127                 .accept(MediaType.APPLICATION_JSON)
128                 .bodyValue("The devil rules route 66")
129                 .exchange()
130                 .expectStatus().isNotFound()
131                 .expectBody()
132                 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
133                 .jsonPath("$.chatroomId").isEqualTo(otherChatRoomId));
134   }
135 }