From ce74b0d9aed8ea54dd2b95a25997b6a7663ab431 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 10 Jan 2023 19:17:15 +0100 Subject: [PATCH] WIP --- pom.xml | 28 +++++++- .../InMemoryWithMongoDbStorageStrategyIT.java | 70 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java diff --git a/pom.xml b/pom.xml index 4d3d183a..f9c93a0c 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,17 @@ 17 1.0.8 + + + + org.testcontainers + testcontainers-bom + 1.17.6 + pom + import + + + org.springframework.boot @@ -60,9 +71,20 @@ test - de.flapdoodle.embed - de.flapdoodle.embed.mongo - 4.3.2 + org.testcontainers + testcontainers + test + + + + org.testcontainers + junit-jupiter + test + + + + org.testcontainers + mongodb test diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java new file mode 100644 index 00000000..386c8202 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java @@ -0,0 +1,70 @@ +package de.juplo.kafka.chat.backend.persistence; + +import de.juplo.kafka.chat.backend.domain.ChatHomeService; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService; +import de.juplo.kafka.chat.backend.persistence.InMemoryWithMongoDbStorageStrategyIT.DataSourceInitializer; +import de.juplo.kafka.chat.backend.persistence.storage.mongodb.ChatHomeRepository; +import de.juplo.kafka.chat.backend.persistence.storage.mongodb.MongoDbStorageStrategy; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.support.TestPropertySourceUtils; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.time.Clock; +import java.util.function.Supplier; + + +@Testcontainers +@DataMongoTest +@ContextConfiguration(initializers = DataSourceInitializer.class) +@Slf4j +public class InMemoryWithMongoDbStorageStrategyIT extends AbstractStorageStrategyIT +{ + @Autowired + MongoDbStorageStrategy storageStrategy; + @Autowired + Clock clock; + @Autowired + ChatHomeRepository repository; + + + @Override + protected StorageStrategy getStorageStrategy() + { + return storageStrategy; + } + + @Override + protected Supplier chatHomeServiceSupplier() + { + return () -> new InMemoryChatHomeService(getStorageStrategy().read(), clock, 8); + } + + @Container + private static final MongoDBContainer mongodb = + new MongoDBContainer("mongo:6") + .withEnv("MONGO_INITDB_ROOT_USERNAME", "mongo") + .withEnv("MONGO_INITDB_ROOT_PASSWORD", "mongo"); + + public static class DataSourceInitializer + implements ApplicationContextInitializer + { + @Override + public void initialize(ConfigurableApplicationContext applicationContext) + { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment( + applicationContext, + "spring.data.mongodb.host=localhost", + "spring.data.mongodb.port=27017", + "spring.data.mongodb.database=test", + "spring.data.mongodb.username=mongo", + "spring.data.mongodb.password=mongo"); + } + } +} -- 2.20.1