refactor: Made `SimpleChatHome` an implementation of `ChatHome`
authorKai Moritz <kai@juplo.de>
Sat, 14 Jan 2023 15:07:04 +0000 (16:07 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:39:01 +0000 (19:39 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java
src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java
src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeTest.java [deleted file]
src/test/java/de/juplo/kafka/chat/backend/domain/SimpleChatHomeTest.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java

index 2fc0e35..99439d3 100644 (file)
@@ -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<ChatRoom> putChatRoom(ChatRoom chatRoom);
 
-  public Mono<ChatRoom> putChatRoom(ChatRoom chatRoom)
-  {
-    return service.putChatRoom(chatRoom);
-  }
+  Mono<ChatRoom> getChatRoom(UUID id);
 
-  public Mono<ChatRoom> getChatRoom(UUID id)
-  {
-    return service
-        .getChatRoom(shard, id)
-        .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
-  }
-
-  public Flux<ChatRoom> getChatRooms()
-  {
-    return service.getChatRooms(shard);
-  }
+  Flux<ChatRoom> 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 (file)
index 0000000..daa710b
--- /dev/null
@@ -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<ChatRoom> putChatRoom(ChatRoom chatRoom)
+  {
+    return service.putChatRoom(chatRoom);
+  }
+
+  @Override
+  public Mono<ChatRoom> getChatRoom(UUID id)
+  {
+    return service
+        .getChatRoom(shard, id)
+        .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
+  }
+
+  @Override
+  public Flux<ChatRoom> getChatRooms()
+  {
+    return service.getChatRooms(shard);
+  }
+}
index e40d950..a0f37f0 100644 (file)
@@ -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;
   }
index 5ebd08d..b1c80a9 100644 (file)
@@ -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 (file)
index 2eeca40..0000000
+++ /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<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
-    ChatHomeService chatHomeService = mock(ChatHomeService.class);
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    ChatHome chatHome = new ChatHome(chatHomeService, 0);
-
-    // When
-    Mono<ChatRoom> 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 (file)
index 0000000..a648dae
--- /dev/null
@@ -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<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
+    ChatHomeService chatHomeService = mock(ChatHomeService.class);
+    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+    SimpleChatHome chatHome = new SimpleChatHome(chatHomeService, 0);
+
+    // When
+    Mono<ChatRoom> mono = chatHome.getChatRoom(UUID.randomUUID());
+
+    // Then
+    assertThat(mono).sendsError();
+  }
+}
index 84cf7d9..58cfaf0 100644 (file)
@@ -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();
   }