refactor: Streamlined the API of the services
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / api / ChatBackendControllerTest.java
index 4392968..404fab2 100644 (file)
@@ -2,7 +2,6 @@ package de.juplo.kafka.chat.backend.api;
 
 import de.juplo.kafka.chat.backend.domain.*;
 import lombok.extern.slf4j.Slf4j;
-import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -11,11 +10,14 @@ import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.http.MediaType;
 import org.springframework.test.web.reactive.server.WebTestClient;
+import reactor.core.publisher.Mono;
 
+import java.time.LocalDateTime;
 import java.util.Optional;
 import java.util.UUID;
 
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 
@@ -27,14 +29,14 @@ public class ChatBackendControllerTest
   @MockBean
   ChatHome chatHome;
 
-  @Disabled
   @Test
   @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
   void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
+    when(chatHome.getChatroom(any(UUID.class)))
+        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -48,14 +50,14 @@ public class ChatBackendControllerTest
   }
 
 
-  @Disabled
   @Test
   @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}")
   void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
+    when(chatHome.getChatroom(any(UUID.class)))
+        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -76,7 +78,8 @@ public class ChatBackendControllerTest
     UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
+    when(chatHome.getChatroom(any(UUID.class)))
+        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -102,7 +105,8 @@ public class ChatBackendControllerTest
     UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
+    when(chatHome.getChatroom(any(UUID.class)))
+        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -125,13 +129,14 @@ public class ChatBackendControllerTest
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty());
+    when(chatHome.getChatroom(any(UUID.class)))
+        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
         .get()
         .uri("/listen/{chatroomId}", chatroomId)
-        .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
+        // .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) << TODO: Does not work!
         .exchange();
 
     // Then
@@ -148,4 +153,41 @@ public class ChatBackendControllerTest
         .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom")
         .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString());
   }
+
+  @Test
+  @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
+  void testMessageMutationException(@Autowired WebTestClient client) throws Exception
+  {
+    // Given
+    UUID chatroomId = UUID.randomUUID();
+    String username = "foo";
+    Long messageId = 66l;
+    ChatRoom chatRoom = mock(ChatRoom.class);
+    when(chatHome.getChatroom(any(UUID.class)))
+        .thenReturn(Mono.just(chatRoom));
+    Message.MessageKey key = Message.MessageKey.of("foo", 1l);
+    LocalDateTime timestamp = LocalDateTime.now();
+    Message mutated = new Message(key, 0l, timestamp, "Mutated!");
+    Message existing = new Message(key, 0l, timestamp, "Existing");
+    when(chatRoom.addMessage(any(Long.class), any(String.class), any(String.class)))
+        .thenThrow(new MessageMutationException(mutated, existing));
+
+    // When
+    client
+        .put()
+        .uri(
+            "/put/{chatroomId}/{username}/{messageId}",
+            chatroomId,
+            username,
+            messageId)
+        .bodyValue("bar")
+        .accept(MediaType.APPLICATION_JSON)
+        .exchange()
+        // Then
+        .expectStatus().is4xxClientError()
+        .expectBody()
+        .jsonPath("$.type").isEqualTo("/problem/message-mutation")
+        .jsonPath("$.mutatedMessage.text").isEqualTo("Mutated!")
+        .jsonPath("$.existingMessage.text").isEqualTo("Existing");
+  }
 }