test: Simplified & unified test-setup for ``ChatHomeServiceTest``s
authorKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 12:10:09 +0000 (13:10 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 13:20:57 +0000 (14:20 +0100)
* Using existing configuration-classes instead of hand-coded configuartion.
* Aligned the `KafkaChatHomeServiceTest` with the `in-memory`-tests.

src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeServiceTest.java
src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeServiceTest.java
src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeServiceTest.java
src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java

index 984da1d..2db530e 100644 (file)
@@ -1,22 +1,33 @@
 package de.juplo.kafka.chat.backend.domain;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
 import de.juplo.kafka.chat.backend.domain.exceptions.LoadInProgressException;
 import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException;
+import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
+import de.juplo.kafka.chat.backend.storage.files.FilesStorageConfiguration;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 import reactor.core.publisher.Mono;
 import reactor.util.retry.Retry;
 
+import java.time.Clock;
 import java.time.Duration;
 import java.util.UUID;
 
 import static pl.rzrz.assertj.reactor.Assertions.assertThat;
 
 
-@ExtendWith(SpringExtension.class)
+@SpringJUnitConfig(classes = {
+    InMemoryServicesConfiguration.class,
+    FilesStorageConfiguration.class,
+    ChatHomeServiceTest.TestConfiguration.class })
+@EnableConfigurationProperties(ChatBackendProperties.class)
 public abstract class ChatHomeServiceTest
 {
   @Autowired
@@ -65,4 +76,21 @@ public abstract class ChatHomeServiceTest
       assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
     });
   }
+
+  static class TestConfiguration
+  {
+    @Bean
+    ObjectMapper objectMapper()
+    {
+      ObjectMapper objectMapper = new ObjectMapper();
+      objectMapper.registerModule(new JavaTimeModule());
+      return objectMapper;
+    }
+
+    @Bean
+    Clock clock()
+    {
+      return Clock.systemDefaultZone();
+    }
+  }
 }
index 8e21c5f..95501ad 100644 (file)
@@ -1,88 +1,18 @@
 package de.juplo.kafka.chat.backend.implementation.inmemory;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 import de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest;
-import de.juplo.kafka.chat.backend.implementation.ShardingStrategy;
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
-import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
-import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.TestPropertySource;
 
-import java.net.URI;
-import java.nio.file.Paths;
-import java.time.Clock;
-import java.util.logging.Level;
-import java.util.stream.IntStream;
+import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS;
+import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.OWNED_SHARD;
 
+
+@TestPropertySource(properties = {
+    "chat.backend.inmemory.sharding-strategy=kafkalike",
+    "chat.backend.inmemory.num-shards=" + NUM_SHARDS,
+    "chat.backend.inmemory.owned-shards=" + OWNED_SHARD,
+    "chat.backend.inmemory.storage-strategy=files",
+    "chat.backend.inmemory.storage-directory=target/test-classes/data/files" })
 public class ShardedChatHomeServiceTest extends ChatHomeServiceWithShardsTest
 {
-  @TestConfiguration
-  static class Configuration
-  {
-    @Bean
-    ShardedChatHomeService chatHome(
-        StorageStrategy storageStrategy,
-        Clock clock)
-    {
-      SimpleChatHomeService[] chatHomes = new SimpleChatHomeService[NUM_SHARDS];
-
-      IntStream
-          .of(ownedShards())
-          .forEach(shard -> chatHomes[shard] = new SimpleChatHomeService(
-              shard,
-              storageStrategy,
-              clock,
-              bufferSize()));
-
-      ShardingStrategy strategy = new KafkaLikeShardingStrategy(NUM_SHARDS);
-
-      return new ShardedChatHomeService(
-          "http://instance-0",
-          chatHomes,
-          IntStream
-              .range(0, NUM_SHARDS)
-              .mapToObj(shard -> "http://instance-0")
-              .map(uriString -> URI.create(uriString))
-              .toArray(size -> new URI[size]),
-          strategy);
-    }
-
-    @Bean
-    FilesStorageStrategy storageStrategy(
-        Clock clock,
-        ObjectMapper objectMapper)
-    {
-      return new FilesStorageStrategy(
-          Paths.get("target", "test-classes", "data", "files"),
-          new KafkaLikeShardingStrategy(NUM_SHARDS),
-          objectMapper,
-          Level.FINE,
-          true);
-    }
-
-    @Bean
-    ObjectMapper objectMapper()
-    {
-      ObjectMapper objectMapper = new ObjectMapper();
-      objectMapper.registerModule(new JavaTimeModule());
-      return objectMapper;
-    }
-
-    @Bean
-    Clock clock()
-    {
-      return Clock.systemDefaultZone();
-    }
-
-    int[] ownedShards()
-    {
-      return new int[] { OWNED_SHARD };
-    }
-
-    int bufferSize()
-    {
-      return 8;
-    }
-  }
 }
index e57d06d..09369cb 100644 (file)
@@ -1,64 +1,13 @@
 package de.juplo.kafka.chat.backend.implementation.inmemory;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 import de.juplo.kafka.chat.backend.domain.ChatHomeServiceTest;
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
-import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
-import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.context.annotation.Bean;
-
-import java.nio.file.Paths;
-import java.time.Clock;
-import java.util.logging.Level;
+import org.springframework.test.context.TestPropertySource;
 
 
+@TestPropertySource(properties = {
+    "chat.backend.inmemory.sharding-strategy=none",
+    "chat.backend.inmemory.storage-strategy=files",
+    "chat.backend.inmemory.storage-directory=target/test-classes/data/files" })
 public class SimpleChatHomeServiceTest extends ChatHomeServiceTest
 {
-  @TestConfiguration
-  static class Configuration
-  {
-    @Bean
-    SimpleChatHomeService chatHome(
-        StorageStrategy storageStrategy,
-        Clock clock)
-    {
-      return new SimpleChatHomeService(
-          storageStrategy,
-          clock,
-          bufferSize());
-    }
-
-    @Bean
-    FilesStorageStrategy storageStrategy(
-        Clock clock,
-        ObjectMapper objectMapper)
-    {
-      return new FilesStorageStrategy(
-          Paths.get("target", "test-classes", "data", "files"),
-          chatRoomId -> 0,
-          objectMapper,
-          Level.FINE,
-          true);
-    }
-
-    @Bean
-    ObjectMapper objectMapper()
-    {
-      ObjectMapper objectMapper = new ObjectMapper();
-      objectMapper.registerModule(new JavaTimeModule());
-      return objectMapper;
-    }
-
-    @Bean
-    Clock clock()
-    {
-      return Clock.systemDefaultZone();
-    }
-
-    int bufferSize()
-    {
-      return 8;
-    }
-  }
 }
index e345a75..180ff15 100644 (file)
@@ -7,23 +7,22 @@ import org.junit.jupiter.api.BeforeAll;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
 import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
-import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
 
 import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS;
 import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.DATA_TOPIC;
 import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.INFO_TOPIC;
 
 
-@SpringBootTest(
-    classes = {
+@ContextConfiguration(classes = {
         KafkaTestUtils.KafkaTestConfiguration.class,
         KafkaAutoConfiguration.class,
         TaskExecutionAutoConfiguration.class,
-    },
-    properties = {
-        "spring.main.allow-bean-definition-overriding=true",
+    })
+@TestPropertySource(properties = {
         "chat.backend.services=kafka",
         "chat.backend.kafka.client-id-PREFIX=TEST",
         "chat.backend.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
@@ -34,7 +33,7 @@ import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServ
 })
 @EmbeddedKafka(
     topics = { INFO_TOPIC, DATA_TOPIC },
-    partitions = 10)
+    partitions = NUM_SHARDS)
 @Slf4j
 public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
 {