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);
}
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;
}
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));
}
}
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;
}
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());
}
Aggregations