test: Added a test for a simple chat-home
authorKai Moritz <kai@juplo.de>
Tue, 22 Aug 2023 15:33:36 +0000 (17:33 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 29 Aug 2023 17:17:27 +0000 (19:17 +0200)
src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTestBase.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHomeTest.java

diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTestBase.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTestBase.java
new file mode 100644 (file)
index 0000000..0a0b624
--- /dev/null
@@ -0,0 +1,54 @@
+package de.juplo.kafka.chat.backend.domain;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import reactor.core.publisher.Mono;
+
+import java.util.UUID;
+
+import static pl.rzrz.assertj.reactor.Assertions.assertThat;
+
+
+@ExtendWith(SpringExtension.class)
+public class ChatHomeTestBase
+{
+  @Autowired
+  ChatHome chatHome;
+
+
+  @Test
+  @DisplayName("Assert chatroom is delivered, if it exists")
+  void testGetExistingChatroom()
+  {
+    // Given
+    UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
+
+    // When
+    Mono<ChatRoom> mono = chatHome.getChatRoom(chatRoomId);
+
+    // Then
+    assertThat(mono).emitsCount(1);
+  }
+
+  @Test
+  @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
+  void testGetNonExistentChatroom()
+  {
+    // Given
+    UUID chatRoomId = UUID.fromString("7f59ec77-832e-4a17-8d22-55ef46242c17");
+
+    // When
+    Mono<ChatRoom> mono = chatHome.getChatRoom(chatRoomId);
+
+    // Then
+    assertThat(mono).sendsError(e ->
+    {
+      assertThat(e).isInstanceOf(UnknownChatroomException.class);
+      UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e;
+      assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
+    });
+  }
+}
index e45f1a5..1e5f0d4 100644 (file)
@@ -1,59 +1,46 @@
 package de.juplo.kafka.chat.backend.persistence.inmemory;
 
-import de.juplo.kafka.chat.backend.domain.ChatRoom;
-import de.juplo.kafka.chat.backend.domain.ChatRoomService;
-import org.junit.jupiter.api.DisplayName;
-import org.junit.jupiter.api.Test;
-import reactor.core.publisher.Mono;
-
+import com.fasterxml.jackson.databind.ObjectMapper;
+import de.juplo.kafka.chat.backend.domain.ChatHomeTestBase;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.context.annotation.Bean;
+
+import java.nio.file.Paths;
 import java.time.Clock;
-import java.util.UUID;
-
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static pl.rzrz.assertj.reactor.Assertions.assertThat;
 
 
-public class SimpleChatHomeTest
+public class SimpleChatHomeTest extends ChatHomeTestBase
 {
-  @Test
-  @DisplayName("Assert chatroom is delivered, if it exists")
-  void testGetExistingChatroom()
+  @TestConfiguration
+  static class Configuration
   {
-    // Given
-    InMemoryChatHomeService chatHomeService = mock(InMemoryChatHomeService.class);
-    ChatRoom chatRoom = new ChatRoom(
-        UUID.randomUUID(),
-        "Foo",
-        0,
-        Clock.systemDefaultZone(),
-        mock(ChatRoomService.class),
-        8);
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom));
-    SimpleChatHome chatHome = new SimpleChatHome(chatHomeService);
-
-    // When
-    Mono<ChatRoom> mono = chatHome.getChatRoom(chatRoom.getId());
-
-    // Then
-    assertThat(mono).emitsExactly(chatRoom);
-  }
-
-  @Test
-  @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
-  void testGetNonExistentChatroom()
-  {
-    // Given
-    InMemoryChatHomeService chatHomeService = mock(InMemoryChatHomeService.class);
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    SimpleChatHome chatHome = new SimpleChatHome(chatHomeService);
-
-    // When
-    Mono<ChatRoom> mono = chatHome.getChatRoom(UUID.randomUUID());
-
-    // Then
-    assertThat(mono).sendsError();
+    @Bean
+    SimpleChatHome chatHome(InMemoryChatHomeService chatHomeService)
+    {
+      return new SimpleChatHome(chatHomeService);
+    }
+
+    @Bean
+    InMemoryChatHomeService chatHomeService(StorageStrategy storageStrategy)
+    {
+      return new InMemoryChatHomeService(
+          1,
+          new int[] { 0 },
+          storageStrategy.read());
+    }
+
+    @Bean
+    public FilesStorageStrategy storageStrategy()
+    {
+      return new FilesStorageStrategy(
+          Paths.get("target", "test-classes", "data", "files"),
+          Clock.systemDefaultZone(),
+          8,
+          chatRoomId -> 0,
+          messageFlux -> new InMemoryChatRoomService(messageFlux),
+          new ObjectMapper());
+    }
   }
 }