Search in sources :

Example 66 with MongoClientSettings

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

the class MongoNamespaceReplicaSetTests method testParsingWithPropertyPlaceHolder.

@Test
@SuppressWarnings("unchecked")
public void testParsingWithPropertyPlaceHolder() throws Exception {
    assertThat(ctx.containsBean("manyReplicaSetMongo")).isTrue();
    MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&manyReplicaSetMongo");
    MongoClientSettings settings = (MongoClientSettings) ReflectionTestUtils.getField(mfb, "mongoClientSettings");
    List<ServerAddress> replicaSetSeeds = settings.getClusterSettings().getHosts();
    assertThat(replicaSetSeeds).isNotNull();
    assertThat(replicaSetSeeds).hasSize(3);
    List<Integer> ports = new ArrayList<Integer>();
    for (ServerAddress replicaSetSeed : replicaSetSeeds) {
        ports.add(replicaSetSeed.getPort());
    }
    assertThat(ports).contains(27017, 27018, 27019);
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.Test)

Example 67 with MongoClientSettings

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

the class MongoClientFactoryBeanUnitTests method explicitSettingsOverrideConnectionStringOnes.

// DATAMONGO-2427
@Test
void explicitSettingsOverrideConnectionStringOnes() {
    MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
    factoryBean.setConnectionString(CONNECTION_STRING);
    factoryBean.setMongoClientSettings(MongoClientSettings.builder().applyToClusterSettings(it -> it.requiredReplicaSetName("rs0")).applyToSocketSettings(it -> it.connectTimeout(100, TimeUnit.MILLISECONDS)).build());
    MongoClientSettings settings = factoryBean.computeClientSetting();
    assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("rs0");
    assertThat(settings.getSocketSettings().getConnectTimeout(TimeUnit.MILLISECONDS)).isEqualTo(100);
    assertThat(settings.getClusterSettings().getHosts()).hasSize(2);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) Assertions(org.assertj.core.api.Assertions) MongoClientSettings(com.mongodb.MongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.jupiter.api.Test)

Example 68 with MongoClientSettings

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

the class MongoClientFactoryBeanUnitTests method connectionStringParametersNotOverriddenByDefaults.

// DATAMONGO-2427
@Test
void connectionStringParametersNotOverriddenByDefaults() {
    MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
    factoryBean.setConnectionString(CONNECTION_STRING);
    factoryBean.setMongoClientSettings(MongoClientSettings.builder().build());
    MongoClientSettings settings = factoryBean.computeClientSetting();
    assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("test");
    assertThat(settings.getSocketSettings().getConnectTimeout(TimeUnit.MILLISECONDS)).isEqualTo(300000);
    assertThat(settings.getClusterSettings().getHosts()).hasSize(2);
}
Also used : MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.jupiter.api.Test)

Example 69 with MongoClientSettings

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

the class MongoClientNamespaceTests method clientWithClusterSettings.

// DATAMONGO-2384
@Test
public void clientWithClusterSettings() {
    assertThat(ctx.containsBean("client-with-cluster-settings")).isTrue();
    MongoClientFactoryBean factoryBean = ctx.getBean("&client-with-cluster-settings", MongoClientFactoryBean.class);
    MongoClientSettings settings = (MongoClientSettings) getField(factoryBean, "mongoClientSettings");
    assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.REPLICA_SET);
    assertThat(settings.getClusterSettings().getServerSelectionTimeout(TimeUnit.MILLISECONDS)).isEqualTo(10);
    assertThat(settings.getClusterSettings().getLocalThreshold(TimeUnit.MILLISECONDS)).isEqualTo(5);
    assertThat(settings.getClusterSettings().getHosts()).contains(new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019), new ServerAddress("localhost", 27020));
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.Test)

Example 70 with MongoClientSettings

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

the class MongoClientNamespaceTests method clientWithConnectionPoolSettings.

// DATAMONGO-2384
@Test
public void clientWithConnectionPoolSettings() {
    assertThat(ctx.containsBean("client-with-connection-pool-settings")).isTrue();
    MongoClientFactoryBean factoryBean = ctx.getBean("&client-with-connection-pool-settings", MongoClientFactoryBean.class);
    MongoClientSettings settings = (MongoClientSettings) getField(factoryBean, "mongoClientSettings");
    assertThat(settings.getConnectionPoolSettings().getMaxConnectionLifeTime(TimeUnit.MILLISECONDS)).isEqualTo(10);
    assertThat(settings.getConnectionPoolSettings().getMinSize()).isEqualTo(10);
    assertThat(settings.getConnectionPoolSettings().getMaxSize()).isEqualTo(20);
    assertThat(settings.getConnectionPoolSettings().getMaintenanceFrequency(TimeUnit.MILLISECONDS)).isEqualTo(10);
    assertThat(settings.getConnectionPoolSettings().getMaintenanceInitialDelay(TimeUnit.MILLISECONDS)).isEqualTo(11);
    assertThat(settings.getConnectionPoolSettings().getMaxConnectionIdleTime(TimeUnit.MILLISECONDS)).isEqualTo(30);
    assertThat(settings.getConnectionPoolSettings().getMaxWaitTime(TimeUnit.MILLISECONDS)).isEqualTo(15);
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) 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