Search in sources :

Example 56 with BsonString

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"));
}
Also used : BsonBinary(org.bson.BsonBinary) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) BsonString(org.bson.BsonString) Document(org.bson.Document) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) BsonString(org.bson.BsonString) InsertOneResult(com.mongodb.client.result.InsertOneResult) Test(org.junit.Test)

Example 57 with BsonString

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());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.Test) ClusterFixture.isClientSideEncryptionTest(com.mongodb.ClusterFixture.isClientSideEncryptionTest)

Example 58 with BsonString

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();
    });
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 59 with BsonString

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);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent) Test(org.junit.Test)

Example 60 with BsonString

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);
}
Also used : BsonString(org.bson.BsonString) Test(org.junit.jupiter.api.Test)

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