fix: Refined `ChatBackendControllerTest` and fixed a bug in `ChatRoom`
authorKai Moritz <kai@juplo.de>
Mon, 9 Jan 2023 19:51:20 +0000 (20:51 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 20:59:37 +0000 (21:59 +0100)
- Only `ChatHomeService` and `ChatRoomService` are mocked.
- This makes the test more robust, because there are less sources for
  false positives or wron negatives because of wrongly mocked behaviour.
- Aforementioned is proofed by an unnoticed bug, that was discoverd by
  the freshly refined test.

src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java

index 58efa54..35d0c3d 100644 (file)
@@ -56,7 +56,7 @@ public class ChatRoom
             : Mono.error(() -> new MessageMutationException(existing, text)))
         .switchIfEmpty(
             Mono
-                .just(service.persistMessage(key, LocalDateTime.now(clock), text))
+                .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))
                 .doOnNext(m ->
                 {
                   Sinks.EmitResult result = sink.tryEmitNext(m);
index 5e3c928..2f120ae 100644 (file)
@@ -12,12 +12,12 @@ import org.springframework.http.MediaType;
 import org.springframework.test.web.reactive.server.WebTestClient;
 import reactor.core.publisher.Mono;
 
+import java.time.Clock;
 import java.time.LocalDateTime;
 import java.util.UUID;
 
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 
 @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
@@ -26,7 +26,9 @@ import static org.mockito.Mockito.when;
 public class ChatBackendControllerTest
 {
   @MockBean
-  ChatHome chatHome;
+  ChatHomeService chatHomeService;
+  @MockBean
+  ChatRoomService chatRoomService;
 
   @Test
   @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
@@ -34,8 +36,7 @@ public class ChatBackendControllerTest
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    when(chatHome.getChatRoom(any(UUID.class)))
-        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
+    when(chatHomeService.getChatRoom(any(UUID.class))).thenReturn(Mono.empty());
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -55,8 +56,7 @@ public class ChatBackendControllerTest
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    when(chatHome.getChatRoom(any(UUID.class)))
-        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
+    when(chatHomeService.getChatRoom(any(UUID.class))).thenReturn(Mono.empty());
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -77,8 +77,7 @@ public class ChatBackendControllerTest
     UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHome.getChatRoom(any(UUID.class)))
-        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
+    when(chatHomeService.getChatRoom(any(UUID.class))).thenReturn(Mono.empty());
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -104,8 +103,7 @@ public class ChatBackendControllerTest
     UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHome.getChatRoom(any(UUID.class)))
-        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
+    when(chatHomeService.getChatRoom(any(UUID.class))).thenReturn(Mono.empty());
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -128,8 +126,7 @@ public class ChatBackendControllerTest
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    when(chatHome.getChatRoom(any(UUID.class)))
-        .thenReturn(Mono.error(() -> new UnknownChatroomException(chatroomId)));
+    when(chatHomeService.getChatRoom(any(UUID.class))).thenReturn(Mono.empty());
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -159,16 +156,30 @@ public class ChatBackendControllerTest
   {
     // Given
     UUID chatroomId = UUID.randomUUID();
-    String username = "foo";
+    String user = "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 existing = new Message(key, 0l, timestamp, "Existing");
-    when(chatRoom.addMessage(any(Long.class), any(String.class), any(String.class)))
-        .thenReturn(Mono.error(() -> new MessageMutationException(existing, "Mutated!")));
+    Message.MessageKey key = Message.MessageKey.of(user, messageId);
+    Long serialNumberExistingMessage = 0l;
+    String timeExistingMessageAsString = "2023-01-09T20:44:57.389665447";
+    LocalDateTime timeExistingMessage = LocalDateTime.parse(timeExistingMessageAsString);
+    String textExistingMessage = "Existing";
+    String textMutatedMessage = "Mutated!";
+    ChatRoom chatRoom = new ChatRoom(
+        chatroomId,
+        "Test-ChatRoom",
+        Clock.systemDefaultZone(),
+        chatRoomService, 8);
+    when(chatHomeService.getChatRoom(any(UUID.class))).thenReturn(Mono.just(chatRoom));
+    Message existingMessage = new Message(
+        key,
+        serialNumberExistingMessage,
+        timeExistingMessage,
+        textExistingMessage);
+    when(chatRoomService.getMessage(any(Message.MessageKey.class)))
+        .thenReturn(Mono.just(existingMessage));
+    // Needed for readable error-reports, in case of a bug that leads to according unwanted call
+    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class)))
+        .thenReturn(mock(Message.class));
 
     // When
     client
@@ -176,16 +187,21 @@ public class ChatBackendControllerTest
         .uri(
             "/put/{chatroomId}/{username}/{messageId}",
             chatroomId,
-            username,
+            user,
             messageId)
-        .bodyValue("bar")
+        .bodyValue(textMutatedMessage)
         .accept(MediaType.APPLICATION_JSON)
         .exchange()
         // Then
         .expectStatus().is4xxClientError()
         .expectBody()
         .jsonPath("$.type").isEqualTo("/problem/message-mutation")
-        .jsonPath("$.existingMessage.text").isEqualTo("Existing")
-        .jsonPath("$.mutatedText").isEqualTo("Mutated!");
+        .jsonPath("$.existingMessage.id").isEqualTo(messageId)
+        .jsonPath("$.existingMessage.serial").isEqualTo(serialNumberExistingMessage)
+        .jsonPath("$.existingMessage.time").isEqualTo(timeExistingMessageAsString)
+        .jsonPath("$.existingMessage.user").isEqualTo(user)
+        .jsonPath("$.existingMessage.text").isEqualTo(textExistingMessage)
+        .jsonPath("$.mutatedText").isEqualTo(textMutatedMessage);
+    verify(chatRoomService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class));
   }
 }