Search in sources :

Example 36 with MongoClient

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

the class MongoTestUtils method serverVersion.

/**
 * @return the server version extracted from buildInfo.
 * @since 3.0
 */
public static Version serverVersion() {
    try (MongoClient client = client()) {
        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) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 37 with MongoClient

use of com.mongodb.client.MongoClient in project rocketmq-externals by apache.

the class ReplicaContextTest method testCreateMongoClient.

@Test
public void testCreateMongoClient() {
    MongoClient mongoClient = context.createMongoClient(new ReplicaSetConfig("shardName1", "", "127.0.0.1:27027"));
    MongoIterable<String> collectionNames = mongoClient.getDatabase("local").listCollectionNames();
    MongoCursor<String> iterator = collectionNames.iterator();
    while (iterator.hasNext()) {
        Assert.assertTrue(StringUtils.isNoneBlank(iterator.next()));
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) Test(org.junit.Test)

Example 38 with MongoClient

use of com.mongodb.client.MongoClient in project iaf by ibissource.

the class MongoDbSenderTest method setUp.

@Override
public void setUp() throws Exception {
    String url = "mongodb://" + user + ":" + password + "@" + host;
    MongoClient mongoClient = MongoClients.create(url);
    mongoClientFactory = new JndiMongoClientFactory();
    mongoClientFactory.add(mongoClient, JndiMongoClientFactory.GLOBAL_DEFAULT_DATASOURCE_NAME);
    super.setUp();
}
Also used : MongoClient(com.mongodb.client.MongoClient)

Example 39 with MongoClient

use of com.mongodb.client.MongoClient in project logging-log4j2 by apache.

the class MongoDb4AuthFailureTest method test.

@Test
public void test() {
    final Logger logger = LogManager.getLogger();
    logger.info("Hello log");
    try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) {
        final MongoDatabase database = mongoClient.getDatabase("testDb");
        Assert.assertNotNull(database);
        final MongoCollection<Document> collection = database.getCollection("testCollection");
        Assert.assertNotNull(collection);
        final Document first = collection.find().first();
        Assert.assertNull(first);
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) Logger(org.apache.logging.log4j.Logger) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 40 with MongoClient

use of com.mongodb.client.MongoClient in project logging-log4j2 by apache.

the class MongoDb4MapMessageTest method test.

@Test
public void test() {
    final Logger logger = LogManager.getLogger();
    final MapMessage<?, Object> mapMessage = new MapMessage<>();
    mapMessage.with("SomeName", "SomeValue");
    mapMessage.with("SomeInt", 1);
    logger.info(mapMessage);
    // 
    try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) {
        final MongoDatabase database = mongoClient.getDatabase("testDb");
        Assert.assertNotNull(database);
        final MongoCollection<Document> collection = database.getCollection("testCollection");
        Assert.assertNotNull(collection);
        final Document first = collection.find().first();
        Assert.assertNotNull(first);
        final String firstJson = first.toJson();
        Assert.assertEquals(firstJson, "SomeValue", first.getString("SomeName"));
        Assert.assertEquals(firstJson, Integer.valueOf(1), first.getInteger("SomeInt"));
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) MapMessage(org.apache.logging.log4j.message.MapMessage) Logger(org.apache.logging.log4j.Logger) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

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