Search in sources :

Example 21 with MongoClient

use of com.mongodb.MongoClient in project spring-cloud-connectors by spring-cloud.

the class MongoServiceConnectorCreatorTest method cloudMongoCreationWithMultipleHostsByUri.

@Test
public void cloudMongoCreationWithMultipleHostsByUri() throws Exception {
    String uri = String.format("%s://%s:%s@%s:%s/%s", MONGODB_SCHEME, TEST_USERNAME, TEST_PASSWORD, StringUtils.arrayToDelimitedString(TEST_HOSTS, ","), TEST_PORT, TEST_DB);
    MongoServiceInfo serviceInfo = new MongoServiceInfo("id", uri);
    MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
    assertNotNull(mongoDbFactory);
    MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
    assertNotNull(mongo);
    List<ServerAddress> addresses = extractServerAddresses(mongo);
    assertEquals(3, addresses.size());
    MongoCredential credentials = mongo.getCredentialsList().get(0);
    assertEquals(TEST_USERNAME, credentials.getUserName());
    assertNotNull(credentials.getPassword());
    // Don't do connector.getDatabase().getName() as that will try to initiate the connection
    assertEquals(TEST_DB, ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
    ServerAddress address1 = addresses.get(0);
    assertEquals(TEST_HOST, address1.getHost());
    assertEquals(TEST_PORT_DEFAULT, address1.getPort());
    ServerAddress address2 = addresses.get(1);
    assertEquals(TEST_HOST_1, address2.getHost());
    assertEquals(TEST_PORT_DEFAULT, address2.getPort());
    ServerAddress address3 = addresses.get(2);
    assertEquals(TEST_HOST_2, address3.getHost());
    assertEquals(TEST_PORT, address3.getPort());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 22 with MongoClient

use of com.mongodb.MongoClient in project spring-boot by spring-projects.

the class MongoClientFactory method createNetworkMongoClient.

private MongoClient createNetworkMongoClient(MongoClientOptions options) {
    if (hasCustomAddress() || hasCustomCredentials()) {
        if (this.properties.getUri() != null) {
            throw new IllegalStateException("Invalid mongo configuration, " + "either uri or host/port/credentials must be specified");
        }
        if (options == null) {
            options = MongoClientOptions.builder().build();
        }
        List<MongoCredential> credentials = new ArrayList<>();
        if (hasCustomCredentials()) {
            String database = this.properties.getAuthenticationDatabase() == null ? this.properties.getMongoClientDatabase() : this.properties.getAuthenticationDatabase();
            credentials.add(MongoCredential.createCredential(this.properties.getUsername(), database, this.properties.getPassword()));
        }
        String host = this.properties.getHost() == null ? "localhost" : this.properties.getHost();
        int port = this.properties.getPort() != null ? this.properties.getPort() : MongoProperties.DEFAULT_PORT;
        return new MongoClient(Collections.singletonList(new ServerAddress(host, port)), credentials, options);
    }
    // The options and credentials are in the URI
    return new MongoClient(new MongoClientURI(this.properties.determineUri(), builder(options)));
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) MongoClientURI(com.mongodb.MongoClientURI) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress)

Example 23 with MongoClient

use of com.mongodb.MongoClient in project spring-boot by spring-projects.

the class EmbeddedMongoAutoConfigurationTests method randomlyAllocatedPortIsAvailableWhenCreatingMongoClient.

@Test
public void randomlyAllocatedPortIsAvailableWhenCreatingMongoClient() {
    load(MongoClientConfiguration.class);
    MongoClient client = this.context.getBean(MongoClient.class);
    Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
    assertThat(client.getAddress().getPort()).isEqualTo(mongoPort);
}
Also used : MongoClient(com.mongodb.MongoClient) Test(org.junit.Test)

Example 24 with MongoClient

use of com.mongodb.MongoClient in project spring-boot by spring-projects.

the class EmbeddedMongoAutoConfigurationTests method useRandomPortByDefault.

@Test
public void useRandomPortByDefault() {
    load();
    assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
    MongoClient client = this.context.getBean(MongoClient.class);
    Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
    assertThat(client.getAddress().getPort()).isEqualTo(mongoPort);
}
Also used : MongoClient(com.mongodb.MongoClient) Test(org.junit.Test)

Example 25 with MongoClient

use of com.mongodb.MongoClient in project spring-boot by spring-projects.

the class MongoAutoConfigurationTests method optionsSslConfig.

@Test
public void optionsSslConfig() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.uri:mongodb://localhost/test");
    this.context.register(SslOptionsConfig.class, PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
    this.context.refresh();
    MongoClient mongo = this.context.getBean(MongoClient.class);
    MongoClientOptions options = mongo.getMongoClientOptions();
    assertThat(options.isSslEnabled()).isTrue();
    assertThat(options.getSocketFactory()).isSameAs(this.context.getBean("mySocketFactory"));
}
Also used : MongoClient(com.mongodb.MongoClient) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MongoClientOptions(com.mongodb.MongoClientOptions) Test(org.junit.Test)

Aggregations

MongoClient (com.mongodb.MongoClient)81 MongoClientURI (com.mongodb.MongoClientURI)27 Test (org.junit.Test)20 Document (org.bson.Document)15 ServerAddress (com.mongodb.ServerAddress)13 MongoDatabase (com.mongodb.client.MongoDatabase)13 Before (org.junit.Before)10 BasicDBObject (com.mongodb.BasicDBObject)9 MongoCredential (com.mongodb.MongoCredential)8 ArrayList (java.util.ArrayList)8 DBCollection (com.mongodb.DBCollection)7 DB (com.mongodb.DB)6 DBObject (com.mongodb.DBObject)5 IOException (java.io.IOException)5 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)4 File (java.io.File)4 Closer (com.google.common.io.Closer)3 BasicDBList (com.mongodb.BasicDBList)3 MongoClientOptions (com.mongodb.MongoClientOptions)3 MongoException (com.mongodb.MongoException)3