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;
18 import java.util.concurrent.atomic.AtomicBoolean;
20 import static org.assertj.core.api.Assertions.assertThat;
21 import static org.hamcrest.Matchers.endsWith;
25 public abstract class AbstractConfigurationIT
27 final static String EXISTING_CHATROOM = "5c73531c-6fc4-426c-adcb-afc5c140a0f7";
28 String NONEXISTENT_CHATROOM = "7f59ec77-832e-4a17-8d22-55ef46242c17";
34 WebTestClient webTestClient;
36 ObjectMapper objectMapper;
44 .atMost(Duration.ofSeconds(15))
50 "http://localhost:{port}/actuator/health",
53 .expectStatus().isOk()
54 .expectBody().jsonPath("$.status").isEqualTo("UP");
59 @DisplayName("Restored chat-rooms can be listed")
60 void testRestoredChatRoomsCanBeListed()
64 .atMost(Duration.ofSeconds(15))
67 AtomicBoolean existingChatRoomFound = new AtomicBoolean(false);
71 "http://localhost:{port}/list",
73 .accept(MediaType.APPLICATION_JSON)
75 .expectStatus().isOk()
76 .returnResult(ChatRoomInfoTo.class)
79 .forEach(chatRoomInfoTo ->
81 log.debug("Inspecting chat-room {}", chatRoomInfoTo);
82 if (chatRoomInfoTo.getId().equals(UUID.fromString(EXISTING_CHATROOM)))
84 log.debug("Found existing chat-room {}", chatRoomInfoTo);
85 existingChatRoomFound.set(true);
86 assertThat(chatRoomInfoTo.getName().equals("FOO"));
89 assertThat(existingChatRoomFound.get()).isTrue();
94 @DisplayName("Details as expected for restored chat-room")
95 void testRestoredChatRoomHasExpectedDetails()
99 .atMost(Duration.ofSeconds(15))
105 "http://localhost:{port}/{chatRoomId}",
108 .accept(MediaType.APPLICATION_JSON)
110 .expectStatus().isOk()
111 .expectBody().jsonPath("$.name").isEqualTo("FOO");
116 @DisplayName("Restored message from Ute has expected Text")
117 void testRestoredMessageForUteHasExpectedText()
121 .atMost(Duration.ofSeconds(15))
127 "http://localhost:{port}/{chatRoomId}/ute/1",
130 .accept(MediaType.APPLICATION_JSON)
132 .expectStatus().isOk()
133 .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
138 @DisplayName("Restored message from Peter has expected Text")
139 void testRestoredMessageForPeterHasExpectedText()
143 .atMost(Duration.ofSeconds(15))
149 "http://localhost:{port}/{chatRoomId}/peter/1",
152 .accept(MediaType.APPLICATION_JSON)
154 .expectStatus().isOk()
155 .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
160 @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
161 void testNotFoundForPutMessageToNonExistentChatRoom()
165 .atMost(Duration.ofSeconds(15))
171 "http://localhost:{port}/{chatRoomId}/otto/66",
173 NONEXISTENT_CHATROOM)
174 .contentType(MediaType.TEXT_PLAIN)
175 .accept(MediaType.APPLICATION_JSON)
176 .bodyValue("The devil rules route 66")
178 .expectStatus().isNotFound()
180 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
181 .jsonPath("$.chatroomId").isEqualTo(NONEXISTENT_CHATROOM);
186 @DisplayName("A message can be put into a newly created chat-room")
187 void testPutMessageInNewChatRoom() throws IOException
191 .atMost(Duration.ofSeconds(15))
194 byte[] result = webTestClient
196 .uri("http://localhost:{port}/create", port)
197 .contentType(MediaType.TEXT_PLAIN)
199 .accept(MediaType.APPLICATION_JSON)
201 .expectStatus().isOk()
203 .jsonPath("$.id").exists()
204 .jsonPath("$.name").isEqualTo("bar")
205 // The hard must not be asserted, because not all implementations ar aware of it
206 // .jsonPath("$.shard").isEqualTo(Integer.valueOf(2))
209 ChatRoomInfoTo chatRoomInfo = objectMapper.readValue(result, ChatRoomInfoTo.class);
210 UUID chatRoomId = chatRoomInfo.getId();
214 "http://localhost:{port}/{chatRoomId}/nerd/7",
217 .contentType(MediaType.TEXT_PLAIN)
218 .accept(MediaType.APPLICATION_JSON)
219 .bodyValue("Hello world!")
221 .expectStatus().isOk()
223 .jsonPath("$.id").isEqualTo(Integer.valueOf(7))
224 .jsonPath("$.user").isEqualTo("nerd")
225 .jsonPath("$.text").isEqualTo("Hello world!");