fix: Asserted, that `ChatHome` acts, as expected, if asked for a `ChatRoom`
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 20:13:39 +0000 (21:13 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 9 Jan 2023 19:57:47 +0000 (20:57 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java
src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java [new file with mode: 0644]

index 13f18b9..bb4d89c 100644 (file)
@@ -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<ChatRoom> 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<ChatRoom> 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 (file)
index 0000000..655c1b8
--- /dev/null
@@ -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<ChatRoom> 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<ChatRoom> mono = chatHome.getChatroom(UUID.randomUUID());
+
+    // Then
+    assertThat(mono).sendsError();
+  }
+}