Search in sources :

Example 16 with MongoClient

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

the class MongoNamespaceReplicaSetTests method testMongoWithReplicaSets.

@Test
@Ignore("CI infrastructure does not yet support replica sets")
public void testMongoWithReplicaSets() {
    MongoClient mongo = ctx.getBean(MongoClient.class);
    assertThat(mongo.getClusterDescription().getClusterSettings().getHosts()).isEqualTo(2);
    List<ServerAddress> servers = mongo.getClusterDescription().getClusterSettings().getHosts();
    assertThat(servers.get(0).getHost()).isEqualTo("127.0.0.1");
    assertThat(servers.get(1).getHost()).isEqualTo("localhost");
    assertThat(servers.get(0).getPort()).isEqualTo(10001);
    assertThat(servers.get(1).getPort()).isEqualTo(10002);
    MongoTemplate template = new MongoTemplate(mongo, "admin");
    Document result = template.executeCommand("{replSetGetStatus : 1}");
    assertThat(result.get("set").toString()).isEqualTo("blort");
}
Also used : MongoClient(com.mongodb.client.MongoClient) ServerAddress(com.mongodb.ServerAddress) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Document(org.bson.Document) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with MongoClient

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

the class MongoNamespaceTests method testSecondMongoDbFactory.

@Test
public void testSecondMongoDbFactory() {
    assertThat(ctx.containsBean("secondMongoDbFactory")).isTrue();
    MongoDatabaseFactory dbf = (MongoDatabaseFactory) ctx.getBean("secondMongoDbFactory");
    MongoClient mongo = (MongoClient) getField(dbf, "mongoClient");
    assertThat(mongo.getClusterDescription().getClusterSettings().getHosts()).containsExactly(new ServerAddress());
    assertThat(getField(dbf, "databaseName")).isEqualTo("database");
}
Also used : MongoClient(com.mongodb.client.MongoClient) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 18 with MongoClient

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

the class MongoVersionRule method fetchCurrentVersion.

private Version fetchCurrentVersion() {
    try {
        MongoClient client = MongoTestUtils.client(host, port);
        MongoDatabase database = client.getDatabase("test");
        Document result = database.runCommand(new Document("buildInfo", 1));
        return Version.parse(result.get("version", String.class));
    } catch (Exception e) {
        return ANY;
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) Document(org.bson.Document) AssumptionViolatedException(org.junit.AssumptionViolatedException) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 19 with MongoClient

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

the class ReplicaSet method runsAsReplicaSet.

public boolean runsAsReplicaSet() {
    if (runsAsReplicaSet.get() == null) {
        MongoClient client = MongoTestUtils.client();
        boolean tmp = client.getDatabase("admin").runCommand(new Document("getCmdLineOpts", "1")).get("argv", List.class).contains("--replSet");
        runsAsReplicaSet.compareAndSet(null, tmp);
    }
    return runsAsReplicaSet.get();
}
Also used : MongoClient(com.mongodb.client.MongoClient) List(java.util.List) Document(org.bson.Document)

Example 20 with MongoClient

use of com.mongodb.client.MongoClient in project brave by openzipkin.

the class ITMongoDB method initCollection.

@BeforeClass
public static void initCollection() {
    try (MongoClient mongoClient = MongoClients.create(mongoClientSettingsBuilder().build())) {
        MongoDatabase database = mongoClient.getDatabase(DATABASE_NAME);
        MongoCollection<Document> collection = database.getCollection(COLLECTION_NAME);
        Document document1 = new Document("id", 1);
        Document document2 = new Document("id", 2);
        collection.insertMany(Arrays.asList(document1, document2));
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) BeforeClass(org.junit.BeforeClass)

Aggregations

MongoClient (com.mongodb.client.MongoClient)45 Document (org.bson.Document)22 MongoDatabase (com.mongodb.client.MongoDatabase)19 Test (org.junit.Test)10 Map (java.util.Map)9 MongoClientSettings (com.mongodb.MongoClientSettings)8 ServerAddress (com.mongodb.ServerAddress)8 HashMap (java.util.HashMap)7 ConnectionString (com.mongodb.ConnectionString)6 BsonDocument (org.bson.BsonDocument)6 BsonString (org.bson.BsonString)6 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 SecureRandom (java.security.SecureRandom)4 Set (java.util.Set)4 TimeUnit (java.util.concurrent.TimeUnit)4 BsonValue (org.bson.BsonValue)4 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)3 ClientSessionOptions (com.mongodb.ClientSessionOptions)3 ClusterFixture.getMultiMongosConnectionString (com.mongodb.ClusterFixture.getMultiMongosConnectionString)3