forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
115 changes: 115 additions & 0 deletions
115
modules/mongodb/src/test/java/org/testcontainers/containers/MongoDBDatabaseNameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |