diff --git a/modules/mongodb/build.gradle b/modules/mongodb/build.gradle index 57db1be21de..0bf702b34b5 100644 --- a/modules/mongodb/build.gradle +++ b/modules/mongodb/build.gradle @@ -1,7 +1,21 @@ +plugins { + id 'org.springframework.boot' version '2.3.1.RELEASE' + id 'io.spring.dependency-management' version '1.0.9.RELEASE' +} + description = "Testcontainers :: MongoDB" dependencies { compile project(':testcontainers') testCompile("org.mongodb:mongodb-driver-sync:4.0.2") + testCompile("org.springframework.boot:spring-boot-starter") + testCompile("org.springframework.boot:spring-boot-starter-test") { + exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' + } + testCompile("org.springframework.data:spring-data-mongodb:3.0.1.RELEASE") +} + +bootJar { + enabled = false } diff --git a/modules/mongodb/src/test/java/org/testcontainers/containers/MongoDBDatabaseNameTest.java b/modules/mongodb/src/test/java/org/testcontainers/containers/MongoDBDatabaseNameTest.java new file mode 100644 index 00000000000..03ddf5f16f3 --- /dev/null +++ b/modules/mongodb/src/test/java/org/testcontainers/containers/MongoDBDatabaseNameTest.java @@ -0,0 +1,115 @@ +package org.testcontainers.containers; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Objects; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.StreamSupport; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@Slf4j +@DataMongoTest +@ContextConfiguration(initializers = MongoDBDatabaseNameTest.Initializer.class) +@RunWith(SpringRunner.class) +public class MongoDBDatabaseNameTest { + private static final MongoDBContainer MONGO_DB_CONTAINER = new MongoDBContainer(); + private static final String DATABASE_NAME = "my-db"; + + @Autowired + private MongoTemplate mongoTemplate; + + @BeforeClass + public static void setUpAll() { + MONGO_DB_CONTAINER.start(); + } + + @AfterClass + public static void tearDownAll() { + MONGO_DB_CONTAINER.stop(); + } + + @Test + public void shouldTestDatabaseName() { + //1. Database name was already set to the MongoTemplate during auto-config. + assertEquals(DATABASE_NAME, mongoTemplate.getDb().getName()); + + try (final MongoClient mongoSyncClient = MongoClients.create(MONGO_DB_CONTAINER.getReplicaSetUrl(DATABASE_NAME))) { + //2. But the database is not created yet, because we have not performed any writing operation. + assertFalse( + "Database: " + DATABASE_NAME + " is supposed to be here", + isDatabaseInMongoDB(mongoSyncClient, DATABASE_NAME) + ); + + //3. Perform an operation to create a new collection via mongoTemplate. + mongoTemplate.createCollection(Product.class); + + //4. Now the database is created in MongoDB. + assertTrue( + "Database: " + DATABASE_NAME + " is supposed to be here", + isDatabaseInMongoDB(mongoSyncClient, DATABASE_NAME) + ); + } + } + + private boolean isDatabaseInMongoDB( + final MongoClient mongoSyncClient, + final String databaseName + ) { + Objects.requireNonNull(databaseName); + return StreamSupport.stream( + Spliterators.spliteratorUnknownSize( + mongoSyncClient.listDatabaseNames().iterator(), + Spliterator.ORDERED + ), + false + ).anyMatch(databaseName::equals); + } + + static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { + @Override + public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues.of( + String.format("spring.data.mongodb.uri: %s", MONGO_DB_CONTAINER.getReplicaSetUrl(DATABASE_NAME)) + ).applyTo(configurableApplicationContext); + } + } + + @SpringBootApplication + static class SpringBootApp { + public static void main(String[] args) { + } + } + + @Data + @AllArgsConstructor + @NoArgsConstructor + @Setter(AccessLevel.NONE) + private static class Product { + private Long article; + } +}