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.util.Optional;
16 import java.util.UUID;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.when;
22 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
23 @AutoConfigureWebTestClient
25 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))).thenReturn(Optional.empty());
40 WebTestClient.ResponseSpec responseSpec = client
42 .uri("/list/{chatroomId}", chatroomId)
43 .accept(MediaType.APPLICATION_JSON)
47 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))).thenReturn(Optional.empty());
61 WebTestClient.ResponseSpec responseSpec = client
63 .uri("/get/{chatroomId}", chatroomId)
64 .accept(MediaType.APPLICATION_JSON)
68 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
72 @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}")
73 void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
76 UUID chatroomId = UUID.randomUUID();
77 String username = "foo";
79 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
82 WebTestClient.ResponseSpec responseSpec = client
85 "/put/{chatroomId}/{username}/{messageId}",
90 .accept(MediaType.APPLICATION_JSON)
94 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
98 @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}")
99 void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
102 UUID chatroomId = UUID.randomUUID();
103 String username = "foo";
104 Long messageId = 66l;
105 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
108 WebTestClient.ResponseSpec responseSpec = client
111 "/get/{chatroomId}/{username}/{messageId}",
115 .accept(MediaType.APPLICATION_JSON)
119 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
123 @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
124 void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
127 UUID chatroomId = UUID.randomUUID();
128 when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
131 WebTestClient.ResponseSpec responseSpec = client
133 .uri("/listen/{chatroomId}", chatroomId)
134 .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
138 assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
141 private void assertProblemDetailsForUnknownChatroomException(
142 WebTestClient.ResponseSpec responseSpec,
146 .expectStatus().isNotFound()
148 .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
149 .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());