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()
61 "http://localhost:{port}/list",
63 .accept(MediaType.APPLICATION_JSON)
65 .expectStatus().isOk()
67 .jsonPath("$.length()").isEqualTo(1)
68 .jsonPath("$[0].name").isEqualTo("FOO");
72 @DisplayName("Details as expected for restored chat-room")
73 void testRestoredChatRoomHasExpectedDetails()
78 "http://localhost:{port}/{chatRoomId}",
81 .accept(MediaType.APPLICATION_JSON)
83 .expectStatus().isOk()
84 .expectBody().jsonPath("$.name").isEqualTo("FOO");
88 @DisplayName("Restored message from Ute has expected Text")
89 void testRestoredMessageForUteHasExpectedText()
94 "http://localhost:{port}/{chatRoomId}/ute/1",
97 .accept(MediaType.APPLICATION_JSON)
99 .expectStatus().isOk()
100 .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute...");
104 @DisplayName("Restored message from Peter has expected Text")
105 void testRestoredMessageForPeterHasExpectedText()
110 "http://localhost:{port}/{chatRoomId}/peter/1",
113 .accept(MediaType.APPLICATION_JSON)
115 .expectStatus().isOk()
116 .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!");
120 @DisplayName("A PUT-message for a non-existent chat-room yields 404 NOT FOUND")
121 void testNotFoundForPutMessageToNonExistentChatRoom()
125 .atMost(Duration.ofSeconds(15))
130 "http://localhost:{port}/{chatRoomId}/otto/66",
132 NONEXISTENT_CHATROOM)
133 .contentType(MediaType.TEXT_PLAIN)
134 .accept(MediaType.APPLICATION_JSON)
135 .bodyValue("The devil rules route 66")
137 .expectStatus().isNotFound()
139 .jsonPath("$.type").value(endsWith("/problem/unknown-chatroom"))
140 .jsonPath("$.chatroomId").isEqualTo(NONEXISTENT_CHATROOM));
144 @DisplayName("A message can be put into a newly created chat-room")
145 void testPutMessageInNewChatRoom() throws IOException
149 .atMost(Duration.ofSeconds(15))
152 byte[] result = webTestClient
154 .uri("http://localhost:{port}/create", port)
155 .contentType(MediaType.TEXT_PLAIN)
157 .accept(MediaType.APPLICATION_JSON)
159 .expectStatus().isOk()
161 .jsonPath("$.id").exists()
162 .jsonPath("$.name").isEqualTo("bar")
163 .jsonPath("$.shard").isEqualTo(Integer.valueOf(2))
166 ChatRoomInfoTo chatRoomInfo = objectMapper.readValue(result, ChatRoomInfoTo.class);
167 UUID chatRoomId = chatRoomInfo.getId();
171 "http://localhost:{port}/{chatRoomId}/nerd/7",
174 .contentType(MediaType.TEXT_PLAIN)
175 .accept(MediaType.APPLICATION_JSON)
176 .bodyValue("Hello world!")
178 .expectStatus().isOk()
180 .jsonPath("$.id").isEqualTo(Integer.valueOf(7))
181 .jsonPath("$.user").isEqualTo("nerd")
182 .jsonPath("$.text").isEqualTo("Hello world!");