refactor: DRY for the configuration of the `AbstractStorageStrategyIT`
authorKai Moritz <kai@juplo.de>
Fri, 17 Feb 2023 23:10:03 +0000 (00:10 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 17 Feb 2023 23:13:24 +0000 (00:13 +0100)
src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractInMemoryStorageIT.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java
src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageIT.java

diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractInMemoryStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractInMemoryStorageIT.java
new file mode 100644 (file)
index 0000000..832ebd9
--- /dev/null
@@ -0,0 +1,47 @@
+package de.juplo.kafka.chat.backend.persistence;
+
+import de.juplo.kafka.chat.backend.domain.ChatHomeService;
+import de.juplo.kafka.chat.backend.domain.ChatRoomFactory;
+import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
+import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+
+import java.time.Clock;
+
+
+@RequiredArgsConstructor
+@Slf4j
+public abstract class AbstractInMemoryStorageIT extends AbstractStorageStrategyIT
+{
+  final Clock clock;
+
+  @Override
+  protected StorageStrategyITConfig getConfig()
+  {
+    return new StorageStrategyITConfig()
+    {
+      InMemoryChatHomeService chatHomeService = new InMemoryChatHomeService(
+          1,
+          new int[] { 0 },
+          getStorageStrategy().read());
+
+      InMemoryChatRoomFactory chatRoomFactory = new InMemoryChatRoomFactory(
+          chatRoomId -> 0,
+          clock,
+          8);
+
+      @Override
+      public ChatHomeService getChatHomeService()
+      {
+        return chatHomeService;
+      }
+
+      @Override
+      public ChatRoomFactory getChatRoomFactory()
+      {
+        return chatRoomFactory;
+      }
+    };
+  }
+}
index 8ca7d47..1bb0870 100644 (file)
@@ -3,11 +3,7 @@ package de.juplo.kafka.chat.backend.persistence;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
-import de.juplo.kafka.chat.backend.domain.ChatHomeService;
-import de.juplo.kafka.chat.backend.domain.ChatRoomFactory;
-import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory;
 import de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy;
-import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
 import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.BeforeEach;
@@ -20,18 +16,17 @@ import java.time.Clock;
 
 
 @Slf4j
-public class InMemoryWithFilesStorageIT extends AbstractStorageStrategyIT
+public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
 {
   final static Path path = Paths.get("target","files");
 
-  final Clock clock;
   final ObjectMapper mapper;
   final FilesStorageStrategy storageStrategy;
 
 
   public InMemoryWithFilesStorageIT()
   {
-    clock = Clock.systemDefaultZone();
+    super(Clock.systemDefaultZone());
     mapper = new ObjectMapper();
     mapper.registerModule(new JavaTimeModule());
     mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
@@ -51,35 +46,6 @@ public class InMemoryWithFilesStorageIT extends AbstractStorageStrategyIT
     return storageStrategy;
   }
 
-  @Override
-  protected StorageStrategyITConfig getConfig()
-  {
-    return new StorageStrategyITConfig()
-    {
-      InMemoryChatHomeService chatHomeService = new InMemoryChatHomeService(
-          1,
-          new int[] { 0 },
-          getStorageStrategy().read());
-
-      InMemoryChatRoomFactory chatRoomFactory = new InMemoryChatRoomFactory(
-          chatRoomId -> 0,
-          clock,
-          8);
-
-      @Override
-      public ChatHomeService getChatHomeService()
-      {
-        return chatHomeService;
-      }
-
-      @Override
-      public ChatRoomFactory getChatRoomFactory()
-      {
-        return chatRoomFactory;
-      }
-    };
-  }
-
   @BeforeEach
   void reset() throws Exception
   {
index f86c9d3..7ca9cb2 100644 (file)
@@ -1,10 +1,6 @@
 package de.juplo.kafka.chat.backend.persistence;
 
-import de.juplo.kafka.chat.backend.domain.ChatHomeService;
-import de.juplo.kafka.chat.backend.domain.ChatRoomFactory;
-import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
 import de.juplo.kafka.chat.backend.persistence.InMemoryWithMongoDbStorageIT.DataSourceInitializer;
-import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory;
 import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
 import de.juplo.kafka.chat.backend.persistence.storage.mongodb.ChatRoomRepository;
 import de.juplo.kafka.chat.backend.persistence.storage.mongodb.MongoDbStorageStrategy;
@@ -35,7 +31,7 @@ import java.time.Clock;
 @AutoConfigureDataMongo
 @ContextConfiguration(initializers = DataSourceInitializer.class)
 @Slf4j
-public class InMemoryWithMongoDbStorageIT extends AbstractStorageStrategyIT
+public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT
 {
   @Autowired
   MongoDbStorageStrategy storageStrategy;
@@ -45,39 +41,16 @@ public class InMemoryWithMongoDbStorageIT extends AbstractStorageStrategyIT
   Clock clock;
 
 
-  @Override
-  protected StorageStrategy getStorageStrategy()
+  public InMemoryWithMongoDbStorageIT()
   {
-    return storageStrategy;
+    super(Clock.systemDefaultZone());
   }
 
+
   @Override
-  protected StorageStrategyITConfig getConfig()
+  protected StorageStrategy getStorageStrategy()
   {
-    return new StorageStrategyITConfig()
-    {
-      InMemoryChatHomeService chatHomeService = new InMemoryChatHomeService(
-          1,
-          new int[] { 0 },
-          getStorageStrategy().read());
-
-      InMemoryChatRoomFactory chatRoomFactory = new InMemoryChatRoomFactory(
-          chatRoomId -> 0,
-          clock,
-          8);
-
-      @Override
-      public ChatHomeService getChatHomeService()
-      {
-        return chatHomeService;
-      }
-
-      @Override
-      public ChatRoomFactory getChatRoomFactory()
-      {
-        return chatRoomFactory;
-      }
-    };
+    return storageStrategy;
   }
 
   @TestConfiguration