From: Kai Moritz Date: Sat, 14 Jan 2023 15:07:04 +0000 (+0100) Subject: refactor: Made `SimpleChatHome` an implementation of `ChatHome` X-Git-Tag: wip~29 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=c1d3ca2a54bd62e0e7cf631716cdc7bfbeb22d38;p=demos%2Fkafka%2Fchat refactor: Made `SimpleChatHome` an implementation of `ChatHome` --- 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 2fc0e356..99439d36 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 @@ -1,34 +1,16 @@ package de.juplo.kafka.chat.backend.domain; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.*; +import java.util.UUID; -@Slf4j -@RequiredArgsConstructor -public class ChatHome +public interface ChatHome { - private final ChatHomeService service; - private final int shard; + Mono putChatRoom(ChatRoom chatRoom); - public Mono putChatRoom(ChatRoom chatRoom) - { - return service.putChatRoom(chatRoom); - } + Mono getChatRoom(UUID id); - public Mono getChatRoom(UUID id) - { - return service - .getChatRoom(shard, id) - .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id))); - } - - public Flux getChatRooms() - { - return service.getChatRooms(shard); - } + Flux getChatRooms(); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java new file mode 100644 index 00000000..daa710bf --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java @@ -0,0 +1,37 @@ +package de.juplo.kafka.chat.backend.domain; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.*; + + +@Slf4j +@RequiredArgsConstructor +public class SimpleChatHome implements ChatHome +{ + private final ChatHomeService service; + private final int shard; + + @Override + public Mono putChatRoom(ChatRoom chatRoom) + { + return service.putChatRoom(chatRoom); + } + + @Override + public Mono getChatRoom(UUID id) + { + return service + .getChatRoom(shard, id) + .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id))); + } + + @Override + public Flux getChatRooms() + { + return service.getChatRooms(shard); + } +} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java index e40d950f..a0f37f06 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java @@ -4,6 +4,7 @@ import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy; import de.juplo.kafka.chat.backend.api.ShardingStrategy; import de.juplo.kafka.chat.backend.domain.ChatHome; +import de.juplo.kafka.chat.backend.domain.SimpleChatHome; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -26,14 +27,14 @@ public class InMemoryServicesConfiguration InMemoryChatHomeService chatHomeService, StorageStrategy storageStrategy) { - ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; + SimpleChatHome[] chatHomes = new SimpleChatHome[properties.getInmemory().getNumShards()]; storageStrategy .read() .subscribe(chatRoom -> { int shard = chatRoom.getShard(); if (chatHomes[shard] == null) - chatHomes[shard] = new ChatHome(chatHomeService, shard); + chatHomes[shard] = new SimpleChatHome(chatHomeService, shard); }); return chatHomes; } diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java index 5ebd08d1..b1c80a90 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -264,10 +264,10 @@ public class ChatBackendControllerTest ChatBackendProperties properties, InMemoryChatHomeService service) { - ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; + SimpleChatHome[] chatHomes = new SimpleChatHome[properties.getInmemory().getNumShards()]; Arrays .stream(properties.getInmemory().getOwnedShards()) - .forEach(i -> chatHomes[i] = new ChatHome(service, i)); + .forEach(i -> chatHomes[i] = new SimpleChatHome(service, i)); return chatHomes; } } 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 deleted file mode 100644 index 2eeca407..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.juplo.kafka.chat.backend.domain; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import reactor.core.publisher.Mono; - -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 ChatHomeTest -{ - @Test - @DisplayName("Assert chatroom is delivered, if it exists") - void testGetExistingChatroom() - { - // Given - ChatHomeService chatHomeService = mock(ChatHomeService.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)); - ChatHome chatHome = new ChatHome(chatHomeService, 0); - - // 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 - ChatHomeService chatHomeService = mock(ChatHomeService.class); - when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); - ChatHome chatHome = new ChatHome(chatHomeService, 0); - - // When - Mono mono = chatHome.getChatRoom(UUID.randomUUID()); - - // Then - assertThat(mono).sendsError(); - } -} diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/SimpleChatHomeTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/SimpleChatHomeTest.java new file mode 100644 index 00000000..a648daee --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/SimpleChatHomeTest.java @@ -0,0 +1,57 @@ +package de.juplo.kafka.chat.backend.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; + +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 +{ + @Test + @DisplayName("Assert chatroom is delivered, if it exists") + void testGetExistingChatroom() + { + // Given + ChatHomeService chatHomeService = mock(ChatHomeService.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, 0); + + // 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 + ChatHomeService chatHomeService = mock(ChatHomeService.class); + when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + SimpleChatHome chatHome = new SimpleChatHome(chatHomeService, 0); + + // When + Mono mono = chatHome.getChatRoom(UUID.randomUUID()); + + // Then + assertThat(mono).sendsError(); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java index 84cf7d9b..58cfaf0e 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java @@ -24,7 +24,7 @@ public abstract class AbstractStorageStrategyIT protected void start() { - chathome = new ChatHome(getChatHomeServiceSupplier().get(), 0); + chathome = new SimpleChatHome(getChatHomeServiceSupplier().get(), 0); chatRoomFactory = getChatRoomFactory(); }