Search in sources :

Example 71 with BsonString

use of org.bson.BsonString in project mongo-java-driver by mongodb.

the class AbstractClientSideEncryptionDeadlockTest method shouldPassAllOutcomes.

@ParameterizedTest
@MethodSource("testArgumentProvider")
public void shouldPassAllOutcomes(final int maxPoolSize, final int expectedNumberOfClientsCreated, final boolean bypassAutoEncryption, final boolean externalKeyVaultClient, final List<ExpectedEvent> expectedEncryptingClientEvents, final List<ExpectedEvent> expectedExternalKeyVaultsClientEvents) {
    AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).bypassAutoEncryption(bypassAutoEncryption);
    TestCommandListener externalKeyVaultClientCommandListener = new TestCommandListener(singletonList("commandStartedEvent"), emptyList());
    if (externalKeyVaultClient) {
        autoEncryptionSettingsBuilder.keyVaultMongoClientSettings(getKeyVaultClientSettings(externalKeyVaultClientCommandListener));
    }
    TestCommandListener encryptingClientCommandListener = new TestCommandListener(singletonList("commandStartedEvent"), emptyList());
    encryptingClient = createMongoClient(getClientSettings(maxPoolSize, encryptingClientCommandListener, autoEncryptionSettingsBuilder.build()));
    BsonDocument unencryptedDocument = new BsonDocument("_id", new BsonInt32(0)).append("encrypted", new BsonString("string0"));
    if (bypassAutoEncryption) {
        getMongoClient().getDatabase("db").getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(new BsonDocument("_id", new BsonInt32(0)).append("encrypted", cipherText));
    } else {
        encryptingClient.getDatabase("db").getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(unencryptedDocument);
    }
    BsonDocument result = encryptingClient.getDatabase("db").getCollection("coll", BsonDocument.class).find().filter(Filters.eq("_id", 0)).first();
    assertEquals(unencryptedDocument, result);
    assertEquals(expectedNumberOfClientsCreated, getNumUniqueClients(encryptingClientCommandListener), "Unique clients");
    assertEventEquality(encryptingClientCommandListener, expectedEncryptingClientEvents);
    assertEventEquality(externalKeyVaultClientCommandListener, expectedExternalKeyVaultsClientEvents);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 72 with BsonString

use of org.bson.BsonString in project mongo-java-driver by mongodb.

the class QueryProtocol method asFindCommandDocument.

private BsonDocument asFindCommandDocument(final ByteBufferBsonOutput bsonOutput, final int firstDocumentPosition) {
    BsonDocument command = new BsonDocument(FIND_COMMAND_NAME, new BsonString(namespace.getCollectionName()));
    boolean isExplain = false;
    List<ByteBufBsonDocument> documents = ByteBufBsonDocument.createList(bsonOutput, firstDocumentPosition);
    ByteBufBsonDocument rawQueryDocument = documents.get(0);
    for (Map.Entry<String, BsonValue> cur : rawQueryDocument.entrySet()) {
        String commandFieldName = META_OPERATOR_TO_COMMAND_FIELD_MAP.get(cur.getKey());
        if (commandFieldName != null) {
            command.append(commandFieldName, cur.getValue());
        } else if (cur.getKey().equals("$explain")) {
            isExplain = true;
        }
    }
    if (command.size() == 1) {
        command.append("filter", rawQueryDocument);
    }
    if (documents.size() == 2) {
        command.append("projection", documents.get(1));
    }
    if (skip != 0) {
        command.append("skip", new BsonInt32(skip));
    }
    if (withLimitAndBatchSize) {
        if (limit != 0) {
            command.append("limit", new BsonInt32(limit));
        }
        if (batchSize != 0) {
            command.append("batchSize", new BsonInt32(batchSize));
        }
    }
    if (tailableCursor) {
        command.append("tailable", BsonBoolean.valueOf(tailableCursor));
    }
    if (noCursorTimeout) {
        command.append("noCursorTimeout", BsonBoolean.valueOf(noCursorTimeout));
    }
    if (oplogReplay) {
        command.append("oplogReplay", BsonBoolean.valueOf(oplogReplay));
    }
    if (awaitData) {
        command.append("awaitData", BsonBoolean.valueOf(awaitData));
    }
    if (partial) {
        command.append("allowPartialResults", BsonBoolean.valueOf(partial));
    }
    if (isExplain) {
        command = new BsonDocument(EXPLAIN_COMMAND_NAME, command);
    }
    return command;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 73 with BsonString

use of org.bson.BsonString in project mongo-java-driver by mongodb.

the class QueryProtocol method asFindCommandResponseDocument.

private BsonDocument asFindCommandResponseDocument(final ResponseBuffers responseBuffers, final QueryResult<T> queryResult, final boolean isExplain) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() > 0) {
        responseBuffers.reset();
        rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers);
    }
    if (isExplain) {
        BsonDocument explainCommandResponseDocument = new BsonDocument("ok", new BsonDouble(1));
        explainCommandResponseDocument.putAll(rawResultDocuments.get(0));
        return explainCommandResponseDocument;
    } else {
        BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("firstBatch", new BsonArray(rawResultDocuments));
        return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
    }
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Example 74 with BsonString

use of org.bson.BsonString in project mongo-java-driver by mongodb.

the class WriteProtocol method getBaseCommandDocument.

protected BsonDocument getBaseCommandDocument(final String commandName) {
    BsonDocument baseCommandDocument = new BsonDocument(commandName, new BsonString(getNamespace().getCollectionName())).append("ordered", BsonBoolean.valueOf(isOrdered()));
    baseCommandDocument.append("writeConcern", WriteConcern.UNACKNOWLEDGED.asDocument());
    return baseCommandDocument;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 75 with BsonString

use of org.bson.BsonString in project mongo-java-driver by mongodb.

the class FindPublisherImplTest method shouldBuildTheExpectedOperation.

@DisplayName("Should build the expected FindOperation")
@Test
void shouldBuildTheExpectedOperation() {
    configureBatchCursor();
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor()));
    FindPublisher<Document> publisher = new FindPublisherImpl<>(null, createMongoOperationPublisher(executor), new Document());
    FindOperation<Document> expectedOperation = new FindOperation<>(NAMESPACE, getDefaultCodecRegistry().get(Document.class)).batchSize(Integer.MAX_VALUE).retryReads(true).filter(new BsonDocument());
    // default input should be as expected
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    // Should apply settings
    publisher.filter(new Document("filter", 1)).sort(Sorts.ascending("sort")).projection(new Document("projection", 1)).maxTime(10, SECONDS).maxAwaitTime(20, SECONDS).batchSize(100).limit(100).skip(10).cursorType(CursorType.NonTailable).oplogReplay(false).noCursorTimeout(false).partial(false).collation(COLLATION).comment("my comment").hintString("a_1").min(new Document("min", 1)).max(new Document("max", 1)).returnKey(false).showRecordId(false).allowDiskUse(false);
    expectedOperation.allowDiskUse(false).batchSize(100).collation(COLLATION).comment("my comment").cursorType(CursorType.NonTailable).filter(new BsonDocument("filter", new BsonInt32(1))).hint(new BsonString("a_1")).limit(100).max(new BsonDocument("max", new BsonInt32(1))).maxAwaitTime(20000, MILLISECONDS).maxTime(10000, MILLISECONDS).min(new BsonDocument("min", new BsonInt32(1))).projection(new BsonDocument("projection", new BsonInt32(1))).returnKey(false).showRecordId(false).skip(10).secondaryOk(false).sort(new BsonDocument("sort", new BsonInt32(1)));
    configureBatchCursor();
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

BsonString (org.bson.BsonString)178 BsonDocument (org.bson.BsonDocument)153 BsonInt32 (org.bson.BsonInt32)55 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)36 Document (org.bson.Document)32 BsonValue (org.bson.BsonValue)31 BsonInt64 (org.bson.BsonInt64)28 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)14 BsonDouble (org.bson.BsonDouble)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 List (java.util.List)10 Before (org.junit.Before)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9