From: Kai Moritz Date: Sun, 8 Jan 2023 18:47:43 +0000 (+0100) Subject: fix: Fixed generated problem-details for mutated messages X-Git-Tag: TEST~31 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=fd1875cbaf73a6c24748c57340dde7e964d228ea;p=demos%2Fkafka%2Fchat fix: Fixed generated problem-details for mutated messages --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java index b64f7e00..6ff108ec 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java @@ -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; } diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java index 4392968d..08361dd9 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -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"); + } }