Search in sources :

Example 61 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoConfigurationSupport method mongoClientSettings.

/**
 * Return the {@link MongoClientSettings} used to create the actual {@literal MongoClient}. <br />
 * Override either this method, or use {@link #configureClientSettings(Builder)} to alter the setup.
 *
 * @return never {@literal null}.
 * @since 3.0
 */
protected MongoClientSettings mongoClientSettings() {
    MongoClientSettings.Builder builder = MongoClientSettings.builder();
    builder.uuidRepresentation(UuidRepresentation.JAVA_LEGACY);
    configureClientSettings(builder);
    return builder.build();
}
Also used : MongoClientSettings(com.mongodb.MongoClientSettings) Builder(com.mongodb.MongoClientSettings.Builder)

Example 62 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoClientParserIntegrationTests method createsMongoClientWithServerSelectionTimeoutCorrectly.

// DATAMONGO-1620
@Test
public void createsMongoClientWithServerSelectionTimeoutCorrectly() {
    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongoClient-bean.xml"));
    try (AbstractApplicationContext context = new GenericApplicationContext(factory)) {
        context.refresh();
        MongoClientSettings settings = extractClientSettingsFromBean(context, "mongo-client-with-server-selection-timeout");
        assertThat(settings.getClusterSettings().getServerSelectionTimeout(TimeUnit.MILLISECONDS)).isEqualTo(100);
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MongoClientSettings(com.mongodb.MongoClientSettings) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 63 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoClientParserIntegrationTests method createsMongoClientWithCredentialsCorrectly.

// DATAMONGO-1158
@Test
public void createsMongoClientWithCredentialsCorrectly() {
    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongoClient-bean.xml"));
    try (AbstractApplicationContext context = new GenericApplicationContext(factory)) {
        context.refresh();
        MongoClientSettings settings = extractClientSettingsFromBean(context, "mongo-client-with-credentials");
        assertThat(settings.getCredential()).isEqualTo(MongoCredential.createPlainCredential("jon", "snow", "warg".toCharArray()));
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MongoClientSettings(com.mongodb.MongoClientSettings) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 64 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoClientParserIntegrationTests method createsMongoClientWithOptionsCorrectly.

// DATAMONGO-1158, DATAMONGO-2199
@Test
public void createsMongoClientWithOptionsCorrectly() {
    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongoClient-bean.xml"));
    try (AbstractApplicationContext context = new GenericApplicationContext(factory)) {
        context.refresh();
        MongoClientSettings settings = extractClientSettingsFromBean(context, "mongo-client-with-options-for-write-concern-and-read-preference");
        assertThat(settings.getReadPreference()).isEqualTo(ReadPreference.secondary());
        assertThat(settings.getWriteConcern()).isEqualTo(WriteConcern.UNACKNOWLEDGED);
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MongoClientSettings(com.mongodb.MongoClientSettings) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 65 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoNamespaceReplicaSetTests method testParsingMongoWithReplicaSets.

@Test
@SuppressWarnings("unchecked")
public void testParsingMongoWithReplicaSets() throws Exception {
    assertThat(ctx.containsBean("replicaSetMongo")).isTrue();
    MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&replicaSetMongo");
    MongoClientSettings settings = (MongoClientSettings) ReflectionTestUtils.getField(mfb, "mongoClientSettings");
    List<ServerAddress> replicaSetSeeds = settings.getClusterSettings().getHosts();
    assertThat(replicaSetSeeds).isNotNull();
    assertThat(replicaSetSeeds).contains(new ServerAddress(InetAddress.getByName("127.0.0.1"), 10001), new ServerAddress(InetAddress.getByName("localhost"), 10002));
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.Test)

Aggregations

MongoClientSettings (com.mongodb.MongoClientSettings)75 Test (org.junit.jupiter.api.Test)24 BsonString (org.bson.BsonString)21 Map (java.util.Map)20 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 Document (org.bson.Document)19 BsonDocument (org.bson.BsonDocument)18 ServerAddress (com.mongodb.ServerAddress)17 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)14 Before (org.junit.Before)14 MongoNamespace (com.mongodb.MongoNamespace)13 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)12 ConnectionString (com.mongodb.ConnectionString)12 MongoClientFactoryBean (org.springframework.data.mongodb.core.MongoClientFactoryBean)10 Block (com.mongodb.Block)8 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)8 SecureRandom (java.security.SecureRandom)8 TimeUnit (java.util.concurrent.TimeUnit)8 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)7