use of com.mongodb.RequestContext in project mongo-java-driver by mongodb.
the class ContextProviderTest method contextShouldBeAvailableInCommandEvents.
@Test
public void contextShouldBeAvailableInCommandEvents() {
RequestContext requestContext = mock(RequestContext.class);
TestCommandListener commandListener = new TestCommandListener(requestContext);
try (MongoClient client = MongoClients.create(getMongoClientSettingsBuilder().contextProvider(new ReactiveContextProvider() {
@Override
public RequestContext getContext(final Subscriber<?> subscriber) {
return requestContext;
}
}).addCommandListener(commandListener).build())) {
// given
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("ContextProviderTest");
Mono.from(collection.drop()).block();
Mono.from(collection.insertMany(asList(new Document(), new Document(), new Document(), new Document()))).block();
commandListener.reset();
// when
Mono.from(collection.countDocuments()).block();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
Document document = new Document();
// when
Mono.from(collection.insertOne(document)).block();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
Mono.from(collection.updateOne(document, inc("x", 1))).block();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
Document documentTwo = new Document();
// when
Mono.from(collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).insertOne(documentTwo)).block();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
Mono.from(collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).updateOne(documentTwo, inc("x", 1))).block();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
Mono.from(collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).deleteOne(documentTwo)).block();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
Flux<Document> findFlux = Flux.from(collection.find().batchSize(4));
findFlux.blockLast();
// then
assertEquals(2, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(2, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
try {
Mono.from(client.getDatabase("admin").runCommand(new Document("notRealCommand", 1))).block();
fail();
} catch (Exception e) {
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandFailedEventsWithExpectedContext);
}
}
}
use of com.mongodb.RequestContext in project mongo-java-driver by mongodb.
the class CryptConnection method commandAsync.
@Override
public <T> void commandAsync(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator, final SingleResultCallback<T> callback) {
if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
callback.onResult(null, new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2"));
return;
}
try {
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
getEncoder(command).encode(writer, command, EncoderContext.builder().build());
crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize())).flatMap((Function<RawBsonDocument, Mono<RawBsonDocument>>) encryptedCommand -> Mono.create(sink -> wrapped.commandAsync(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null, sinkToCallback(sink)))).flatMap(crypt::decrypt).map(decryptedResponse -> commandResultDecoder.decode(new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO()), DecoderContext.builder().build())).subscribe(decryptedResult -> callback.onResult(decryptedResult, null), e -> callback.onResult(null, e));
} catch (Throwable t) {
callback.onResult(null, t);
}
}
use of com.mongodb.RequestContext in project mongo-java-driver by mongodb.
the class ContextProviderTest method contextShouldBeAvailableInCommandEvents.
@Test
public void contextShouldBeAvailableInCommandEvents() {
RequestContext requestContext = mock(RequestContext.class);
TestCommandListener commandListener = new TestCommandListener(requestContext);
try (MongoClient client = MongoClients.create(getMongoClientSettingsBuilder().contextProvider(new SynchronousContextProvider() {
@Override
public RequestContext getContext() {
return requestContext;
}
}).addCommandListener(commandListener).build())) {
// given
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("ContextProviderTest");
collection.drop();
collection.insertMany(asList(new Document(), new Document(), new Document(), new Document()));
commandListener.reset();
// when
collection.countDocuments();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
Document document = new Document();
// when
collection.insertOne(document);
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
collection.updateOne(document, inc("x", 1));
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
Document documentTwo = new Document();
// when
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).insertOne(documentTwo);
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).updateOne(documentTwo, inc("x", 1));
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).deleteOne(documentTwo);
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
MongoCursor<Document> cursor = collection.find().batchSize(2).cursor();
cursor.next();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
cursor.next();
cursor.next();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
cursor.close();
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandSucceededEventsWithExpectedContext);
// given
commandListener.reset();
// when
try {
client.getDatabase("admin").runCommand(new Document("notRealCommand", 1));
fail();
} catch (Exception e) {
// then
assertEquals(1, commandListener.numCommandStartedEventsWithExpectedContext);
assertEquals(1, commandListener.numCommandFailedEventsWithExpectedContext);
}
}
}
Aggregations