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