Search in sources :

Example 36 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class AbstractCrudTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.create(collectionName, new CreateCollectionOptions(), WriteConcern.ACKNOWLEDGED);
    createMongoClient(commandListener);
    database = getDatabase(databaseName);
    collection = database.getCollection(collectionName, BsonDocument.class);
    helper = new JsonPoweredCrudTestHelper(description, database, collection);
    if (!data.isEmpty()) {
        List<BsonDocument> documents = new ArrayList<>();
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        if (documents.size() > 0) {
            collectionHelper.insertDocuments(documents, WriteConcern.MAJORITY);
        }
    }
    commandListener.reset();
}
Also used : BsonDocument(org.bson.BsonDocument) DocumentCodec(org.bson.codecs.DocumentCodec) ArrayList(java.util.ArrayList) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 37 with MongoNamespace

use of com.mongodb.MongoNamespace 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)

Example 38 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class ClientSideEncryptionBypassAutoEncryptionTest method setUp.

@Before
public void setUp() {
    assumeTrue(serverVersionAtLeast(4, 2));
    MongoClient mongoClient = getMongoClient();
    final byte[] localMasterKey = new byte[96];
    new SecureRandom().nextBytes(localMasterKey);
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {

        {
            put("local", new HashMap<String, Object>() {

                {
                    put("key", localMasterKey);
                }
            });
        }
    };
    // Set up the key vault for this example
    MongoNamespace keyVaultNamespace = new MongoNamespace("encryption.testKeyVault");
    MongoCollection<Document> keyVaultCollection = mongoClient.getDatabase(keyVaultNamespace.getDatabaseName()).getCollection(keyVaultNamespace.getCollectionName());
    keyVaultCollection.drop();
    // Ensure that two data keys cannot share the same keyAltName.
    keyVaultCollection.createIndex(Indexes.ascending("keyAltNames"), new IndexOptions().unique(true).partialFilterExpression(Filters.exists("keyAltNames")));
    MongoDatabase db = mongoClient.getDatabase(Fixture.getDefaultDatabaseName());
    db.getCollection("test").drop();
    // Create the ClientEncryption instance
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getMongoClientSettings()).keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
    clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).bypassAutoEncryption(true).build();
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : HashMap(java.util.HashMap) IndexOptions(com.mongodb.client.model.IndexOptions) SecureRandom(java.security.SecureRandom) BsonString(org.bson.BsonString) Fixture.getMongoClientSettings(com.mongodb.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) Fixture.getMongoClient(com.mongodb.client.Fixture.getMongoClient) ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 39 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class AbstractChangeStreamsTest method checkStreamValues.

private void checkStreamValues(final BsonDocument result, final MongoCursor<ChangeStreamDocument<BsonDocument>> cursor) {
    for (BsonValue success : result.getArray("success", new BsonArray())) {
        BsonDocument expected = success.asDocument();
        ChangeStreamDocument<BsonDocument> actual = cursor.next();
        MongoNamespace expectedNamespace = null;
        if (expected.containsKey("ns")) {
            BsonDocument nsDocument = expected.getDocument("ns");
            expectedNamespace = nsDocument != null ? new MongoNamespace(nsDocument.getString("db").getValue(), nsDocument.getString("coll").getValue()) : null;
        }
        assertEquals(expectedNamespace, actual.getNamespace());
        assertEquals(OperationType.fromString(expected.getString("operationType").getValue()), actual.getOperationType());
        if (actual.getFullDocument() != null) {
            actual.getFullDocument().remove("_id");
        }
        assertEquals(expected.get("fullDocument"), actual.getFullDocument());
    }
    if (result.containsKey("error")) {
        BsonDocument error = result.getDocument("error");
        try {
            cursor.next();
        } catch (MongoException e) {
            assertTrue(e.getCode() == error.getInt32("code").intValue() || !Collections.disjoint(e.getErrorLabels(), error.getArray("errorLabels", new BsonArray())));
        }
    }
}
Also used : MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue)

Example 40 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class AbstractChangeStreamsTest method handleOperations.

private void handleOperations() {
    for (BsonValue operations : definition.getArray("operations")) {
        BsonDocument op = operations.asDocument();
        MongoNamespace opNamespace = new MongoNamespace(op.getString("database").getValue(), op.getString("collection").getValue());
        createJsonPoweredCrudTestHelper(Fixture.getMongoClient(), opNamespace).getOperationResults(op);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)55 BsonDocument (org.bson.BsonDocument)34 BsonString (org.bson.BsonString)21 Document (org.bson.Document)20 Before (org.junit.Before)17 MongoClientSettings (com.mongodb.MongoClientSettings)15 BsonValue (org.bson.BsonValue)13 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 DocumentCodec (org.bson.codecs.DocumentCodec)8 CollectionHelper (com.mongodb.client.test.CollectionHelper)7 Test (org.junit.jupiter.api.Test)7 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 Test (org.junit.Test)6 IndexOptions (com.mongodb.client.model.IndexOptions)5 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)5 DisplayName (org.junit.jupiter.api.DisplayName)5