test: Simplified the integration-tests for `StorageStrategy`
authorKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 14:35:39 +0000 (15:35 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 15:52:20 +0000 (16:52 +0100)
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/InMemoryWithFilesStorageIT.java
src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java
src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java [new file with mode: 0644]

index 3fea43e..0ec0bc1 100644 (file)
@@ -1,36 +1,26 @@
 package de.juplo.kafka.chat.backend;
 
 import de.juplo.kafka.chat.backend.domain.ChatHomeService;
-import de.juplo.kafka.chat.backend.implementation.inmemory.SimpleChatHomeService;
-import lombok.RequiredArgsConstructor;
+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 lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
 
 import java.time.Clock;
 
 
-@RequiredArgsConstructor
+@ContextConfiguration(classes = InMemoryTestUtils.class)
 @Slf4j
 public abstract class AbstractInMemoryStorageIT extends AbstractStorageStrategyIT
 {
-  final Clock clock;
+  @Autowired
+  InMemoryTestUtils testUtils;
 
   @Override
-  protected StorageStrategyITConfig getConfig()
+  ChatHomeService getChatHome()
   {
-    return new StorageStrategyITConfig()
-    {
-      int bufferSize = 8;
-
-      SimpleChatHomeService simpleChatHome = new SimpleChatHomeService(
-          getStorageStrategy(),
-          clock,
-          bufferSize);
-
-      @Override
-      public ChatHomeService getChatHome()
-      {
-        return simpleChatHome;
-      }
-    };
+    return testUtils.createNoneShardingChatHomeService();
   }
 }
index 41e80ed..9568545 100644 (file)
@@ -2,33 +2,47 @@ package de.juplo.kafka.chat.backend;
 
 import de.juplo.kafka.chat.backend.domain.*;
 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
+import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
+import de.juplo.kafka.chat.backend.storage.files.FilesStorageConfiguration;
+import de.juplo.kafka.chat.backend.storage.mongodb.MongoDbStorageConfiguration;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 
+import java.time.Clock;
 import java.util.List;
 import java.util.UUID;
 
 import static pl.rzrz.assertj.reactor.Assertions.*;
 
 
+@SpringJUnitConfig(classes = {
+    InMemoryServicesConfiguration.class,
+    FilesStorageConfiguration.class,
+    MongoDbStorageConfiguration.class,
+    AbstractStorageStrategyIT.TestConfig.class })
+@EnableConfigurationProperties(ChatBackendProperties.class)
 @Slf4j
 public abstract class AbstractStorageStrategyIT
 {
-  protected ChatHomeService chathome;
+  ChatHomeService chathome;
 
+  @Autowired
+  StorageStrategy storageStrategy;
 
-  protected abstract StorageStrategy getStorageStrategy();
-  protected abstract StorageStrategyITConfig getConfig();
+  abstract ChatHomeService getChatHome();
 
   protected void start()
   {
-    StorageStrategyITConfig config = getConfig();
-    chathome = config.getChatHome();
+    chathome = getChatHome();
   }
 
   protected void stop()
   {
-    getStorageStrategy()
+    storageStrategy
         .write(chathome)
         .subscribe();
   }
@@ -115,8 +129,12 @@ public abstract class AbstractStorageStrategyIT
   }
 
 
-  interface StorageStrategyITConfig
+  static class TestConfig
   {
-    ChatHomeService getChatHome();
+    @Bean
+    Clock clock()
+    {
+      return Clock.systemDefaultZone();
+    }
   }
 }
index 78f4625..e4eaf3a 100644 (file)
@@ -1,54 +1,29 @@
 package de.juplo.kafka.chat.backend;
 
-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.implementation.StorageStrategy;
-import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.BeforeEach;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.TestPropertySource;
 
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.time.Clock;
-import java.util.logging.Level;
 
 
+@TestPropertySource(properties = {
+    "chat.backend.inmemory.sharding-strategy=none",
+    "chat.backend.inmemory.storage-strategy=files",
+    "chat.backend.inmemory.storage-directory=target/files" })
 @Slf4j
 public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
 {
-  final static Path path = Paths.get("target","files");
-
-  final ObjectMapper mapper;
-  final FilesStorageStrategy storageStrategy;
-
-
-  public InMemoryWithFilesStorageIT()
-  {
-    super(Clock.systemDefaultZone());
-    mapper = new ObjectMapper();
-    mapper.registerModule(new JavaTimeModule());
-    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
-    storageStrategy = new FilesStorageStrategy(
-        path,
-        chatRoomId -> 0,
-        mapper,
-        Level.FINE,
-        true);
-  }
-
-
-  @Override
-  protected StorageStrategy getStorageStrategy()
-  {
-    return storageStrategy;
-  }
-
   @BeforeEach
-  void reset() throws Exception
+  void resetStorage(
+      @Autowired ChatBackendProperties properties)
+      throws Exception
   {
+    Path path = Paths.get(properties.getInmemory().getStorageDirectory());
     if (Files.exists(path))
     {
       Files
index 7055af5..8f4e37a 100644 (file)
@@ -1,83 +1,45 @@
 package de.juplo.kafka.chat.backend;
 
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
 import de.juplo.kafka.chat.backend.storage.mongodb.ChatRoomRepository;
 import de.juplo.kafka.chat.backend.storage.mongodb.MessageRepository;
-import de.juplo.kafka.chat.backend.storage.mongodb.MongoDbStorageStrategy;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
-import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
-import org.springframework.context.annotation.Bean;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.context.TestPropertySource;
 import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.containers.MongoDBContainer;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.junit.jupiter.Container;
 import org.testcontainers.junit.jupiter.Testcontainers;
 
-import java.time.Clock;
-
 
+@TestPropertySource(properties = {
+    "chat.backend.inmemory.sharding-strategy=none",
+    "chat.backend.inmemory.storage-strategy=mongodb" })
 @Testcontainers
-@ExtendWith({SpringExtension.class})
 @EnableAutoConfiguration
-@AutoConfigureDataMongo
 @Slf4j
 public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT
 {
-  @Autowired
-  MongoDbStorageStrategy storageStrategy;
-  @Autowired
-  ChatRoomRepository chatRoomRepository;
-  @Autowired
-  MessageRepository messageRepository;
-
-
-  public InMemoryWithMongoDbStorageIT()
-  {
-    super(Clock.systemDefaultZone());
-  }
-
-
-  @Override
-  protected StorageStrategy getStorageStrategy()
-  {
-    return storageStrategy;
-  }
-
-  @TestConfiguration
-  static class InMemoryWithMongoDbStorageStrategyITConfig
-  {
-    @Bean
-    MongoDbStorageStrategy storageStrategy(
-        ChatRoomRepository chatRoomRepository,
-        MessageRepository messageRepository)
-    {
-      return new MongoDbStorageStrategy(chatRoomRepository, messageRepository);
-    }
-
-    @Bean
-    Clock clock()
-    {
-      return Clock.systemDefaultZone();
-    }
-  }
-
   @Container
   @ServiceConnection
   private static final GenericContainer MONGODB = new MongoDBContainer("mongo:6");
 
+  @BeforeEach
+  void resetStorage(
+      @Autowired ChatRoomRepository chatRoomRepository,
+      @Autowired MessageRepository messageRepository)
+  {
+    chatRoomRepository.deleteAll();
+    messageRepository.deleteAll();
+  }
+
   @BeforeEach
   void setUpLogging()
   {
     Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
     MONGODB.followOutput(logConsumer);
-    chatRoomRepository.deleteAll();
-    messageRepository.deleteAll();
   }
 }
diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java
new file mode 100644 (file)
index 0000000..3f6cf9c
--- /dev/null
@@ -0,0 +1,31 @@
+package de.juplo.kafka.chat.backend.implementation.inmemory;
+
+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 java.time.Clock;
+
+
+public class InMemoryTestUtils
+{
+  private final InMemoryServicesConfiguration config =
+      new InMemoryServicesConfiguration();
+
+  @Autowired
+  ChatBackendProperties properties;
+  @Autowired
+  StorageStrategy storageStrategy;
+  @Autowired
+  Clock clock;
+
+
+  public ChatHomeService createNoneShardingChatHomeService()
+  {
+    return config.noneShardingChatHome(
+        properties,
+        storageStrategy,
+        clock);
+  }
+}