feat: Implemented problem-details for `UnknownChatroomException`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / api / ChatBackendControllerTest.java
1 package de.juplo.kafka.chat.backend.api;
2
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;
14
15 import java.util.Optional;
16 import java.util.UUID;
17
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.when;
20
21
22 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
23 @AutoConfigureWebTestClient
24 @Slf4j
25 public class ChatBackendControllerTest
26 {
27   @MockBean
28   ChatHome chatHome;
29
30   @Disabled
31   @Test
32   @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
33   void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
34   {
35     // Given
36     UUID chatroomId = UUID.randomUUID();
37     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
38
39     // When
40     WebTestClient.ResponseSpec responseSpec = client
41         .get()
42         .uri("/list/{chatroomId}", chatroomId)
43         .accept(MediaType.APPLICATION_JSON)
44         .exchange();
45
46     // Then
47     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
48   }
49
50
51   @Disabled
52   @Test
53   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
54   void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
55   {
56     // Given
57     UUID chatroomId = UUID.randomUUID();
58     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
59
60     // When
61     WebTestClient.ResponseSpec responseSpec = client
62         .get()
63         .uri("/get/{chatroomId}", chatroomId)
64         .accept(MediaType.APPLICATION_JSON)
65         .exchange();
66
67     // Then
68     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
69   }
70
71   @Test
72   @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}")
73   void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
74   {
75     // Given
76     UUID chatroomId = UUID.randomUUID();
77     String username = "foo";
78     Long messageId = 66l;
79     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
80
81     // When
82     WebTestClient.ResponseSpec responseSpec = client
83         .put()
84         .uri(
85             "/put/{chatroomId}/{username}/{messageId}",
86             chatroomId,
87             username,
88             messageId)
89         .bodyValue("bar")
90         .accept(MediaType.APPLICATION_JSON)
91         .exchange();
92
93     // Then
94     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
95   }
96
97   @Test
98   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}")
99   void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
100   {
101     // Given
102     UUID chatroomId = UUID.randomUUID();
103     String username = "foo";
104     Long messageId = 66l;
105     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
106
107     // When
108     WebTestClient.ResponseSpec responseSpec = client
109         .get()
110         .uri(
111             "/get/{chatroomId}/{username}/{messageId}",
112             chatroomId,
113             username,
114             messageId)
115         .accept(MediaType.APPLICATION_JSON)
116         .exchange();
117
118     // Then
119     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
120   }
121
122   @Test
123   @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
124   void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
125   {
126     // Given
127     UUID chatroomId = UUID.randomUUID();
128     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
129
130     // When
131     WebTestClient.ResponseSpec responseSpec = client
132         .get()
133         .uri("/listen/{chatroomId}", chatroomId)
134         .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
135         .exchange();
136
137     // Then
138     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
139   }
140
141   private void assertProblemDetailsForUnknownChatroomException(
142       WebTestClient.ResponseSpec responseSpec,
143       UUID chatroomId)
144   {
145     responseSpec
146         .expectStatus().isNotFound()
147         .expectBody()
148         .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
149         .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
150   }
151 }