fix: Fixed generated problem-details for mutated messages
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 18:47:43 +0000 (19:47 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 9 Jan 2023 19:57:47 +0000 (20:57 +0100)
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java

index b64f7e0..6ff108e 100644 (file)
@@ -76,9 +76,9 @@ public class ChatBackendControllerAdvice
     stringBuilder.append(" cannot be mutated!");
     problem.setDetail(stringBuilder.toString());
 
-    problem.setProperty("mutatedMessage", e.getMutated());
+    problem.setProperty("mutatedMessage", MessageTo.from(e.getMutated()));
 
-    problem.setProperty("existingMessage", e.getExisting());
+    problem.setProperty("existingMessage", MessageTo.from(e.getExisting()));
 
     return problem;
   }
index 4392968..08361dd 100644 (file)
@@ -12,10 +12,12 @@ import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.http.MediaType;
 import org.springframework.test.web.reactive.server.WebTestClient;
 
+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;
 
 
@@ -148,4 +150,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(Optional.of(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");
+  }
 }