fix: Fixed generated problem-details for mutated messages
[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.time.LocalDateTime;
16 import java.util.Optional;
17 import java.util.UUID;
18
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22
23
24 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
25 @AutoConfigureWebTestClient
26 @Slf4j
27 public class ChatBackendControllerTest
28 {
29   @MockBean
30   ChatHome chatHome;
31
32   @Disabled
33   @Test
34   @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
35   void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
36   {
37     // Given
38     UUID chatroomId = UUID.randomUUID();
39     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
40
41     // When
42     WebTestClient.ResponseSpec responseSpec = client
43         .get()
44         .uri("/list/{chatroomId}", chatroomId)
45         .accept(MediaType.APPLICATION_JSON)
46         .exchange();
47
48     // Then
49     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
50   }
51
52
53   @Disabled
54   @Test
55   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
56   void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
57   {
58     // Given
59     UUID chatroomId = UUID.randomUUID();
60     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
61
62     // When
63     WebTestClient.ResponseSpec responseSpec = client
64         .get()
65         .uri("/get/{chatroomId}", chatroomId)
66         .accept(MediaType.APPLICATION_JSON)
67         .exchange();
68
69     // Then
70     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
71   }
72
73   @Test
74   @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}")
75   void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
76   {
77     // Given
78     UUID chatroomId = UUID.randomUUID();
79     String username = "foo";
80     Long messageId = 66l;
81     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
82
83     // When
84     WebTestClient.ResponseSpec responseSpec = client
85         .put()
86         .uri(
87             "/put/{chatroomId}/{username}/{messageId}",
88             chatroomId,
89             username,
90             messageId)
91         .bodyValue("bar")
92         .accept(MediaType.APPLICATION_JSON)
93         .exchange();
94
95     // Then
96     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
97   }
98
99   @Test
100   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}")
101   void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
102   {
103     // Given
104     UUID chatroomId = UUID.randomUUID();
105     String username = "foo";
106     Long messageId = 66l;
107     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
108
109     // When
110     WebTestClient.ResponseSpec responseSpec = client
111         .get()
112         .uri(
113             "/get/{chatroomId}/{username}/{messageId}",
114             chatroomId,
115             username,
116             messageId)
117         .accept(MediaType.APPLICATION_JSON)
118         .exchange();
119
120     // Then
121     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
122   }
123
124   @Test
125   @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
126   void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
127   {
128     // Given
129     UUID chatroomId = UUID.randomUUID();
130     when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
131
132     // When
133     WebTestClient.ResponseSpec responseSpec = client
134         .get()
135         .uri("/listen/{chatroomId}", chatroomId)
136         .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
137         .exchange();
138
139     // Then
140     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
141   }
142
143   private void assertProblemDetailsForUnknownChatroomException(
144       WebTestClient.ResponseSpec responseSpec,
145       UUID chatroomId)
146   {
147     responseSpec
148         .expectStatus().isNotFound()
149         .expectBody()
150         .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
151         .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
152   }
153
154   @Test
155   @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
156   void testMessageMutationException(@Autowired WebTestClient client) throws Exception
157   {
158     // Given
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));
171
172     // When
173     client
174         .put()
175         .uri(
176             "/put/{chatroomId}/{username}/{messageId}",
177             chatroomId,
178             username,
179             messageId)
180         .bodyValue("bar")
181         .accept(MediaType.APPLICATION_JSON)
182         .exchange()
183         // Then
184         .expectStatus().is4xxClientError()
185         .expectBody()
186         .jsonPath("$.type").isEqualTo("/problem/message-mutation")
187         .jsonPath("$.mutatedMessage.text").isEqualTo("Mutated!")
188         .jsonPath("$.existingMessage.text").isEqualTo("Existing");
189   }
190 }