Search in sources :

Example 31 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 32 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 33 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)

Example 34 with MongoClient

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

the class MongoClientFactoryTests method uriIsIgnoredInEmbeddedMode.

@Test
public void uriIsIgnoredInEmbeddedMode() {
    MongoProperties properties = new MongoProperties();
    properties.setUri("mongodb://mongo.example.com:1234/mydb");
    this.environment.setProperty("local.mongo.port", "4000");
    MongoClient client = createMongoClient(properties, this.environment);
    List<ServerAddress> allAddresses = extractServerAddresses(client);
    assertThat(allAddresses).hasSize(1);
    assertServerAddress(allAddresses.get(0), "localhost", 4000);
}
Also used : MongoClient(com.mongodb.MongoClient) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 35 with MongoClient

use of com.mongodb.MongoClient in project java-design-patterns by iluwatar.

the class MongoBank method connect.

/**
 * Connect to database with given parameters
 */
public void connect(String dbName, String accountsCollectionName) {
    if (mongoClient != null) {
        mongoClient.close();
    }
    mongoClient = new MongoClient(System.getProperty("mongo-host"), Integer.parseInt(System.getProperty("mongo-port")));
    database = mongoClient.getDatabase(dbName);
    accountsCollection = database.getCollection(accountsCollectionName);
}
Also used : MongoClient(com.mongodb.MongoClient)

Aggregations

MongoClient (com.mongodb.MongoClient)126 Test (org.junit.Test)31 MongoClientURI (com.mongodb.MongoClientURI)29 Document (org.bson.Document)26 ServerAddress (com.mongodb.ServerAddress)21 MongoDatabase (com.mongodb.client.MongoDatabase)21 Before (org.junit.Before)20 BasicDBObject (com.mongodb.BasicDBObject)11 ArrayList (java.util.ArrayList)11 MongoCredential (com.mongodb.MongoCredential)9 MongoException (com.mongodb.MongoException)8 DB (com.mongodb.DB)7 DBCollection (com.mongodb.DBCollection)7 UnknownHostException (java.net.UnknownHostException)7 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)7 DBObject (com.mongodb.DBObject)6 MongoClientOptions (com.mongodb.MongoClientOptions)6 IOException (java.io.IOException)5 List (java.util.List)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5