use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class ClientSideEncryptionBypassAutoEncryptionTest method shouldAutoDecryptManuallyEncryptedData.
@Test
public void shouldAutoDecryptManuallyEncryptedData() {
String fieldValue = "123456789";
ObservableSubscriber<BsonBinary> binarySubscriber = new OperationSubscriber<>();
clientEncryption.createDataKey("local", new DataKeyOptions()).subscribe(binarySubscriber);
BsonBinary dataKeyId = binarySubscriber.get().get(0);
binarySubscriber = new OperationSubscriber<>();
clientEncryption.encrypt(new BsonString(fieldValue), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(dataKeyId)).subscribe(binarySubscriber);
BsonBinary encryptedFieldValue = binarySubscriber.get().get(0);
MongoCollection<Document> collection = clientEncrypted.getDatabase(Fixture.getDefaultDatabaseName()).getCollection("test");
ObservableSubscriber<InsertOneResult> insertSubscriber = new OperationSubscriber<>();
collection.insertOne(new Document("encryptedField", encryptedFieldValue)).subscribe(insertSubscriber);
insertSubscriber.await();
ObservableSubscriber<Document> resultSubscriber = new OperationSubscriber<>();
collection.find().first().subscribe(resultSubscriber);
assertEquals(fieldValue, resultSubscriber.get().get(0).getString("encryptedField"));
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class ClientSideEncryptionSessionTest method testWithExplicitSession.
@Test
public void testWithExplicitSession() throws Throwable {
BsonString unencryptedValue = new BsonString("test");
try (ClientSession clientSession = Mono.from(clientEncrypted.startSession()).block(TIMEOUT_DURATION)) {
assertNotNull(clientSession);
if (useTransaction) {
clientSession.startTransaction();
}
MongoCollection<BsonDocument> autoEncryptedCollection = clientEncrypted.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME, BsonDocument.class);
Mono.from(autoEncryptedCollection.insertOne(clientSession, new BsonDocument().append("encrypted", new BsonString("test")))).block(TIMEOUT_DURATION);
BsonDocument unencryptedDocument = Mono.from(autoEncryptedCollection.find(clientSession).first()).block(TIMEOUT_DURATION);
assertEquals(unencryptedValue, unencryptedDocument.getString("encrypted"));
if (useTransaction) {
Mono.from(clientSession.commitTransaction()).block(TIMEOUT_DURATION);
}
}
MongoCollection<BsonDocument> encryptedCollection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME, BsonDocument.class);
BsonDocument encryptedDocument = Mono.from(encryptedCollection.find().first()).block(TIMEOUT_DURATION);
assertTrue(encryptedDocument.isBinary("encrypted"));
assertEquals(6, encryptedDocument.getBinary("encrypted").getType());
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class GridFSPublisherCreator method createRenamePublisher.
public static Publisher<Void> createRenamePublisher(final MongoCollection<GridFSFile> filesCollection, @Nullable final ClientSession clientSession, final BsonValue id, final String newFilename) {
notNull("filesCollection", filesCollection);
notNull("id", id);
notNull("newFilename", newFilename);
BsonDocument filter = new BsonDocument("_id", id);
BsonDocument update = new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename)));
Publisher<UpdateResult> publisher;
if (clientSession == null) {
publisher = filesCollection.updateOne(filter, update);
} else {
publisher = filesCollection.updateOne(clientSession, filter, update);
}
return Mono.from(publisher).flatMap(updateResult -> {
if (updateResult.wasAcknowledged() && updateResult.getModifiedCount() == 0) {
throw new MongoGridFSException(format("No file found with the ObjectId: %s", id));
}
return Mono.empty();
});
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class ReadConcernTest method shouldIncludeReadConcernInCommand.
@Test
public void shouldIncludeReadConcernInCommand() throws InterruptedException {
Mono.from(mongoClient.getDatabase(getDefaultDatabaseName()).getCollection("test").withReadConcern(ReadConcern.LOCAL).find()).block(TIMEOUT_DURATION);
List<CommandEvent> events = commandListener.getCommandStartedEvents();
BsonDocument commandDocument = new BsonDocument("find", new BsonString("test")).append("readConcern", ReadConcern.LOCAL.asDocument()).append("filter", new BsonDocument());
assertEventsEquality(singletonList(new CommandStartedEvent(1, null, getDefaultDatabaseName(), "find", commandDocument)), events);
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class EnumCodecTest method shouldDecodeEnum.
@Test
public void shouldDecodeEnum() {
Codec<SimpleEnum> codec = new EnumCodec<>(SimpleEnum.class);
SimpleEnum decodedValue = getDecodedValue(new BsonString(SimpleEnum.BRAVO.name()), codec);
assertEquals(SimpleEnum.BRAVO, decodedValue);
}
Aggregations