From f7115e7978bcca1d85257caed006dc41c1eaca9c Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 22 Aug 2023 17:33:36 +0200 Subject: [PATCH] test: Added a test for a simple chat-home --- .../chat/backend/domain/ChatHomeTestBase.java | 54 ++++++++++++ .../inmemory/SimpleChatHomeTest.java | 87 ++++++++----------- 2 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTestBase.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 index 00000000..0a0b6249 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTestBase.java @@ -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 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 mono = chatHome.getChatRoom(chatRoomId); + + // Then + assertThat(mono).sendsError(e -> + { + assertThat(e).isInstanceOf(UnknownChatroomException.class); + UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e; + assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId); + }); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHomeTest.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHomeTest.java index e45f1a5e..1e5f0d4c 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHomeTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHomeTest.java @@ -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 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 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()); + } } } -- 2.20.1