Skip to content

Commit

Permalink
Add MongoDBDatabaseNameTest
Browse files Browse the repository at this point in the history
  • Loading branch information
silaev committed Jul 15, 2020
1 parent 4bef95b commit 2086fd6
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
14 changes: 14 additions & 0 deletions modules/mongodb/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 2086fd6

Please sign in to comment.