From: Kai Moritz Date: Fri, 8 Mar 2024 09:48:20 +0000 (+0100) Subject: test: Added tests for listening to `ChatRoomDataTest` X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=b2d60ed2df0394f62dce00992e53d0786921b4db;p=demos%2Fkafka%2Fchat test: Added tests for listening to `ChatRoomDataTest` --- 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 389d12a5..39c17dc9 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 @@ -1,14 +1,14 @@ package de.juplo.kafka.chat.backend.domain; import org.junit.jupiter.api.BeforeEach; +import org.awaitility.Awaitility; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; -import java.time.Clock; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; +import java.time.*; +import java.util.LinkedList; +import java.util.List; import static org.mockito.Mockito.*; import static pl.rzrz.assertj.reactor.Assertions.assertThat; @@ -185,6 +185,93 @@ public class ChatRoomDataTest verify(chatMessageService, never()).persistMessage(any(), any(), any()); } + @Test + @DisplayName("Assert, that a listener receives a message, that was added after the listening had started") + void testListenerReceivesMessageAddedAfterListeningStarts() + { + // Given + 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(someMessage())); + + // When + List receivedMessages = new LinkedList<>(); + chatRoomData + .listen() + .subscribe(receivedMessage -> receivedMessages.add(receivedMessage)); + Message sentMessage = chatRoomData + .addMessage(messageId, user, "Some Text") + .block(); + + // Then + Awaitility + .await() + .atMost(Duration.ofSeconds(1)) + .untilAsserted(() -> assertThat(receivedMessages).contains(sentMessage)); + } + + @Test + @DisplayName("Assert, that a listener receives a message, that was added before the listening had started") + void testListenerReceivesMessageAddedBeforeListeningStarts() + { + // Given + 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(someMessage())); + + // When + Message sentMessage = chatRoomData + .addMessage(messageId, user, "Some Text") + .block(); + List receivedMessages = new LinkedList<>(); + chatRoomData + .listen() + .subscribe(receivedMessage -> receivedMessages.add(receivedMessage)); + + // Then + Awaitility + .await() + .atMost(Duration.ofSeconds(1)) + .untilAsserted(() -> assertThat(receivedMessages).contains(sentMessage)); + } + + @Test + @DisplayName("Assert, that a listener receives several messages, that were added before and after the listening had started, in correct order") + void testListenerReceivesMessagesFromBeforeAndAfterListeningHadStartedInCorrectOrder() + { + // Given + Message message1 = new Message(key, 1l, timestamp, "#1"); + Message message2 = new Message(key, 2l, timestamp, "#2"); + Message message3 = new Message(key, 3l, timestamp, "#3"); + Message message4 = new Message(key, 4l, timestamp, "#4"); + 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(message1)) + .thenReturn(Mono.just(message2)) + .thenReturn(Mono.just(message3)) + .thenReturn(Mono.just(message4)); + + // When + Message[] sentMessages = new Message[4]; + sentMessages[0] = chatRoomData.addMessage(messageId, user, "Some Text").block(); + sentMessages[1] = chatRoomData.addMessage(messageId, user, "Some Text").block(); + List receivedMessages = new LinkedList<>(); + chatRoomData + .listen() + .subscribe(receivedMessage -> receivedMessages.add(receivedMessage)); + sentMessages[2] = chatRoomData.addMessage(messageId, user, "Some Text").block(); + sentMessages[3] = chatRoomData.addMessage(messageId, user, "Some Text").block(); + + // Then + Awaitility + .await() + .atMost(Duration.ofSeconds(1)) + .untilAsserted(() -> assertThat(receivedMessages).contains(sentMessages)); + } + /** * This message is used, when methods of {@link ChatMessageService} are mocked,