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.annotation.DirtiesContext;
13 import org.springframework.test.web.reactive.server.WebTestClient;
14 import org.testcontainers.shaded.org.awaitility.Awaitility;
16 import java.io.IOException;
17 import java.time.Duration;
18 import java.util.UUID;
19 import java.util.concurrent.atomic.AtomicBoolean;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.hamcrest.Matchers.endsWith;
27 public abstract class AbstractConfigurationIT
29 final static String EXISTING_CHATROOM = "5c73531c-6fc4-426c-adcb-afc5c140a0f7";
30 String NONEXISTENT_CHATROOM = "7f59ec77-832e-4a17-8d22-55ef46242c17";
36 WebTestClient webTestClient;
38 ObjectMapper objectMapper;
46 .atMost(Duration.ofSeconds(15))
52 "http://localhost:{port}/actuator/health",
55 .expectStatus().isOk()
56 .expectBody().jsonPath("$.status").isEqualTo("UP");
61 @DisplayName("Restored chat-rooms can be listed")
62 void testRestoredChatRoomsCanBeListed()
66 .atMost(Duration.ofSeconds(15))
69 AtomicBoolean existingChatRoomFound = new AtomicBoolean(false);
73 "http://localhost:{port}/list",
75 .accept(MediaType.APPLICATION_JSON)
77 .expectStatus().isOk()
78 .returnResult(ChatRoomInfoTo.class)
81 .forEach(chatRoomInfoTo ->
83 log.debug("Inspecting chat-room {}", chatRoomInfoTo);
84 if (chatRoomInfoTo.getId().equals(UUID.fromString(EXISTING_CHATROOM)))
86 log.debug("Found existing chat-room {}", chatRoomInfoTo);
87 existingChatRoomFound.set(true);
88 assertThat(chatRoomInfoTo.getName().equals("FOO"));
91 assertThat(existingChatRoomFound.get()).isTrue();
96 @DisplayName("Details as expected for restored chat-room")
97 void testRestoredChatRoomHasExpectedDetails()
101 .atMost(Duration.ofSeconds(15))
107 "http://localhost:{port}/{chatRoomId}",
110 .accept(MediaType.APPLICATION_JSON)
112 .expectStatus().isOk()
113 .expectBody().jsonPath("$.name").isEqualTo("FOO");
118 @DisplayName("Restored message from Ute has expected Text")
119 void testRestoredMessageForUteHasExpectedText()
123 .atMost(Duration.ofSeconds(15))
129 "http://localhost:{port}/{chatRoomId}/ute/1",
132 .accept(MediaType.APPLICATION_JSON)
134 .expectStatus().isOk()
135 .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
140 @DisplayName("Restored message from Peter has expected Text")
141 void testRestoredMessageForPeterHasExpectedText()
145 .atMost(Duration.ofSeconds(15))
151 "http://localhost:{port}/{chatRoomId}/peter/1",
154 .accept(MediaType.APPLICATION_JSON)
156 .expectStatus().isOk()
157 .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
162 @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
163 void testNotFoundForPutMessageToNonExistentChatRoom()
167 .atMost(Duration.ofSeconds(15))
173 "http://localhost:{port}/{chatRoomId}/otto/66",
175 NONEXISTENT_CHATROOM)
176 .contentType(MediaType.TEXT_PLAIN)
177 .accept(MediaType.APPLICATION_JSON)
178 .bodyValue("The devil rules route 66")
180 .expectStatus().isNotFound()
182 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
183 .jsonPath("$.chatroomId").isEqualTo(NONEXISTENT_CHATROOM);
188 @DisplayName("A message can be put into a newly created chat-room")
189 void testPutMessageInNewChatRoom() throws IOException
191 ChatRoomInfoTo chatRoomInfo;
194 // The first request creates a new chat-room
195 // It must be repeated, until a chat-room was created,
196 // that is owned by the instance
197 chatRoomInfo = webTestClient
199 .uri("http://localhost:{port}/create", port)
200 .contentType(MediaType.TEXT_PLAIN)
202 .accept(MediaType.APPLICATION_JSON)
204 .returnResult(ChatRoomInfoTo.class)
209 while(!(chatRoomInfo.getShard() == null || chatRoomInfo.getShard().intValue() == 2));
211 UUID chatRoomId = chatRoomInfo.getId();
215 .atMost(Duration.ofSeconds(15))
221 "http://localhost:{port}/{chatRoomId}/nerd/7",
224 .contentType(MediaType.TEXT_PLAIN)
225 .accept(MediaType.APPLICATION_JSON)
226 .bodyValue("Hello world!")
228 .expectStatus().isOk()
230 .jsonPath("$.id").isEqualTo(Integer.valueOf(7))
231 .jsonPath("$.user").isEqualTo("nerd")
232 .jsonPath("$.text").isEqualTo("Hello world!");