From 4362543d70a78ac546e44d4cfe4d8e91820564ad Mon Sep 17 00:00:00 2001
From: Scott Leberknight <174812+sleberknight@users.noreply.github.com>
Date: Wed, 16 Oct 2024 22:07:51 -0400
Subject: [PATCH] Convert all Mongo tests to use Testcontainers (#1208)
Closes #1207
---
pom.xml | 6 --
.../junit/jupiter/MongoServerExtension.java | 30 ---------
.../context/MongoRepositoryContextTest.java | 14 ++--
.../AbstractPagingQueryIntegrationTest.java | 34 +---------
.../spring/data/KiwiMongoConvertersTest.java | 15 +++--
.../spring/data/KiwiMongoIndexesTest.java | 15 +++--
.../data/KiwiSpringMongoQueriesTest.java | 54 ++++++++++-----
.../PagingQueryMongo4IntegrationTest.java | 3 +
.../PagingQueryMongo5IntegrationTest.java | 3 +
.../PagingQueryMongo6IntegrationTest.java | 3 +
.../PagingQueryMongo7IntegrationTest.java | 3 +
... => PagingQueryMongo8IntegrationTest.java} | 9 ++-
...PagingQueryMongoLatestIntegrationTest.java | 23 +++++++
.../util/MongoTestContainerHelpers.java | 65 +++++++++++++++++++
.../spring/util/MongoTestHelpers.java | 30 ---------
15 files changed, 170 insertions(+), 137 deletions(-)
delete mode 100644 src/test/java/org/kiwiproject/junit/jupiter/MongoServerExtension.java
rename src/test/java/org/kiwiproject/spring/data/{PagingQueryMongo3IntegrationTest.java => PagingQueryMongo8IntegrationTest.java} (64%)
create mode 100644 src/test/java/org/kiwiproject/spring/data/PagingQueryMongoLatestIntegrationTest.java
create mode 100644 src/test/java/org/kiwiproject/spring/util/MongoTestContainerHelpers.java
delete mode 100644 src/test/java/org/kiwiproject/spring/util/MongoTestHelpers.java
diff --git a/pom.xml b/pom.xml
index 66f053a7..4cba5c5e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -223,12 +223,6 @@
test
-
- de.bwaldvogel
- mongo-java-server
- test
-
-
org.testcontainers
junit-jupiter
diff --git a/src/test/java/org/kiwiproject/junit/jupiter/MongoServerExtension.java b/src/test/java/org/kiwiproject/junit/jupiter/MongoServerExtension.java
deleted file mode 100644
index 9015016b..00000000
--- a/src/test/java/org/kiwiproject/junit/jupiter/MongoServerExtension.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package org.kiwiproject.junit.jupiter;
-
-import static org.kiwiproject.spring.util.MongoTestHelpers.mongoConnectionString;
-import static org.kiwiproject.spring.util.MongoTestHelpers.startInMemoryMongoServer;
-
-import de.bwaldvogel.mongo.MongoServer;
-import lombok.Getter;
-import org.junit.jupiter.api.extension.AfterAllCallback;
-import org.junit.jupiter.api.extension.BeforeAllCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
-
-public class MongoServerExtension implements BeforeAllCallback, AfterAllCallback {
-
- @Getter
- private MongoServer mongoServer;
-
- @Override
- public void beforeAll(ExtensionContext context) {
- mongoServer = startInMemoryMongoServer();
- }
-
- @Override
- public void afterAll(ExtensionContext context) {
- mongoServer.shutdownNow();
- }
-
- public String getConnectionString() {
- return mongoConnectionString(mongoServer);
- }
-}
diff --git a/src/test/java/org/kiwiproject/spring/context/MongoRepositoryContextTest.java b/src/test/java/org/kiwiproject/spring/context/MongoRepositoryContextTest.java
index 86238cb3..31fb48da 100644
--- a/src/test/java/org/kiwiproject/spring/context/MongoRepositoryContextTest.java
+++ b/src/test/java/org/kiwiproject/spring/context/MongoRepositoryContextTest.java
@@ -3,6 +3,8 @@
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.connectionStringFor;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
import lombok.Data;
import lombok.NoArgsConstructor;
@@ -13,10 +15,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
-import org.kiwiproject.junit.jupiter.MongoServerExtension;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
@@ -24,22 +24,26 @@
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
import org.springframework.data.mongodb.repository.MongoRepository;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@DisplayName("MongoRepositoryContext")
+@Testcontainers(disabledWithoutDocker = true)
class MongoRepositoryContextTest {
- @RegisterExtension
- static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer();
private MongoRepositoryContext mongoRepositoryContext;
@BeforeEach
void setUp() {
- var connectionString = MONGO_SERVER_EXTENSION.getConnectionString();
+ var connectionString = connectionStringFor(MONGODB, "test");
mongoRepositoryContext = new MongoRepositoryContext(connectionString);
}
diff --git a/src/test/java/org/kiwiproject/spring/data/AbstractPagingQueryIntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/AbstractPagingQueryIntegrationTest.java
index 54ea4393..9a34d5ea 100644
--- a/src/test/java/org/kiwiproject/spring/data/AbstractPagingQueryIntegrationTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/AbstractPagingQueryIntegrationTest.java
@@ -3,11 +3,9 @@
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingDouble;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.kiwiproject.base.KiwiStrings.f;
import static org.kiwiproject.spring.data.OrderTestData.ORDER_COLLECTION;
import static org.kiwiproject.spring.data.OrderTestData.insertSampleOrders;
-import com.mongodb.client.MongoClients;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
@@ -22,9 +20,7 @@
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.testcontainers.containers.MongoDBContainer;
-import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
import org.testcontainers.junit.jupiter.Testcontainers;
-import org.testcontainers.utility.DockerImageName;
import java.util.List;
import java.util.function.Predicate;
@@ -42,9 +38,7 @@
@Testcontainers(disabledWithoutDocker = true)
@ExtendWith(SoftAssertionsExtension.class)
@Slf4j
-abstract class AbstractPagingQueryIntegrationTest {
-
- static final int STANDARD_MONGODB_PORT = 27_017;
+public abstract class AbstractPagingQueryIntegrationTest {
/**
* Subclasses must set this in a {@code BeforeAll}.
@@ -54,32 +48,6 @@ abstract class AbstractPagingQueryIntegrationTest {
List storedOrders;
int storedOrderCount;
- /**
- * Subclasses should use this to create the test container, supplying the Docker image name for a specific
- * version of Mongo.
- */
- @SuppressWarnings("resource") // because Testcontainers closs it for us
- static MongoDBContainer newMongoDBContainer(String dockerImageName) {
- LOG.info("Create MongoDBcontainer for Docker image name: {}", dockerImageName);
- return new MongoDBContainer(DockerImageName.parse(dockerImageName)).waitingFor(new HostPortWaitStrategy());
- }
-
- /**
- * Subclassses should use this to create and set the static MongoTemplate field.
- */
- static MongoTemplate newMongoTemplate(MongoDBContainer container) {
- var mongoClient = MongoClients.create(connectionStringFor(container));
- return new MongoTemplate(mongoClient, "test");
- }
-
- private static String connectionStringFor(MongoDBContainer container) {
- var host = container.getHost();
- var port = container.getMappedPort(STANDARD_MONGODB_PORT);
- var connectionString = f("mongodb://{}:{}", host, port);
- LOG.info("Mongo connection string: {}", connectionString);
- return connectionString;
- }
-
@BeforeEach
void setUp() {
storedOrders = insertSampleOrders(mongoTemplate);
diff --git a/src/test/java/org/kiwiproject/spring/data/KiwiMongoConvertersTest.java b/src/test/java/org/kiwiproject/spring/data/KiwiMongoConvertersTest.java
index f8d1e055..af941ddb 100644
--- a/src/test/java/org/kiwiproject/spring/data/KiwiMongoConvertersTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/KiwiMongoConvertersTest.java
@@ -1,28 +1,31 @@
package org.kiwiproject.spring.data;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.kiwiproject.spring.util.MongoTestHelpers.newMongoTemplate;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
import org.bson.BsonUndefined;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
-import org.kiwiproject.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;
@DisplayName("KiwiMongoConverters")
+@Testcontainers(disabledWithoutDocker = true)
class KiwiMongoConvertersTest {
- @RegisterExtension
- static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer();
private MongoTemplate mongoTemplate;
@BeforeEach
void setUp() {
- mongoTemplate = newMongoTemplate(MONGO_SERVER_EXTENSION.getMongoServer());
+ mongoTemplate = newMongoTemplate(MONGODB);
}
@Nested
diff --git a/src/test/java/org/kiwiproject/spring/data/KiwiMongoIndexesTest.java b/src/test/java/org/kiwiproject/spring/data/KiwiMongoIndexesTest.java
index f9eda788..10301329 100644
--- a/src/test/java/org/kiwiproject/spring/data/KiwiMongoIndexesTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/KiwiMongoIndexesTest.java
@@ -2,7 +2,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
-import static org.kiwiproject.spring.util.MongoTestHelpers.newMongoTemplate;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -11,21 +12,23 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
-import org.kiwiproject.junit.jupiter.MongoServerExtension;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.data.mongodb.core.mapping.Document;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.Collection;
@DisplayName("KiwiMongoIndexes")
+@Testcontainers(disabledWithoutDocker = true)
class KiwiMongoIndexesTest {
- @RegisterExtension
- static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer();
private static final String PEOPLE_COLLECTION_NAME = "people";
@@ -33,7 +36,7 @@ class KiwiMongoIndexesTest {
@BeforeEach
void setUp() {
- mongoTemplate = newMongoTemplate(MONGO_SERVER_EXTENSION.getMongoServer());
+ mongoTemplate = newMongoTemplate(MONGODB);
mongoTemplate.createCollection(PEOPLE_COLLECTION_NAME);
}
diff --git a/src/test/java/org/kiwiproject/spring/data/KiwiSpringMongoQueriesTest.java b/src/test/java/org/kiwiproject/spring/data/KiwiSpringMongoQueriesTest.java
index 65ba878b..51a1a064 100644
--- a/src/test/java/org/kiwiproject/spring/data/KiwiSpringMongoQueriesTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/KiwiSpringMongoQueriesTest.java
@@ -2,6 +2,7 @@
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingDouble;
+import static java.util.Comparator.reverseOrder;
import static org.apache.commons.lang3.StringUtils.containsAny;
import static org.assertj.core.api.Assertions.assertThat;
import static org.kiwiproject.collect.KiwiLists.first;
@@ -10,6 +11,7 @@
import static org.kiwiproject.spring.data.KiwiSpringMongoQueries.addMultiplePartialOrEqualMatchCriteria;
import static org.kiwiproject.spring.data.KiwiSpringMongoQueries.addPartialOrEqualMatchCriteria;
import static org.kiwiproject.spring.data.OrderTestData.insertSampleOrders;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.SoftAssertions;
@@ -20,19 +22,21 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.kiwiproject.base.KiwiDoubles;
-import org.kiwiproject.junit.jupiter.MongoServerExtension;
import org.kiwiproject.search.KiwiSearching;
import org.kiwiproject.spring.data.KiwiSpringMongoQueries.PartialMatchType;
import org.kiwiproject.time.KiwiInstants;
+import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.query.Criteria;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.time.Instant;
import java.util.List;
@@ -40,12 +44,13 @@
import java.util.stream.DoubleStream;
@DisplayName("KiwiSpringMongoQueries")
+@Testcontainers(disabledWithoutDocker = true)
@ExtendWith(SoftAssertionsExtension.class)
@Slf4j
class KiwiSpringMongoQueriesTest {
- @RegisterExtension
- static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer();
private static MongoTemplate mongoTemplate;
@@ -54,7 +59,7 @@ class KiwiSpringMongoQueriesTest {
@BeforeAll
static void beforeAll() {
- var connectionString = MONGO_SERVER_EXTENSION.getConnectionString();
+ var connectionString = MONGODB.getConnectionString() + "/test";
var mongoClientDbFactory = new SimpleMongoClientDatabaseFactory(connectionString);
mongoTemplate = new MongoTemplate(mongoClientDbFactory);
}
@@ -113,12 +118,13 @@ void shouldPaginate_WithAscendingSort(SoftAssertions softly) {
softly.assertThat(orderPage.getSize()).isEqualTo(limit);
softly.assertThat(orderPage.getSort()).isEqualTo(Sort.by(Sort.Direction.ASC, "customerId"));
- var expectedOrders = storedOrders.stream()
- .sorted(comparing(Order::getCustomerId))
+ var expectedCustomerIds = storedOrders.stream()
+ .map(Order::getCustomerId)
+ .sorted()
.limit(limit)
.toList();
- softly.assertThat(orderPage.getContent()).isEqualTo(expectedOrders);
+ softly.assertThat(customerIds(orderPage)).isEqualTo(expectedCustomerIds);
}
@Test
@@ -172,12 +178,13 @@ void shouldPaginate_WithDescendingSort(SoftAssertions softly) {
softly.assertThat(orderPage.getSize()).isEqualTo(limit);
softly.assertThat(orderPage.getSort()).isEqualTo(Sort.by(Sort.Direction.DESC, "customerId"));
- var expectedOrders = storedOrders.stream()
- .sorted(comparing(Order::getCustomerId).reversed())
+ var expectedCustomerIds = storedOrders.stream()
+ .map(Order::getCustomerId)
+ .sorted(reverseOrder())
.limit(limit)
.toList();
- softly.assertThat(orderPage.getContent()).isEqualTo(expectedOrders);
+ softly.assertThat(customerIds(orderPage)).isEqualTo(expectedCustomerIds);
}
@Test
@@ -190,8 +197,8 @@ void shouldPaginate_GettingSecondPage(SoftAssertions softly) {
pagingParams.setLimit(limit);
pagingParams.setPrimarySort("customerId");
pagingParams.setPrimaryDirection(Sort.Direction.ASC);
- pagingParams.setSecondarySort("status");
- pagingParams.setSecondaryDirection(Sort.Direction.ASC);
+ pagingParams.setSecondarySort("amount");
+ pagingParams.setSecondaryDirection(Sort.Direction.DESC);
var orderPage = KiwiSpringMongoQueries.paginate(mongoTemplate, pagingParams, Order.class);
@@ -201,10 +208,13 @@ void shouldPaginate_GettingSecondPage(SoftAssertions softly) {
softly.assertThat(orderPage.getNumberOfElements()).isEqualTo(limit);
softly.assertThat(orderPage.getSize()).isEqualTo(limit);
softly.assertThat(orderPage.getSort()).isEqualTo(
- Sort.by(new Sort.Order(Sort.Direction.ASC, "customerId"), new Sort.Order(Sort.Direction.ASC, "status")));
+ Sort.by(new Sort.Order(Sort.Direction.ASC, "customerId"),
+ new Sort.Order(Sort.Direction.DESC, "amount"))
+ );
var expectedOrders = storedOrders.stream()
- .sorted(comparing(Order::getCustomerId).thenComparing(Order::getStatus))
+ .sorted(comparing(Order::getCustomerId)
+ .thenComparing(comparing(Order::getAmount).reversed()))
.skip(pageNumber * limit)
.limit(limit)
.toList();
@@ -445,13 +455,21 @@ void shouldAddPartialOrEqualMatchCriteria_WithPartialMatch(SoftAssertions softly
softly.assertThat(orderPage.getSize()).isEqualTo(limit);
softly.assertThat(orderPage.getSort()).isEqualTo(Sort.by(Sort.Direction.ASC, "customerId"));
- var expectedOrders = storedOrders.stream()
+ var expectedCustomerIds = storedOrders.stream()
.filter(customerFilter)
- .sorted(comparing(Order::getCustomerId))
+ .map(Order::getCustomerId)
+ .sorted()
.limit(limit)
.toList();
- softly.assertThat(orderPage.getContent()).isEqualTo(expectedOrders);
+ softly.assertThat(customerIds(orderPage)).isEqualTo(expectedCustomerIds);
+ }
+
+ private static List customerIds(Page orderPage) {
+ return orderPage.getContent()
+ .stream()
+ .map(Order::getCustomerId)
+ .toList();
}
@Test
diff --git a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo4IntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo4IntegrationTest.java
index 22f92bd3..2eda838b 100644
--- a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo4IntegrationTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo4IntegrationTest.java
@@ -1,5 +1,8 @@
package org.kiwiproject.spring.data;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
+
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.testcontainers.containers.MongoDBContainer;
diff --git a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo5IntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo5IntegrationTest.java
index 8743f8ba..4ae43161 100644
--- a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo5IntegrationTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo5IntegrationTest.java
@@ -1,5 +1,8 @@
package org.kiwiproject.spring.data;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
+
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.testcontainers.containers.MongoDBContainer;
diff --git a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo6IntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo6IntegrationTest.java
index 671025c1..76d9d883 100644
--- a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo6IntegrationTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo6IntegrationTest.java
@@ -1,5 +1,8 @@
package org.kiwiproject.spring.data;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
+
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.testcontainers.containers.MongoDBContainer;
diff --git a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo7IntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo7IntegrationTest.java
index 63292472..6fe45f93 100644
--- a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo7IntegrationTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo7IntegrationTest.java
@@ -1,5 +1,8 @@
package org.kiwiproject.spring.data;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
+
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.testcontainers.containers.MongoDBContainer;
diff --git a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo3IntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo8IntegrationTest.java
similarity index 64%
rename from src/test/java/org/kiwiproject/spring/data/PagingQueryMongo3IntegrationTest.java
rename to src/test/java/org/kiwiproject/spring/data/PagingQueryMongo8IntegrationTest.java
index 15d06183..4d265d11 100644
--- a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo3IntegrationTest.java
+++ b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongo8IntegrationTest.java
@@ -1,17 +1,20 @@
package org.kiwiproject.spring.data;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
+
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
-@DisplayName("PagingQuery (Mongo 3)")
+@DisplayName("PagingQuery (Mongo 8)")
@Testcontainers(disabledWithoutDocker = true)
-class PagingQueryMongo3IntegrationTest extends AbstractPagingQueryIntegrationTest {
+class PagingQueryMongo8IntegrationTest extends AbstractPagingQueryIntegrationTest {
@Container
- static final MongoDBContainer MONGODB = newMongoDBContainer("mongo:3");
+ static final MongoDBContainer MONGODB = newMongoDBContainer("mongo:8");
@BeforeAll
static void beforeAll() {
diff --git a/src/test/java/org/kiwiproject/spring/data/PagingQueryMongoLatestIntegrationTest.java b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongoLatestIntegrationTest.java
new file mode 100644
index 00000000..d1b0967d
--- /dev/null
+++ b/src/test/java/org/kiwiproject/spring/data/PagingQueryMongoLatestIntegrationTest.java
@@ -0,0 +1,23 @@
+package org.kiwiproject.spring.data;
+
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoDBContainer;
+import static org.kiwiproject.spring.util.MongoTestContainerHelpers.newMongoTemplate;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.DisplayName;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@DisplayName("PagingQuery (Mongo Latest)")
+@Testcontainers(disabledWithoutDocker = true)
+class PagingQueryMongoLatestIntegrationTest extends AbstractPagingQueryIntegrationTest {
+
+ @Container
+ static final MongoDBContainer MONGODB = newMongoDBContainer("mongo:latest");
+
+ @BeforeAll
+ static void beforeAll() {
+ mongoTemplate = newMongoTemplate(MONGODB);
+ }
+}
diff --git a/src/test/java/org/kiwiproject/spring/util/MongoTestContainerHelpers.java b/src/test/java/org/kiwiproject/spring/util/MongoTestContainerHelpers.java
new file mode 100644
index 00000000..0d6912d4
--- /dev/null
+++ b/src/test/java/org/kiwiproject/spring/util/MongoTestContainerHelpers.java
@@ -0,0 +1,65 @@
+package org.kiwiproject.spring.util;
+
+import static java.util.Objects.isNull;
+import static org.kiwiproject.base.KiwiStrings.f;
+
+import com.mongodb.client.MongoClients;
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.mongodb.core.MongoTemplate;
+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 int STANDARD_MONGODB_PORT = 27_017;
+
+ 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());
+ }
+
+ public static MongoTemplate newMongoTemplate(MongoDBContainer container) {
+ var mongoClient = MongoClients.create(connectionStringFor(container));
+ return new MongoTemplate(mongoClient, "test");
+ }
+
+ public static String connectionStringFor(MongoDBContainer container) {
+ var host = container.getHost();
+ var port = container.getMappedPort(STANDARD_MONGODB_PORT);
+ var connectionString = f("mongodb://{}:{}", host, port);
+ logConnectionString(connectionString);
+ return connectionString;
+ }
+
+ public static String connectionStringFor(MongoDBContainer container, String databaseName) {
+ var host = container.getHost();
+ var port = container.getMappedPort(STANDARD_MONGODB_PORT);
+ var connectionString = f("mongodb://{}:{}/{}", host, port, databaseName);
+ logConnectionString(connectionString);
+ return connectionString;
+ }
+
+ private static void logConnectionString(String connectionString) {
+ LOG.info("Mongo connection string: {}", connectionString);
+ }
+}
diff --git a/src/test/java/org/kiwiproject/spring/util/MongoTestHelpers.java b/src/test/java/org/kiwiproject/spring/util/MongoTestHelpers.java
deleted file mode 100644
index 3b76be75..00000000
--- a/src/test/java/org/kiwiproject/spring/util/MongoTestHelpers.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package org.kiwiproject.spring.util;
-
-import static org.kiwiproject.base.KiwiStrings.f;
-
-import de.bwaldvogel.mongo.MongoServer;
-import de.bwaldvogel.mongo.backend.memory.MemoryBackend;
-import lombok.experimental.UtilityClass;
-import org.springframework.data.mongodb.core.MongoTemplate;
-import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
-
-@UtilityClass
-public class MongoTestHelpers {
-
- public static MongoServer startInMemoryMongoServer() {
- var mongoServer = new MongoServer(new MemoryBackend());
- mongoServer.bind();
- return mongoServer;
- }
-
- public static String mongoConnectionString(MongoServer mongoServer) {
- var addr = mongoServer.getLocalAddress();
- return f("mongodb://{}:{}/test_db_{}", addr.getHostName(), addr.getPort(), System.currentTimeMillis());
- }
-
- public static MongoTemplate newMongoTemplate(MongoServer mongoServer) {
- var connectionString = MongoTestHelpers.mongoConnectionString(mongoServer);
- var factory = new SimpleMongoClientDatabaseFactory(connectionString);
- return new MongoTemplate(factory);
- }
-}