WIP
authorKai Moritz <kai@juplo.de>
Tue, 10 Jan 2023 18:17:15 +0000 (19:17 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 10 Jan 2023 18:17:15 +0000 (19:17 +0100)
pom.xml
src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 4d3d183..f9c93a0 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                <java.version>17</java.version>
                <assertj-reactor.version>1.0.8</assertj-reactor.version>
        </properties>
+       <dependencyManagement>
+               <dependencies>
+                       <dependency>
+                               <groupId>org.testcontainers</groupId>
+                               <artifactId>testcontainers-bom</artifactId>
+                               <version>1.17.6</version>
+                               <type>pom</type>
+                               <scope>import</scope>
+                       </dependency>
+               </dependencies>
+       </dependencyManagement>
        <dependencies>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <scope>test</scope>
                </dependency>
                <dependency>
-                       <groupId>de.flapdoodle.embed</groupId>
-                       <artifactId>de.flapdoodle.embed.mongo</artifactId>
-                       <version>4.3.2</version>
+                       <groupId>org.testcontainers</groupId>
+                       <artifactId>testcontainers</artifactId>
+                       <scope>test</scope>
+               </dependency>
+       
+               <dependency>
+                       <groupId>org.testcontainers</groupId>
+                       <artifactId>junit-jupiter</artifactId>
+                       <scope>test</scope>
+               </dependency>
+       
+               <dependency>
+                       <groupId>org.testcontainers</groupId>
+                       <artifactId>mongodb</artifactId>
                        <scope>test</scope>
                </dependency>
        </dependencies>
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 (file)
index 0000000..386c820
--- /dev/null
@@ -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<ChatHomeService> 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<ConfigurableApplicationContext>
+  {
+    @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");
+    }
+  }
+}