From 22e99dcf3b3fc2efd9a6a0834f1af764653ff1da Mon Sep 17 00:00:00 2001
From: Scott Leberknight <174812+sleberknight@users.noreply.github.com>
Date: Wed, 16 Oct 2024 20:58:41 -0400
Subject: [PATCH] Switch tests to use Testcontainers (#245)
Closes #244
---
pom.xml | 15 +++++---
.../migrations/mongo/DbCommandTest.java | 32 +++++++++++------
.../mongo/MongoMigrationsBundleTest.java | 15 ++++----
.../mongo/MongoTestContainerHelpers.java | 34 +++++++++++++++++++
4 files changed, 75 insertions(+), 21 deletions(-)
create mode 100644 src/test/java/org/kiwiproject/migrations/mongo/MongoTestContainerHelpers.java
diff --git a/pom.xml b/pom.xml
index 40b0dcb..d374a98 100644
--- a/pom.xml
+++ b/pom.xml
@@ -96,15 +96,22 @@
+
- org.kiwiproject
- kiwi-test
+ org.testcontainers
+ junit-jupiter
test
- de.bwaldvogel
- mongo-java-server
+ org.testcontainers
+ mongodb
+ test
+
+
+
+ org.kiwiproject
+ kiwi-test
test
diff --git a/src/test/java/org/kiwiproject/migrations/mongo/DbCommandTest.java b/src/test/java/org/kiwiproject/migrations/mongo/DbCommandTest.java
index 1cab48d..45762b4 100644
--- a/src/test/java/org/kiwiproject/migrations/mongo/DbCommandTest.java
+++ b/src/test/java/org/kiwiproject/migrations/mongo/DbCommandTest.java
@@ -2,40 +2,47 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.kiwiproject.migrations.mongo.MongoTestContainerHelpers.newMongoDBContainer;
import com.mongodb.client.MongoClients;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
-import org.kiwiproject.test.junit.jupiter.MongoServerExtension;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;
+@DisplayName("DbCommand")
+@Testcontainers(disabledWithoutDocker = true)
class DbCommandTest {
- @RegisterExtension
- static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer();
private String mongoConnectionString;
private String mongoDatabaseName;
@BeforeEach
void setUp() {
- mongoConnectionString = MONGO_SERVER_EXTENSION.getConnectionString();
- mongoDatabaseName = MONGO_SERVER_EXTENSION.getTestDatabaseName();
+ mongoConnectionString = MONGODB.getConnectionString();
+ mongoDatabaseName = "test";
}
@Test
void testRunSubCommandWithMongoDatabase() {
var dbCommand = new DbCommand<>("db",
- new TestMongoMigrationConfiguration(mongoConnectionString,
- mongoDatabaseName, "org.kiwiproject.migrations.mongo.samples.mongodatabase"),
+ new TestMongoMigrationConfiguration(
+ mongoConnectionString,
+ mongoDatabaseName,
+ "org.kiwiproject.migrations.mongo.samples.mongodatabase"),
TestMigrationConfiguration.class);
dbCommand.run(null, new Namespace(Map.of("subcommand", "migrate")), new TestMigrationConfiguration());
@@ -50,7 +57,8 @@ void testRunSubCommandWithMongoDatabase() {
void testRunSubCommandWithMongoTemplate() {
var dbCommand = new DbCommand<>("db",
new TestMongoMigrationConfiguration(mongoConnectionString,
- mongoDatabaseName, "org.kiwiproject.migrations.mongo.samples.mongotemplate"),
+ mongoDatabaseName,
+ "org.kiwiproject.migrations.mongo.samples.mongotemplate"),
TestMigrationConfiguration.class);
dbCommand.run(null, new Namespace(Map.of("subcommand", "migrate")), new TestMigrationConfiguration());
@@ -65,7 +73,8 @@ void testRunSubCommandWithMongoTemplate() {
void testPrintHelp() {
var dbCommand = new DbCommand<>("db",
new TestMongoMigrationConfiguration(mongoConnectionString,
- mongoDatabaseName, "org.kiwiproject.migrations.mongo.samples.mongodatabase"),
+ mongoDatabaseName,
+ "org.kiwiproject.migrations.mongo.samples.mongodatabase"),
TestMigrationConfiguration.class);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -97,7 +106,8 @@ protected static Subparser createSubparser(AbstractMongockCommand> command) {
void shouldReturnConfigurationClass() {
var dbCommand = new DbCommand<>("db",
new TestMongoMigrationConfiguration(mongoConnectionString,
- mongoDatabaseName, "org.kiwiproject.migrations.mongo.samples.mongodatabase"),
+ mongoDatabaseName,
+ "org.kiwiproject.migrations.mongo.samples.mongodatabase"),
TestMigrationConfiguration.class);
assertThat(dbCommand.getConfigurationClass()).isEqualTo(TestMigrationConfiguration.class);
diff --git a/src/test/java/org/kiwiproject/migrations/mongo/MongoMigrationsBundleTest.java b/src/test/java/org/kiwiproject/migrations/mongo/MongoMigrationsBundleTest.java
index a06208d..d8dd12b 100644
--- a/src/test/java/org/kiwiproject/migrations/mongo/MongoMigrationsBundleTest.java
+++ b/src/test/java/org/kiwiproject/migrations/mongo/MongoMigrationsBundleTest.java
@@ -1,6 +1,7 @@
package org.kiwiproject.migrations.mongo;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.kiwiproject.migrations.mongo.MongoTestContainerHelpers.newMongoDBContainer;
import com.mongodb.client.MongoClients;
import io.dropwizard.core.Application;
@@ -9,14 +10,16 @@
import io.mongock.driver.api.driver.ConnectionDriver;
import io.mongock.driver.mongodb.springdata.v4.SpringDataMongoV4Driver;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
-import org.kiwiproject.test.junit.jupiter.MongoServerExtension;
import org.springframework.data.mongodb.core.MongoTemplate;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+@Testcontainers(disabledWithoutDocker = true)
class MongoMigrationsBundleTest {
- @RegisterExtension
- static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer();
private final MongoMigrationsBundle migrationsBundle = new MongoMigrationsBundle<>() {
@@ -27,12 +30,12 @@ public String getMigrationPackage(TestMigrationConfiguration config) {
@Override
public String getMongoUri(TestMigrationConfiguration config) {
- return MONGO_SERVER_EXTENSION.getConnectionString();
+ return MONGODB.getConnectionString();
}
@Override
public String getDatabaseName(TestMigrationConfiguration config) {
- return MONGO_SERVER_EXTENSION.getTestDatabaseName();
+ return "test";
}
@Override
diff --git a/src/test/java/org/kiwiproject/migrations/mongo/MongoTestContainerHelpers.java b/src/test/java/org/kiwiproject/migrations/mongo/MongoTestContainerHelpers.java
new file mode 100644
index 0000000..f37f13c
--- /dev/null
+++ b/src/test/java/org/kiwiproject/migrations/mongo/MongoTestContainerHelpers.java
@@ -0,0 +1,34 @@
+package org.kiwiproject.migrations.mongo;
+
+import static java.util.Objects.isNull;
+
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
+import org.testcontainers.utility.DockerImageName;
+
+@UtilityClass
+@Slf4j
+public class MongoTestContainerHelpers {
+
+ public static final String ENV_MONGO_IMAGE_NAME = "MONGO_IMAGE_NAME";
+
+ public static final String MONGO_LATEST_IMAGE_NAME = "mongo:latest";
+
+ public static MongoDBContainer newMongoDBContainer() {
+ var imageName = envMongoImageNameOrLatest();
+ return newMongoDBContainer(imageName);
+ }
+
+ private static String envMongoImageNameOrLatest() {
+ var envImageName = System.getenv(ENV_MONGO_IMAGE_NAME);
+ return isNull(envImageName) ? MONGO_LATEST_IMAGE_NAME : envImageName;
+ }
+
+ @SuppressWarnings("resource") // because Testcontainers closes it for us
+ public static MongoDBContainer newMongoDBContainer(String dockerImageName) {
+ LOG.info("Create MongoDBContainer for Docker image name: {}", dockerImageName);
+ return new MongoDBContainer(DockerImageName.parse(dockerImageName)).waitingFor(new HostPortWaitStrategy());
+ }
+}