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.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
33 @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
34 void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
37 UUID chatroomId = UUID.randomUUID();
38 when(chatHome.getChatroom(any(UUID.class)))
39 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
42 WebTestClient.ResponseSpec responseSpec = client
44 .uri("/list/{chatroomId}", chatroomId)
45 .accept(MediaType.APPLICATION_JSON)
49 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
54 @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
55 void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
58 UUID chatroomId = UUID.randomUUID();
59 when(chatHome.getChatroom(any(UUID.class)))
60 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
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)))
82 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
85 WebTestClient.ResponseSpec responseSpec = client
88 "/put/{chatroomId}/{username}/{messageId}",
93 .accept(MediaType.APPLICATION_JSON)
97 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
101 @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}")
102 void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
105 UUID chatroomId = UUID.randomUUID();
106 String username = "foo";
107 Long messageId = 66l;
108 when(chatHome.getChatroom(any(UUID.class)))
109 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
112 WebTestClient.ResponseSpec responseSpec = client
115 "/get/{chatroomId}/{username}/{messageId}",
119 .accept(MediaType.APPLICATION_JSON)
123 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
127 @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
128 void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
131 UUID chatroomId = UUID.randomUUID();
132 when(chatHome.getChatroom(any(UUID.class)))
133 .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
136 WebTestClient.ResponseSpec responseSpec = client
138 .uri("/listen/{chatroomId}", chatroomId)
139 // .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) << TODO: Does not work!
143 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
146 private void assertProblemDetailsForUnknownChatroomException(
147 WebTestClient.ResponseSpec responseSpec,
151 .expectStatus().isNotFound()
153 .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
154 .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
158 @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
159 void testMessageMutationException(@Autowired WebTestClient client) throws Exception
162 UUID chatroomId = UUID.randomUUID();
163 String username = "foo";
164 Long messageId = 66l;
165 ChatRoom chatRoom = mock(ChatRoom.class);
166 when(chatHome.getChatroom(any(UUID.class)))
167 .thenReturn(Mono.just(chatRoom));
168 Message.MessageKey key = Message.MessageKey.of("foo", 1l);
169 LocalDateTime timestamp = LocalDateTime.now();
170 Message mutated = new Message(key, 0l, timestamp, "Mutated!");
171 Message existing = new Message(key, 0l, timestamp, "Existing");
172 when(chatRoom.addMessage(any(Long.class), any(String.class), any(String.class)))
173 .thenThrow(new MessageMutationException(mutated, existing));
179 "/put/{chatroomId}/{username}/{messageId}",
184 .accept(MediaType.APPLICATION_JSON)
187 .expectStatus().is4xxClientError()
189 .jsonPath("$.type").isEqualTo("/problem/message-mutation")
190 .jsonPath("$.mutatedMessage.text").isEqualTo("Mutated!")
191 .jsonPath("$.existingMessage.text").isEqualTo("Existing");