From 2fcb6fe1d45d046665d4a1ea765faa043c09ae56 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 8 Mar 2024 11:55:25 +0100 Subject: [PATCH] test: `ChatRoomDataTest` verifies that persistence is triggered as expected --- .../chat/backend/domain/ChatRoomDataTest.java | 71 +++++++++++++++++-- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java index c3280004..641dc6af 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java @@ -32,7 +32,7 @@ public class ChatRoomDataTest now = Clock.fixed(Instant.now(), ZoneId.systemDefault()); chatMessageService = mock(ChatMessageService.class); chatRoomData = new ChatRoomData( - Clock.systemDefaultZone(), + now, chatMessageService, 8); @@ -73,8 +73,8 @@ public class ChatRoomDataTest } @Test - @DisplayName("Assert, that Mono emits expected message, if a new message is added") - void testAddNewMessage() + @DisplayName("Assert, that Mono emits the persisted message, if a new message is added") + void testAddNewMessageEmitsPersistedMessage() { // Given String messageText = "Bar"; @@ -90,8 +90,27 @@ public class ChatRoomDataTest } @Test - @DisplayName("Assert, that Mono emits expected message, if an unchanged message is added") - void testAddUnchangedMessage() + @DisplayName("Assert, that ChatMessageService.persistMessage() is called correctly, if a new message is added") + void testAddNewMessageTriggersPersistence() + { + // Given + String messageText = "Bar"; + Message message = new Message(key, 0l, timestamp, messageText); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.empty()); + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message)); + + // When + chatRoomData + .addMessage(messageId, user, messageText) + .block(); + + // Then + verify(chatMessageService, times(1)).persistMessage(eq(key), eq(timestamp), eq(messageText)); + } + + @Test + @DisplayName("Assert, that Mono emits the already persisted message, if an unchanged message is added") + void testAddUnchangedMessageEmitsAlreadyPersistedMessage() { // Given String messageText = "Bar"; @@ -106,9 +125,28 @@ public class ChatRoomDataTest assertThat(mono).emitsExactly(message); } + @Test + @DisplayName("Assert, that ChatMessageService.persistMessage() is not called, if an unchanged message is added") + void testAddUnchangedMessageDoesNotTriggerPersistence() + { + // Given + String messageText = "Bar"; + Message message = new Message(key, 0l, timestamp, messageText); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message)); + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message)); + + // When + chatRoomData + .addMessage(messageId, user, messageText) + .block(); + + // Then + verify(chatMessageService, never()).persistMessage(any(), any(), any()); + } + @Test @DisplayName("Assert, that Mono sends an error, if a message is added again with mutated text") - void testAddMutatedMessage() + void testAddMutatedMessageSendsError() { // Given String messageText = "Bar"; @@ -123,4 +161,25 @@ public class ChatRoomDataTest // Then assertThat(mono).sendsError(); } + + @Test + @DisplayName("Assert, that ChatMessageService.persistMessage() is not called, if a message is added again with mutated text") + void testAddMutatedMessageDoesNotTriggerPersistence() + { + // Given + String messageText = "Bar"; + String mutatedText = "Boom!"; + Message message = new Message(key, 0l, timestamp, messageText); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message)); + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message)); + + // When + chatRoomData + .addMessage(messageId, user, mutatedText) + .onErrorResume((throwable) -> Mono.empty()) + .block(); + + // Then + verify(chatMessageService, never()).persistMessage(any(), any(), any()); + } } -- 2.20.1