From: Kai Moritz Date: Thu, 22 Feb 2024 14:35:39 +0000 (+0100) Subject: test: Simplified the integration-tests for `StorageStrategy` X-Git-Tag: rebase--2024-02-23--16-28~22 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=47d4a54aaa1ce13180ab6fdad0569cf2d0851563;p=demos%2Fkafka%2Fchat test: Simplified the integration-tests for `StorageStrategy` --- diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java index 3fea43ed..0ec0bc19 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java @@ -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(); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java index 41e80ed7..9568545a 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java @@ -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(); + } } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java index 78f46250..e4eaf3ab 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java @@ -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 diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java index 7055af54..8f4e37a3 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java @@ -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 index 00000000..3f6cf9c0 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java @@ -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); + } +}