Skip to content

Commit

Permalink
Switch tests to use Testcontainers (#245)
Browse files Browse the repository at this point in the history
Closes #244
  • Loading branch information
sleberknight authored Oct 17, 2024
1 parent 288aad0 commit 22e99dc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
15 changes: 11 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,22 @@
</dependency>

<!-- Test dependencies -->

<dependency>
<groupId>org.kiwiproject</groupId>
<artifactId>kiwi-test</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>de.bwaldvogel</groupId>
<artifactId>mongo-java-server</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.kiwiproject</groupId>
<artifactId>kiwi-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
32 changes: 21 additions & 11 deletions src/test/java/org/kiwiproject/migrations/mongo/DbCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TestMigrationConfiguration> migrationsBundle = new MongoMigrationsBundle<>() {

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 22e99dc

Please sign in to comment.