From 72682edb434aa1ad6e2ce7c3b337711cdb746ef1 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 14 Jan 2023 16:07:04 +0100 Subject: [PATCH] refactor: Made `SimpleChatHome` an implementation of `ChatHome` --- .../kafka/chat/backend/domain/ChatHome.java | 28 +++----------- .../chat/backend/domain/SimpleChatHome.java | 37 +++++++++++++++++++ .../InMemoryServicesConfiguration.java | 5 ++- .../api/ChatBackendControllerTest.java | 4 +- ...tHomeTest.java => SimpleChatHomeTest.java} | 6 +-- .../AbstractStorageStrategyIT.java | 2 +- 6 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java rename src/test/java/de/juplo/kafka/chat/backend/domain/{ChatHomeTest.java => SimpleChatHomeTest.java} (89%) 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/SimpleChatHomeTest.java similarity index 89% rename from src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java rename to src/test/java/de/juplo/kafka/chat/backend/domain/SimpleChatHomeTest.java index 2eeca407..a648daee 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/SimpleChatHomeTest.java @@ -14,7 +14,7 @@ import static org.mockito.Mockito.when; import static pl.rzrz.assertj.reactor.Assertions.assertThat; -public class ChatHomeTest +public class SimpleChatHomeTest { @Test @DisplayName("Assert chatroom is delivered, if it exists") @@ -30,7 +30,7 @@ public class ChatHomeTest mock(ChatRoomService.class), 8); when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom)); - ChatHome chatHome = new ChatHome(chatHomeService, 0); + SimpleChatHome chatHome = new SimpleChatHome(chatHomeService, 0); // When Mono mono = chatHome.getChatRoom(chatRoom.getId()); @@ -46,7 +46,7 @@ public class ChatHomeTest // Given ChatHomeService chatHomeService = mock(ChatHomeService.class); when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); - ChatHome chatHome = new ChatHome(chatHomeService, 0); + SimpleChatHome chatHome = new SimpleChatHome(chatHomeService, 0); // When Mono mono = chatHome.getChatRoom(UUID.randomUUID()); 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(); } -- 2.20.1