refactor: Streamlined the API of the services
[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.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   @Test
33   @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
34   void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
35   {
36     // Given
37     UUID chatroomId = UUID.randomUUID();
38     when(chatHome.getChatroom(any(UUID.class)))
39         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
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   @Test
54   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
55   void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
56   {
57     // Given
58     UUID chatroomId = UUID.randomUUID();
59     when(chatHome.getChatroom(any(UUID.class)))
60         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
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)))
82         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
83
84     // When
85     WebTestClient.ResponseSpec responseSpec = client
86         .put()
87         .uri(
88             "/put/{chatroomId}/{username}/{messageId}",
89             chatroomId,
90             username,
91             messageId)
92         .bodyValue("bar")
93         .accept(MediaType.APPLICATION_JSON)
94         .exchange();
95
96     // Then
97     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
98   }
99
100   @Test
101   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}")
102   void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
103   {
104     // Given
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)));
110
111     // When
112     WebTestClient.ResponseSpec responseSpec = client
113         .get()
114         .uri(
115             "/get/{chatroomId}/{username}/{messageId}",
116             chatroomId,
117             username,
118             messageId)
119         .accept(MediaType.APPLICATION_JSON)
120         .exchange();
121
122     // Then
123     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
124   }
125
126   @Test
127   @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}")
128   void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
129   {
130     // Given
131     UUID chatroomId = UUID.randomUUID();
132     when(chatHome.getChatroom(any(UUID.class)))
133         .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
134
135     // When
136     WebTestClient.ResponseSpec responseSpec = client
137         .get()
138         .uri("/listen/{chatroomId}", chatroomId)
139         // .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) << TODO: Does not work!
140         .exchange();
141
142     // Then
143     assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId);
144   }
145
146   private void assertProblemDetailsForUnknownChatroomException(
147       WebTestClient.ResponseSpec responseSpec,
148       UUID chatroomId)
149   {
150     responseSpec
151         .expectStatus().isNotFound()
152         .expectBody()
153         .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
154         .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
155   }
156
157   @Test
158   @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
159   void testMessageMutationException(@Autowired WebTestClient client) throws Exception
160   {
161     // Given
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));
174
175     // When
176     client
177         .put()
178         .uri(
179             "/put/{chatroomId}/{username}/{messageId}",
180             chatroomId,
181             username,
182             messageId)
183         .bodyValue("bar")
184         .accept(MediaType.APPLICATION_JSON)
185         .exchange()
186         // Then
187         .expectStatus().is4xxClientError()
188         .expectBody()
189         .jsonPath("$.type").isEqualTo("/problem/message-mutation")
190         .jsonPath("$.mutatedMessage.text").isEqualTo("Mutated!")
191         .jsonPath("$.existingMessage.text").isEqualTo("Existing");
192   }
193 }