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.DisplayName;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.boot.test.mock.mockito.MockBean;
11 import org.springframework.http.MediaType;
12 import org.springframework.test.web.reactive.server.WebTestClient;
13 import reactor.core.publisher.Mono;
15 import java.time.LocalDateTime;
16 import java.util.UUID;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
23 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
24 @AutoConfigureWebTestClient
26 public class ChatBackendControllerTest
32 @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
33 void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
36 UUID chatroomId = UUID.randomUUID();
37 when(chatHome.getChatroom(any(UUID.class)))
38 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
41 WebTestClient.ResponseSpec responseSpec = client
43 .uri("/list/{chatroomId}", chatroomId)
44 .accept(MediaType.APPLICATION_JSON)
48 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
53 @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
54 void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
57 UUID chatroomId = UUID.randomUUID();
58 when(chatHome.getChatroom(any(UUID.class)))
59 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
62 WebTestClient.ResponseSpec responseSpec = client
64 .uri("/get/{chatroomId}", chatroomId)
65 .accept(MediaType.APPLICATION_JSON)
69 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
73 @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}")
74 void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
77 UUID chatroomId = UUID.randomUUID();
78 String username = "foo";
80 when(chatHome.getChatroom(any(UUID.class)))
81 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
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)))
108 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
111 WebTestClient.ResponseSpec responseSpec = client
114 "/get/{chatroomId}/{username}/{messageId}",
118 .accept(MediaType.APPLICATION_JSON)
122 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
126 @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
127 void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
130 UUID chatroomId = UUID.randomUUID();
131 when(chatHome.getChatroom(any(UUID.class)))
132 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
135 WebTestClient.ResponseSpec responseSpec = client
137 .uri("/listen/{chatroomId}", chatroomId)
138 // .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) << TODO: Does not work!
142 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
145 private void assertProblemDetailsForUnknownChatroomException(
146 WebTestClient.ResponseSpec responseSpec,
150 .expectStatus().isNotFound()
152 .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
153 .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
157 @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
158 void testMessageMutationException(@Autowired WebTestClient client) throws Exception
161 UUID chatroomId = UUID.randomUUID();
162 String username = "foo";
163 Long messageId = 66l;
164 ChatRoom chatRoom = mock(ChatRoom.class);
165 when(chatHome.getChatroom(any(UUID.class)))
166 .thenReturn(Mono.just(chatRoom));
167 Message.MessageKey key = Message.MessageKey.of("foo", 1l);
168 LocalDateTime timestamp = LocalDateTime.now();
169 Message existing = new Message(key, 0l, timestamp, "Existing");
170 when(chatRoom.addMessage(any(Long.class), any(String.class), any(String.class)))
171 .thenReturn(Mono.error(() -> new MessageMutationException(existing, "Mutated!")));
177 "/put/{chatroomId}/{username}/{messageId}",
182 .accept(MediaType.APPLICATION_JSON)
185 .expectStatus().is4xxClientError()
187 .jsonPath("$.type").isEqualTo("/problem/message-mutation")
188 .jsonPath("$.existingMessage.text").isEqualTo("Existing")
189 .jsonPath("$.mutatedText").isEqualTo("Mutated!");