1 package de.juplo.kafka.chat.backend.api;
3 import de.juplo.kafka.chat.backend.domain.*;
4 import lombok.extern.slf4j.Slf4j;
5 import org.junit.jupiter.api.Disabled;
6 import org.junit.jupiter.api.DisplayName;
7 import org.junit.jupiter.api.Test;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
10 import org.springframework.boot.test.context.SpringBootTest;
11 import org.springframework.boot.test.mock.mockito.MockBean;
12 import org.springframework.http.MediaType;
13 import org.springframework.test.web.reactive.server.WebTestClient;
15 import java.time.LocalDateTime;
16 import java.util.Optional;
17 import java.util.UUID;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
24 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
25 @AutoConfigureWebTestClient
27 public class ChatBackendControllerTest
34 @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
35 void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
38 UUID chatroomId = UUID.randomUUID();
39 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
42 WebTestClient.ResponseSpec responseSpec = client
44 .uri("/list/{chatroomId}", chatroomId)
45 .accept(MediaType.APPLICATION_JSON)
49 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
55 @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
56 void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
59 UUID chatroomId = UUID.randomUUID();
60 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
63 WebTestClient.ResponseSpec responseSpec = client
65 .uri("/get/{chatroomId}", chatroomId)
66 .accept(MediaType.APPLICATION_JSON)
70 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
74 @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}")
75 void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
78 UUID chatroomId = UUID.randomUUID();
79 String username = "foo";
81 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
84 WebTestClient.ResponseSpec responseSpec = client
87 "/put/{chatroomId}/{username}/{messageId}",
92 .accept(MediaType.APPLICATION_JSON)
96 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
100 @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}")
101 void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
104 UUID chatroomId = UUID.randomUUID();
105 String username = "foo";
106 Long messageId = 66l;
107 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
110 WebTestClient.ResponseSpec responseSpec = client
113 "/get/{chatroomId}/{username}/{messageId}",
117 .accept(MediaType.APPLICATION_JSON)
121 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
125 @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
126 void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
129 UUID chatroomId = UUID.randomUUID();
130 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
133 WebTestClient.ResponseSpec responseSpec = client
135 .uri("/listen/{chatroomId}", chatroomId)
136 .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
140 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
143 private void assertProblemDetailsForUnknownChatroomException(
144 WebTestClient.ResponseSpec responseSpec,
148 .expectStatus().isNotFound()
150 .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
151 .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
155 @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
156 void testMessageMutationException(@Autowired WebTestClient client) throws Exception
159 UUID chatroomId = UUID.randomUUID();
160 String username = "foo";
161 Long messageId = 66l;
162 ChatRoom chatRoom = mock(ChatRoom.class);
163 when(chatHome.getChatroom(any(UUID.class)))
164 .thenReturn(Optional.of(chatRoom));
165 Message.MessageKey key = Message.MessageKey.of("foo", 1l);
166 LocalDateTime timestamp = LocalDateTime.now();
167 Message mutated = new Message(key, 0l, timestamp, "Mutated!");
168 Message existing = new Message(key, 0l, timestamp, "Existing");
169 when(chatRoom.addMessage(any(Long.class), any(String.class), any(String.class)))
170 .thenThrow(new MessageMutationException(mutated, existing));
176 "/put/{chatroomId}/{username}/{messageId}",
181 .accept(MediaType.APPLICATION_JSON)
184 .expectStatus().is4xxClientError()
186 .jsonPath("$.type").isEqualTo("/problem/message-mutation")
187 .jsonPath("$.mutatedMessage.text").isEqualTo("Mutated!")
188 .jsonPath("$.existingMessage.text").isEqualTo("Existing");