feat: Moved persistence-logic from `ChatHome` into `ChatHomeService`
[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.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;
14
15 import java.time.LocalDateTime;
16 import java.util.UUID;
17
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21
22
23 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
24 @AutoConfigureWebTestClient
25 @Slf4j
26 public class ChatBackendControllerTest
27 {
28   @MockBean
29   ChatHome chatHome;
30
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)))
38         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
39
40     // When
41     WebTestClient.ResponseSpec responseSpec = client
42         .get()
43         .uri("/list/{chatroomId}", chatroomId)
44         .accept(MediaType.APPLICATION_JSON)
45         .exchange();
46
47     // Then
48     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
49   }
50
51
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)))
59         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
60
61     // When
62     WebTestClient.ResponseSpec responseSpec = client
63         .get()
64         .uri("/get/{chatroomId}", chatroomId)
65         .accept(MediaType.APPLICATION_JSON)
66         .exchange();
67
68     // Then
69     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
70   }
71
72   @Test
73   @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}")
74   void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
75   {
76     // Given
77     UUID chatroomId = UUID.randomUUID();
78     String username = "foo";
79     Long messageId = 66l;
80     when(chatHome.getChatRoom(any(UUID.class)))
81         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
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)))
108         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
109
110     // When
111     WebTestClient.ResponseSpec responseSpec = client
112         .get()
113         .uri(
114             "/get/{chatroomId}/{username}/{messageId}",
115             chatroomId,
116             username,
117             messageId)
118         .accept(MediaType.APPLICATION_JSON)
119         .exchange();
120
121     // Then
122     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
123   }
124
125   @Test
126   @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
127   void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
128   {
129     // Given
130     UUID chatroomId = UUID.randomUUID();
131     when(chatHome.getChatRoom(any(UUID.class)))
132         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
133
134     // When
135     WebTestClient.ResponseSpec responseSpec = client
136         .get()
137         .uri("/listen/{chatroomId}", chatroomId)
138         // .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) << TODO: Does not work!
139         .exchange();
140
141     // Then
142     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
143   }
144
145   private void assertProblemDetailsForUnknownChatroomException(
146       WebTestClient.ResponseSpec responseSpec,
147       UUID chatroomId)
148   {
149     responseSpec
150         .expectStatus().isNotFound()
151         .expectBody()
152         .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
153         .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
154   }
155
156   @Test
157   @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
158   void testMessageMutationException(@Autowired WebTestClient client) throws Exception
159   {
160     // Given
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!")));
172
173     // When
174     client
175         .put()
176         .uri(
177             "/put/{chatroomId}/{username}/{messageId}",
178             chatroomId,
179             username,
180             messageId)
181         .bodyValue("bar")
182         .accept(MediaType.APPLICATION_JSON)
183         .exchange()
184         // Then
185         .expectStatus().is4xxClientError()
186         .expectBody()
187         .jsonPath("$.type").isEqualTo("/problem/message-mutation")
188         .jsonPath("$.existingMessage.text").isEqualTo("Existing")
189         .jsonPath("$.mutatedText").isEqualTo("Mutated!");
190   }
191 }