Search in sources :

Example 51 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDBObjectWithUuidRepresentation.

@Test
public void shouldDecodeDBObjectWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    DBObject document;
    try {
        document = dbObjectCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidCodec.getUuidRepresentation() != STANDARD) {
            throw e;
        }
        return;
    }
    if (uuidRepresentationForClient == UuidRepresentation.STANDARD) {
        assertEquals(UUID.class, document.get("standard").getClass());
        assertEquals(uuid, document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, encodedValue), document.get("legacy"));
    } else {
        if (uuidRepresentationForClient == UuidRepresentation.JAVA_LEGACY) {
            assertEquals(UUID.class, document.get("standard").getClass());
            assertEquals(uuid, document.get("standard"));
        } else {
            assertEquals(Binary.class, document.get("standard").getClass());
            assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        }
        assertEquals(UUID.class, document.get("legacy").getClass());
        assertEquals(uuid, document.get("legacy"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 52 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldEncodeDbObjectWithUuidRepresentation.

@Test
public void shouldEncodeDbObjectWithUuidRepresentation() {
    dbObjectCollection.insertOne(new BasicDBObject("_id", uuid));
    BsonDocument document = bsonDocumentCollection.find().first();
    assertNotNull(document);
    BsonBinary uuidAsBinary = document.getBinary("_id");
    assertEquals(subType.getValue(), uuidAsBinary.getType());
    assertEquals(subType.getValue(), uuidAsBinary.getType());
    assertArrayEquals(encodedValue, uuidAsBinary.getData());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 53 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodePojoWithLegacyUuidRepresentation.

@Test
public void shouldDecodePojoWithLegacyUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    try {
        UuidIdPojo document = uuidIdPojoCollection.find().first();
        assertNotNull(document);
        assertEquals(uuid, document.getId());
    } catch (BSONException e) {
        if (uuidCodec.getUuidRepresentation() == uuidRepresentationForExplicitEncoding) {
            throw e;
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Test(org.junit.Test)

Example 54 with BsonBinary

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

the class AbstractRetryableReadsTest method parseHexDocument.

private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = Hex.decode(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
Also used : BsonBinary(org.bson.BsonBinary)

Example 55 with BsonBinary

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

the class ClientSideEncryptionBypassAutoEncryptionTest method shouldAutoDecryptManuallyEncryptedData.

@Test
public void shouldAutoDecryptManuallyEncryptedData() {
    String fieldValue = "123456789";
    BsonBinary dataKeyId = clientEncryption.createDataKey("local", new DataKeyOptions());
    BsonBinary encryptedFieldValue = clientEncryption.encrypt(new BsonString(fieldValue), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(dataKeyId));
    MongoCollection<Document> collection = clientEncrypted.getDatabase(Fixture.getDefaultDatabaseName()).getCollection("test");
    collection.insertOne(new Document("encryptedField", encryptedFieldValue));
    assertEquals(fieldValue, collection.find().first().getString("encryptedField"));
}
Also used : EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) BsonBinary(org.bson.BsonBinary) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) Document(org.bson.Document) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) Test(org.junit.Test)

Aggregations

BsonBinary (org.bson.BsonBinary)78 Test (org.junit.Test)51 BsonDocument (org.bson.BsonDocument)32 BsonString (org.bson.BsonString)16 Document (org.bson.Document)16 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)13 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)11 BSONException (org.bson.BSONException)9 Map (java.util.Map)8 BasicDBObject (com.mongodb.BasicDBObject)6 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 MongoClientSettings (com.mongodb.MongoClientSettings)6 SecureRandom (java.security.SecureRandom)6 HashMap (java.util.HashMap)6 ClientEncryption (com.mongodb.client.vault.ClientEncryption)5 UUID (java.util.UUID)5 Binary (org.bson.types.Binary)5 MongoNamespace (com.mongodb.MongoNamespace)4 IndexOptions (com.mongodb.client.model.IndexOptions)4