From 22c15a299f38649686d2677731aeb349e51224a5 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 8 Jan 2023 19:47:43 +0100 Subject: [PATCH] fix: Fixed generated problem-details for mutated messages --- .../api/ChatBackendControllerAdvice.java | 4 +- .../api/ChatBackendControllerTest.java | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) 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"); + } } -- 2.20.1