|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.mongodb.client.MongoClient; |
| 4 | +import com.mongodb.client.MongoClients; |
| 5 | +import lombok.AccessLevel; |
| 6 | +import lombok.AllArgsConstructor; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.NoArgsConstructor; |
| 9 | +import lombok.Setter; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.junit.AfterClass; |
| 13 | +import org.junit.BeforeClass; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 18 | +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; |
| 19 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 20 | +import org.springframework.context.ApplicationContextInitializer; |
| 21 | +import org.springframework.context.ConfigurableApplicationContext; |
| 22 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 23 | +import org.springframework.test.context.ContextConfiguration; |
| 24 | +import org.springframework.test.context.junit4.SpringRunner; |
| 25 | + |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.Spliterator; |
| 28 | +import java.util.Spliterators; |
| 29 | +import java.util.stream.StreamSupport; |
| 30 | + |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +@Slf4j |
| 36 | +@DataMongoTest |
| 37 | +@ContextConfiguration(initializers = MongoDBDatabaseNameTest.Initializer.class) |
| 38 | +@RunWith(SpringRunner.class) |
| 39 | +public class MongoDBDatabaseNameTest { |
| 40 | + private static final MongoDBContainer MONGO_DB_CONTAINER = new MongoDBContainer(); |
| 41 | + private static final String DATABASE_NAME = "my-db"; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private MongoTemplate mongoTemplate; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setUpAll() { |
| 48 | + MONGO_DB_CONTAINER.start(); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterClass |
| 52 | + public static void tearDownAll() { |
| 53 | + MONGO_DB_CONTAINER.stop(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldTestDatabaseName() { |
| 58 | + //1. Database name was already set to the MongoTemplate during auto-config. |
| 59 | + assertEquals(DATABASE_NAME, mongoTemplate.getDb().getName()); |
| 60 | + |
| 61 | + try (final MongoClient mongoSyncClient = MongoClients.create(MONGO_DB_CONTAINER.getReplicaSetUrl(DATABASE_NAME))) { |
| 62 | + //2. But the database is not created yet, because we have not performed any writing operation. |
| 63 | + assertFalse( |
| 64 | + "Database: " + DATABASE_NAME + " is supposed to be here", |
| 65 | + isDatabaseInMongoDB(mongoSyncClient, DATABASE_NAME) |
| 66 | + ); |
| 67 | + |
| 68 | + //3. Perform an operation to create a new collection via mongoTemplate. |
| 69 | + mongoTemplate.createCollection(Product.class); |
| 70 | + |
| 71 | + //4. Now the database is created in MongoDB. |
| 72 | + assertTrue( |
| 73 | + "Database: " + DATABASE_NAME + " is supposed to be here", |
| 74 | + isDatabaseInMongoDB(mongoSyncClient, DATABASE_NAME) |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private boolean isDatabaseInMongoDB( |
| 80 | + final MongoClient mongoSyncClient, |
| 81 | + final String databaseName |
| 82 | + ) { |
| 83 | + Objects.requireNonNull(databaseName); |
| 84 | + return StreamSupport.stream( |
| 85 | + Spliterators.spliteratorUnknownSize( |
| 86 | + mongoSyncClient.listDatabaseNames().iterator(), |
| 87 | + Spliterator.ORDERED |
| 88 | + ), |
| 89 | + false |
| 90 | + ).anyMatch(databaseName::equals); |
| 91 | + } |
| 92 | + |
| 93 | + static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 94 | + @Override |
| 95 | + public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) { |
| 96 | + TestPropertyValues.of( |
| 97 | + String.format("spring.data.mongodb.uri: %s", MONGO_DB_CONTAINER.getReplicaSetUrl(DATABASE_NAME)) |
| 98 | + ).applyTo(configurableApplicationContext); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @SpringBootApplication |
| 103 | + static class SpringBootApp { |
| 104 | + public static void main(String[] args) { |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Data |
| 109 | + @AllArgsConstructor |
| 110 | + @NoArgsConstructor |
| 111 | + @Setter(AccessLevel.NONE) |
| 112 | + private static class Product { |
| 113 | + private Long article; |
| 114 | + } |
| 115 | +} |
0 commit comments