From 6c342d1af990d8f03d2cb937f61056489cec57aa Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Thu, 22 Feb 2024 13:10:09 +0100 Subject: [PATCH] test: Simplified & unified test-setup for ``ChatHomeServiceTest``s * Using existing configuration-classes instead of hand-coded configuartion. * Aligned the `KafkaChatHomeServiceTest` with the `in-memory`-tests. --- .../backend/domain/ChatHomeServiceTest.java | 34 ++++++- .../inmemory/ShardedChatHomeServiceTest.java | 90 +++---------------- .../inmemory/SimpleChatHomeServiceTest.java | 61 ++----------- .../kafka/KafkaChatHomeServiceTest.java | 13 ++- 4 files changed, 52 insertions(+), 146 deletions(-) diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeServiceTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeServiceTest.java index 984da1de..2db530e3 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeServiceTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeServiceTest.java @@ -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(); + } + } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeServiceTest.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeServiceTest.java index 8e21c5f0..95501ad9 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeServiceTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeServiceTest.java @@ -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; - } - } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeServiceTest.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeServiceTest.java index e57d06dc..09369cba 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeServiceTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeServiceTest.java @@ -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; - } - } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java index e345a751..180ff152 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java @@ -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 { -- 2.20.1