From 0157815c148d2abc145904eb0a8e91420aecca58 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 8 Jan 2023 21:13:39 +0100 Subject: [PATCH] fix: Asserted, that `ChatHome` acts, as expected, if asked for a `ChatRoom` --- .../kafka/chat/backend/domain/ChatHome.java | 9 ++-- .../chat/backend/domain/ChatHomeTest.java | 49 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java index 13f18b91..bb4d89c5 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java @@ -25,14 +25,15 @@ public class ChatHome { ChatRoom chatroom = service.createChatroom(UUID.randomUUID(), name); chatrooms.put(chatroom.getId(), chatroom); - return Mono.justOrEmpty(chatroom); + return Mono.just(chatroom); } public Mono getChatroom(UUID id) { - return Mono - .justOrEmpty(chatrooms.get(id)) - .or(Mono.error(() -> new UnknownChatroomException(id))); + ChatRoom chatroom = chatrooms.get(id); + return chatroom == null + ? Mono.error(() -> new UnknownChatroomException(id)) + : Mono.just(chatroom); } public Flux list() diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java new file mode 100644 index 00000000..655c1b8c --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java @@ -0,0 +1,49 @@ +package de.juplo.kafka.chat.backend.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.time.Clock; +import java.util.UUID; + +import static org.mockito.Mockito.mock; +import static pl.rzrz.assertj.reactor.Assertions.assertThat; + + +public class ChatHomeTest +{ + @Test + @DisplayName("Assert chatroom is delivered, if it exists") + void testGetExistingChatroom() + { + // Given + ChatHomeService chatHomeService = mock(ChatHomeService.class); + ChatRoomService chatRoomService = mock(ChatRoomService.class); + UUID chatroomId = UUID.randomUUID(); + ChatRoom chatRoom = new ChatRoom(chatroomId, "Foo", Clock.systemDefaultZone(), chatRoomService, 8); + ChatHome chatHome = new ChatHome(chatHomeService, Flux.just(chatRoom)); + + // When + Mono mono = chatHome.getChatroom(chatroomId); + + // Then + assertThat(mono).emitsExactly(chatRoom); + } + + @Test + @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist") + void testGetNonExistentChatroom() + { + // Given + ChatHomeService chatHomeService = mock(ChatHomeService.class); + ChatHome chatHome = new ChatHome(chatHomeService, Flux.empty()); + + // When + Mono mono = chatHome.getChatroom(UUID.randomUUID()); + + // Then + assertThat(mono).sendsError(); + } +} -- 2.20.1