Skip to content

Commit

Permalink
Convert all Mongo tests to use Testcontainers (#1208)
Browse files Browse the repository at this point in the history
Closes #1207
  • Loading branch information
sleberknight authored Oct 17, 2024
1 parent 74a1f46 commit 4362543
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 137 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@
<scope>test</scope>
</dependency>

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

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,33 +15,35 @@
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;
import org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent;
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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}.
Expand All @@ -54,32 +48,6 @@ abstract class AbstractPagingQueryIntegrationTest {
List<Order> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,29 +12,31 @@
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";

private MongoTemplate mongoTemplate;

@BeforeEach
void setUp() {
mongoTemplate = newMongoTemplate(MONGO_SERVER_EXTENSION.getMongoServer());
mongoTemplate = newMongoTemplate(MONGODB);

mongoTemplate.createCollection(PEOPLE_COLLECTION_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -20,32 +22,35 @@
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;
import java.util.function.Predicate;
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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -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<String> customerIds(Page<Order> orderPage) {
return orderPage.getContent()
.stream()
.map(Order::getCustomerId)
.toList();
}

@Test
Expand Down
Loading

0 comments on commit 4362543

Please sign in to comment.