test: `StorageStrategy`-IT are restoring instead of recreating
authorKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 15:46:27 +0000 (16:46 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 15:52:41 +0000 (16:52 +0100)
src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryServicesConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java
src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesConfiguration.java
src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java
src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java
src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java

index 518cf41..3f3d888 100644 (file)
@@ -26,7 +26,7 @@ public class InMemoryServicesConfiguration
       name = "sharding-strategy",
       havingValue = "none",
       matchIfMissing = true)
-  ChatHomeService noneShardingChatHome(
+  SimpleChatHomeService noneShardingChatHome(
       ChatBackendProperties properties,
       StorageStrategy storageStrategy,
       Clock clock)
@@ -43,7 +43,7 @@ public class InMemoryServicesConfiguration
       prefix = "chat.backend.inmemory",
       name = "sharding-strategy",
       havingValue = "kafkalike")
-  ChatHomeService kafkalikeShardingChatHome(
+  ShardedChatHomeService kafkalikeShardingChatHome(
       ChatBackendProperties properties,
       StorageStrategy storageStrategy,
       Clock clock)
index 5c3fe2e..371d4a8 100644 (file)
@@ -49,6 +49,9 @@ public class SimpleChatHomeService implements ChatHomeService
 
   Mono<Void> restore(StorageStrategy storageStrategy)
   {
+    chatRoomInfo.clear();
+    chatRoomData.clear();
+
     return storageStrategy
         .readChatRoomInfo()
         .filter(info ->
index f8bebd6..b5a442f 100644 (file)
@@ -109,7 +109,7 @@ public class KafkaServicesConfiguration
   }
 
   @Bean
-    ChatHomeService kafkaChatHome(
+  KafkaChatHomeService kafkaChatHome(
       ChatBackendProperties properties,
       InfoChannel infoChannel,
       DataChannel dataChannel)
index 0ec0bc1..843b949 100644 (file)
@@ -1,15 +1,11 @@
 package de.juplo.kafka.chat.backend;
 
-import de.juplo.kafka.chat.backend.domain.ChatHomeService;
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
-import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
 import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryTestUtils;
+import de.juplo.kafka.chat.backend.implementation.inmemory.SimpleChatHomeService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 
-import java.time.Clock;
-
 
 @ContextConfiguration(classes = InMemoryTestUtils.class)
 @Slf4j
@@ -17,10 +13,12 @@ public abstract class AbstractInMemoryStorageIT extends AbstractStorageStrategyI
 {
   @Autowired
   InMemoryTestUtils testUtils;
+  @Autowired
+  SimpleChatHomeService simpleChatHomeService;
 
   @Override
-  ChatHomeService getChatHome()
+  void restore()
   {
-    return testUtils.createNoneShardingChatHomeService();
+    testUtils.restore(simpleChatHomeService).block();
   }
 }
index 9568545..7c9c07c 100644 (file)
@@ -28,19 +28,15 @@ import static pl.rzrz.assertj.reactor.Assertions.*;
 @Slf4j
 public abstract class AbstractStorageStrategyIT
 {
+  @Autowired
   ChatHomeService chathome;
-
   @Autowired
   StorageStrategy storageStrategy;
 
-  abstract ChatHomeService getChatHome();
 
-  protected void start()
-  {
-    chathome = getChatHome();
-  }
+  abstract void restore();
 
-  protected void stop()
+  void store()
   {
     storageStrategy
         .write(chathome)
@@ -48,9 +44,9 @@ public abstract class AbstractStorageStrategyIT
   }
 
   @Test
-  protected void testStoreAndRecreate()
+  void testStoreAndRecreate()
   {
-    start();
+    restore();
 
     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
 
@@ -69,8 +65,8 @@ public abstract class AbstractStorageStrategyIT
         .getChatRoomData(chatRoomId)
         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
 
-    stop();
-    start();
+    store();
+    restore();
 
     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
@@ -80,9 +76,9 @@ public abstract class AbstractStorageStrategyIT
   }
 
   @Test
-  protected void testStoreAndRecreateParallelChatRooms()
+  void testStoreAndRecreateParallelChatRooms()
   {
-    start();
+    restore();
 
     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
 
@@ -114,8 +110,8 @@ public abstract class AbstractStorageStrategyIT
         .getChatRoomData(chatRoomBId)
         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
 
-    stop();
-    start();
+    store();
+    restore();
 
     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
index 3f6cf9c..cfc8e72 100644 (file)
@@ -4,28 +4,19 @@ import de.juplo.kafka.chat.backend.ChatBackendProperties;
 import de.juplo.kafka.chat.backend.domain.ChatHomeService;
 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
 import org.springframework.beans.factory.annotation.Autowired;
+import reactor.core.publisher.Mono;
 
 import java.time.Clock;
 
 
 public class InMemoryTestUtils
 {
-  private final InMemoryServicesConfiguration config =
-      new InMemoryServicesConfiguration();
-
-  @Autowired
-  ChatBackendProperties properties;
   @Autowired
   StorageStrategy storageStrategy;
-  @Autowired
-  Clock clock;
 
 
-  public ChatHomeService createNoneShardingChatHomeService()
+  public Mono<Void> restore(SimpleChatHomeService simpleChatHomeService)
   {
-    return config.noneShardingChatHome(
-        properties,
-        storageStrategy,
-        clock);
+    return simpleChatHomeService.restore(storageStrategy);
   }
 }