Search in sources :

Example 1 with CollectionHelper

use of com.mongodb.client.test.CollectionHelper in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.drop();
    if (!documents.isEmpty()) {
        collectionHelper.insertDocuments(documents);
    }
    commandListener.reset();
    MongoDatabase database = mongoClient.getDatabase(databaseName);
    collection = database.getCollection(collectionName, BsonDocument.class);
    if (definition.getDocument("operation").containsKey("read_preference")) {
        collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
    }
    helper = new JsonPoweredCrudTestHelper(description, database, collection);
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) DocumentCodec(org.bson.codecs.DocumentCodec) CollectionHelper(com.mongodb.client.test.CollectionHelper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 2 with CollectionHelper

use of com.mongodb.client.test.CollectionHelper in project mongo-java-driver by mongodb.

the class ConnectionsSurvivePrimaryStepDownProseTest method setUp.

@Before
public void setUp() {
    assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0));
    connectionPoolListener = new TestConnectionPoolListener();
    MongoClientSettings settings = MongoClientSettings.builder(getMongoClientSettings()).retryWrites(false).applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(connectionPoolListener)).build();
    collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    client = MongoClients.create(settings);
    MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
    collection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME);
    Mono.from(collection.withWriteConcern(WriteConcern.MAJORITY).drop()).block(TIMEOUT_DURATION);
    Mono.from(database.withWriteConcern(WriteConcern.MAJORITY).createCollection(COLLECTION_NAME)).block(TIMEOUT_DURATION);
}
Also used : Document(org.bson.Document) ClusterFixture.isDiscoverableReplicaSet(com.mongodb.ClusterFixture.isDiscoverableReplicaSet) Assume.assumeFalse(org.junit.Assume.assumeFalse) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClients(com.mongodb.reactivestreams.client.MongoClients) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) MongoClient(com.mongodb.reactivestreams.client.MongoClient) TIMEOUT_DURATION(com.mongodb.ClusterFixture.TIMEOUT_DURATION) Collections.singletonList(java.util.Collections.singletonList) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) DocumentCodec(org.bson.codecs.DocumentCodec) Assert.fail(org.junit.Assert.fail) MongoNotPrimaryException(com.mongodb.MongoNotPrimaryException) Before(org.junit.Before) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) MongoException(com.mongodb.MongoException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) ClusterFixture.getDefaultDatabaseName(com.mongodb.ClusterFixture.getDefaultDatabaseName) List(java.util.List) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) WriteConcern(com.mongodb.WriteConcern) Assert.assertEquals(org.junit.Assert.assertEquals) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) DocumentCodec(org.bson.codecs.DocumentCodec) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Before(org.junit.Before)

Example 3 with CollectionHelper

use of com.mongodb.client.test.CollectionHelper in project mongo-java-driver by mongodb.

the class ClientSideEncryptionSessionTest method setUp.

@Before
public void setUp() throws Throwable {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue(isClientSideEncryptionTest());
    assumeFalse(isStandalone());
    /* Step 1: get unencrypted client and recreate keys collection */
    client = getMongoClient();
    MongoDatabase keyVaultDatabase = client.getDatabase("keyvault");
    MongoCollection<BsonDocument> dataKeys = keyVaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    Mono.from(dataKeys.drop()).block(TIMEOUT_DURATION);
    Mono.from(dataKeys.insertOne(bsonDocumentFromPath("external-key.json"))).block(TIMEOUT_DURATION);
    /* Step 2: create encryption objects. */
    Map<String, Map<String, Object>> kmsProviders = new HashMap<>();
    Map<String, Object> localMasterkey = new HashMap<>();
    Map<String, BsonDocument> schemaMap = new HashMap<>();
    byte[] localMasterKeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterKeyBytes);
    kmsProviders.put("local", localMasterkey);
    schemaMap.put(getDefaultDatabaseName() + "." + COLLECTION_NAME, bsonDocumentFromPath("external-schema.json"));
    MongoClientSettings clientSettings = getMongoClientBuilderFromConnectionString().autoEncryptionSettings(AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).schemaMap(schemaMap).build()).build();
    clientEncrypted = MongoClients.create(clientSettings);
    CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<>(new BsonDocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    collectionHelper.drop();
    collectionHelper.create();
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) Fixture.getMongoClientBuilderFromConnectionString(com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) CollectionHelper(com.mongodb.client.test.CollectionHelper) HashMap(java.util.HashMap) Map(java.util.Map) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Before(org.junit.Before)

Example 4 with CollectionHelper

use of com.mongodb.client.test.CollectionHelper in project mongo-java-driver by mongodb.

the class AbstractUnifiedTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    assumeTrue("Skipping test: " + definition.getString("skipReason", new BsonString("")).getValue(), !definition.containsKey("skipReason"));
    assumeFalse("Skipping test of count", filename.equals("count.json"));
    collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.killAllSessions();
    if (!isDataLakeTest()) {
        collectionHelper.create(collectionName, new CreateCollectionOptions(), WriteConcern.MAJORITY);
    }
    if (!data.isEmpty()) {
        List<BsonDocument> documents = new ArrayList<BsonDocument>();
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        collectionHelper.insertDocuments(documents, WriteConcern.MAJORITY);
    }
    if (definition.containsKey("failPoint")) {
        collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
    }
    final BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
    connectionString = getConnectionString();
    useMultipleMongoses = definition.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue();
    if (useMultipleMongoses) {
        assumeTrue(isSharded());
        connectionString = getMultiMongosConnectionString();
        assumeTrue("The system property org.mongodb.test.transaction.uri is not set.", connectionString != null);
    }
    MongoClientSettings.Builder builder = getMongoClientSettingsBuilder().applyConnectionString(connectionString).addCommandListener(commandListener).applyToClusterSettings(clusterSettingsBuilder -> {
        if (clientOptions.containsKey("serverSelectionTimeoutMS")) {
            clusterSettingsBuilder.serverSelectionTimeout(clientOptions.getNumber("serverSelectionTimeoutMS").longValue(), MILLISECONDS);
        }
        if (clientOptions.containsKey("directConnection")) {
            setDirectConnection(clusterSettingsBuilder);
        }
    }).applyToSocketSettings(new Block<SocketSettings.Builder>() {

        @Override
        public void apply(final SocketSettings.Builder builder) {
            builder.readTimeout(clientOptions.getInt32("socketTimeoutMS", new BsonInt32(toIntExact(SECONDS.toMillis(5)))).getValue(), MILLISECONDS);
            if (clientOptions.containsKey("connectTimeoutMS")) {
                builder.connectTimeout(clientOptions.getNumber("connectTimeoutMS").intValue(), MILLISECONDS);
            }
        }
    }).writeConcern(getWriteConcern(clientOptions)).readConcern(getReadConcern(clientOptions)).readPreference(getReadPreference(clientOptions)).retryWrites(clientOptions.getBoolean("retryWrites", BsonBoolean.FALSE).getValue()).retryReads(false).applyToConnectionPoolSettings(poolSettingsBuilder -> {
        poolSettingsBuilder.addConnectionPoolListener(connectionPoolListener);
        if (clientOptions.containsKey("minPoolSize")) {
            poolSettingsBuilder.minSize(clientOptions.getInt32("minPoolSize").getValue());
        }
    }).applyToServerSettings(new Block<ServerSettings.Builder>() {

        @Override
        public void apply(final ServerSettings.Builder builder) {
            builder.heartbeatFrequency(50, MILLISECONDS);
            builder.minHeartbeatFrequency(MIN_HEARTBEAT_FREQUENCY_MS, MILLISECONDS);
            builder.addServerListener(serverListener);
        }
    });
    if (clientOptions.containsKey("heartbeatFrequencyMS")) {
        builder.applyToServerSettings(new Block<ServerSettings.Builder>() {

            @Override
            public void apply(final ServerSettings.Builder builder) {
                builder.heartbeatFrequency(clientOptions.getInt32("heartbeatFrequencyMS").intValue(), MILLISECONDS);
            }
        });
    }
    if (clientOptions.containsKey("appname")) {
        builder.applicationName(clientOptions.getString("appname").getValue());
    }
    if (clientOptions.containsKey("w")) {
        if (clientOptions.isString("w")) {
            builder.writeConcern(new WriteConcern(clientOptions.getString("w").getValue()));
        } else if (clientOptions.isNumber("w")) {
            builder.writeConcern(new WriteConcern(clientOptions.getNumber("w").intValue()));
        }
    }
    StreamFactoryFactory streamFactoryFactory = getStreamFactoryFactory();
    if (streamFactoryFactory != null) {
        builder.streamFactoryFactory(streamFactoryFactory);
    }
    mongoClient = createMongoClient(builder.build());
    database = mongoClient.getDatabase(databaseName);
    if (useMultipleMongoses) {
        // non-transactional distinct operation to avoid StaleDbVersion error
        runDistinctOnEachNode();
    }
    helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection(collectionName, BsonDocument.class), null, mongoClient);
    sessionsMap = new HashMap<>();
    lsidMap = new HashMap<>();
    if (createSessions && serverVersionAtLeast(3, 6)) {
        ClientSession sessionZero = createSession("session0");
        ClientSession sessionOne = createSession("session1");
        sessionsMap.put("session0", sessionZero);
        sessionsMap.put("session1", sessionOne);
        lsidMap.put("session0", sessionZero.getServerSession().getIdentifier());
        lsidMap.put("session1", sessionOne.getServerSession().getIdentifier());
    }
}
Also used : Document(org.bson.Document) ServerDescription(com.mongodb.connection.ServerDescription) ClusterFixture.getConnectionString(com.mongodb.ClusterFixture.getConnectionString) BsonValue(org.bson.BsonValue) Collections.singletonList(java.util.Collections.singletonList) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) StreamFactoryFactory(com.mongodb.connection.StreamFactoryFactory) Future(java.util.concurrent.Future) ClusterFixture.isSharded(com.mongodb.ClusterFixture.isSharded) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) SocketSettings(com.mongodb.connection.SocketSettings) BsonArray(org.bson.BsonArray) After(org.junit.After) Map(java.util.Map) ServerType(com.mongodb.connection.ServerType) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) Assert.fail(org.junit.Assert.fail) ReadConcern(com.mongodb.ReadConcern) Parameterized(org.junit.runners.Parameterized) CommandMonitoringTestHelper.assertEventsEquality(com.mongodb.client.CommandMonitoringTestHelper.assertEventsEquality) CommandEvent(com.mongodb.event.CommandEvent) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoException(com.mongodb.MongoException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Block(com.mongodb.Block) Assert.assertFalse(org.junit.Assert.assertFalse) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) ReadPreference(com.mongodb.ReadPreference) CommandMonitoringTestHelper.getExpectedEvents(com.mongodb.client.CommandMonitoringTestHelper.getExpectedEvents) BsonBoolean(org.bson.BsonBoolean) Assume.assumeFalse(org.junit.Assume.assumeFalse) MongoCommandException(com.mongodb.MongoCommandException) ClusterSettings(com.mongodb.connection.ClusterSettings) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) BsonString(org.bson.BsonString) ServerSettings(com.mongodb.connection.ServerSettings) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) TransactionOptions(com.mongodb.TransactionOptions) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) DocumentCodec(org.bson.codecs.DocumentCodec) Fixture.getMongoClient(com.mongodb.client.Fixture.getMongoClient) Math.toIntExact(java.lang.Math.toIntExact) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ServerAddress(com.mongodb.ServerAddress) ClusterFixture.setDirectConnection(com.mongodb.ClusterFixture.setDirectConnection) MongoNamespace(com.mongodb.MongoNamespace) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ClusterFixture.isDataLakeTest(com.mongodb.ClusterFixture.isDataLakeTest) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) ConnectionString(com.mongodb.ConnectionString) Assert.assertNull(org.junit.Assert.assertNull) ClientSessionOptions(com.mongodb.ClientSessionOptions) ReadConcernLevel(com.mongodb.ReadConcernLevel) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) Nullable(com.mongodb.lang.Nullable) BsonInt32(org.bson.BsonInt32) WriteConcern(com.mongodb.WriteConcern) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ArrayList(java.util.ArrayList) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) SocketSettings(com.mongodb.connection.SocketSettings) BsonInt32(org.bson.BsonInt32) ServerSettings(com.mongodb.connection.ServerSettings) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) WriteConcern(com.mongodb.WriteConcern) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Block(com.mongodb.Block) StreamFactoryFactory(com.mongodb.connection.StreamFactoryFactory) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 5 with CollectionHelper

use of com.mongodb.client.test.CollectionHelper in project mongo-java-driver by mongodb.

the class AbstractUnifiedTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    try {
        executeOperations(definition.getArray("operations"), false);
    } finally {
        closeAllSessions();
        shutdownAllExecutors();
    }
    if (definition.containsKey("expectations")) {
        List<CommandEvent> expectedEvents = getExpectedEvents(definition.getArray("expectations"), databaseName, null);
        List<CommandEvent> events = commandListener.getCommandStartedEvents();
        assertTrue("Actual number of events is less than expected number of events", events.size() >= expectedEvents.size());
        assertEventsEquality(expectedEvents, events.subList(0, expectedEvents.size()), lsidMap);
    }
    BsonDocument expectedOutcome = definition.getDocument("outcome", new BsonDocument());
    if (expectedOutcome.containsKey("collection")) {
        BsonDocument collectionDocument = expectedOutcome.getDocument("collection");
        List<BsonDocument> collectionData;
        if (collectionDocument.containsKey("name")) {
            collectionData = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionDocument.getString("name").getValue())).find(new BsonDocumentCodec());
        } else {
            collectionData = collectionHelper.find(new BsonDocumentCodec());
        }
        assertEquals(expectedOutcome.getDocument("collection").getArray("data").getValues(), collectionData);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) CommandEvent(com.mongodb.event.CommandEvent) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test) ClusterFixture.isDataLakeTest(com.mongodb.ClusterFixture.isDataLakeTest)

Aggregations

CollectionHelper (com.mongodb.client.test.CollectionHelper)8 MongoNamespace (com.mongodb.MongoNamespace)7 BsonDocument (org.bson.BsonDocument)7 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)6 Before (org.junit.Before)6 MongoClientSettings (com.mongodb.MongoClientSettings)4 DocumentCodec (org.bson.codecs.DocumentCodec)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BsonString (org.bson.BsonString)3 BsonValue (org.bson.BsonValue)3 Document (org.bson.Document)3 Test (org.junit.Test)3 ClusterFixture.isDataLakeTest (com.mongodb.ClusterFixture.isDataLakeTest)2 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)2 MongoException (com.mongodb.MongoException)2 WriteConcern (com.mongodb.WriteConcern)2 ConnectionPoolClearedEvent (com.mongodb.event.ConnectionPoolClearedEvent)2 TestConnectionPoolListener (com.mongodb.internal.connection.TestConnectionPoolListener)2 ArrayList (java.util.ArrayList)2