1 package de.juplo.kafka.chat.backend;
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;
15 import java.io.IOException;
16 import java.time.Duration;
17 import java.util.UUID;
19 import static org.hamcrest.Matchers.endsWith;
23 public abstract class AbstractConfigurationIT
25 final static String EXISTING_CHATROOM = "5c73531c-6fc4-426c-adcb-afc5c140a0f7";
26 String NONEXISTENT_CHATROOM = "7f59ec77-832e-4a17-8d22-55ef46242c17";
32 WebTestClient webTestClient;
34 ObjectMapper objectMapper;
42 .atMost(Duration.ofSeconds(15))
47 "http://localhost:{port}/actuator/health",
50 .expectStatus().isOk()
51 .expectBody().jsonPath("$.status").isEqualTo("UP"));
55 @DisplayName("Restored chat-rooms can be listed")
56 void testRestoredChatRoomsCanBeListed()
60 .atMost(Duration.ofSeconds(15))
65 "http://localhost:{port}/list",
67 .accept(MediaType.APPLICATION_JSON)
69 .expectStatus().isOk()
71 .jsonPath("$.length()").isEqualTo(1)
72 .jsonPath("$[0].name").isEqualTo("FOO"));
76 @DisplayName("Details as expected for restored chat-room")
77 void testRestoredChatRoomHasExpectedDetails()
81 .atMost(Duration.ofSeconds(15))
86 "http://localhost:{port}/{chatRoomId}",
89 .accept(MediaType.APPLICATION_JSON)
91 .expectStatus().isOk()
92 .expectBody().jsonPath("$.name").isEqualTo("FOO"));
96 @DisplayName("Restored message from Ute has expected Text")
97 void testRestoredMessageForUteHasExpectedText()
101 .atMost(Duration.ofSeconds(15))
106 "http://localhost:{port}/{chatRoomId}/ute/1",
109 .accept(MediaType.APPLICATION_JSON)
111 .expectStatus().isOk()
112 .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute..."));
116 @DisplayName("Restored message from Peter has expected Text")
117 void testRestoredMessageForPeterHasExpectedText()
121 .atMost(Duration.ofSeconds(15))
126 "http://localhost:{port}/{chatRoomId}/peter/1",
129 .accept(MediaType.APPLICATION_JSON)
131 .expectStatus().isOk()
132 .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!"));
136 @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
137 void testNotFoundForPutMessageToNonExistentChatRoom()
141 .atMost(Duration.ofSeconds(15))
146 "http://localhost:{port}/{chatRoomId}/otto/66",
148 NONEXISTENT_CHATROOM)
149 .contentType(MediaType.TEXT_PLAIN)
150 .accept(MediaType.APPLICATION_JSON)
151 .bodyValue("The devil rules route 66")
153 .expectStatus().isNotFound()
155 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
156 .jsonPath("$.chatroomId").isEqualTo(NONEXISTENT_CHATROOM));
160 @DisplayName("A message can be put into a newly created chat-room")
161 void testPutMessageInNewChatRoom() throws IOException
165 .atMost(Duration.ofSeconds(15))
168 byte[] result = webTestClient
170 .uri("http://localhost:{port}/create", port)
171 .contentType(MediaType.TEXT_PLAIN)
173 .accept(MediaType.APPLICATION_JSON)
175 .expectStatus().isOk()
177 .jsonPath("$.id").exists()
178 .jsonPath("$.name").isEqualTo("bar")
179 .jsonPath("$.shard").isEqualTo(Integer.valueOf(2))
182 ChatRoomInfoTo chatRoomInfo = objectMapper.readValue(result, ChatRoomInfoTo.class);
183 UUID chatRoomId = chatRoomInfo.getId();
187 "http://localhost:{port}/{chatRoomId}/nerd/7",
190 .contentType(MediaType.TEXT_PLAIN)
191 .accept(MediaType.APPLICATION_JSON)
192 .bodyValue("Hello world!")
194 .expectStatus().isOk()
196 .jsonPath("$.id").isEqualTo(Integer.valueOf(7))
197 .jsonPath("$.user").isEqualTo("nerd")
198 .jsonPath("$.text").isEqualTo("Hello world!");