Search in sources :

Example 56 with BsonBinary

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

the class AbstractUuidRepresentationTest method shouldEncodeDbObjectWithUuidRepresentation.

@Test
public void shouldEncodeDbObjectWithUuidRepresentation() {
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        try {
            dbObjectCollection.insertOne(new BasicDBObject("_id", uuid));
            fail();
        } catch (CodecConfigurationException e) {
        // all good
        }
    } else {
        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) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Test(org.junit.Test)

Example 57 with BsonBinary

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

the class AbstractUuidRepresentationTest method shouldDecodeDocumentWithUuidRepresentation.

@Test
public void shouldDecodeDocumentWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
    Document document;
    try {
        document = documentCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
            throw e;
        }
        return;
    }
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else if (uuidRepresentation == 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, standardEncodedValue), document.get("legacy"));
    } 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) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) UUID(java.util.UUID) Test(org.junit.Test)

Example 58 with BsonBinary

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

the class AbstractUuidRepresentationTest method shouldEncodePojoWithUuidRepresentation.

@Test
public void shouldEncodePojoWithUuidRepresentation() {
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        try {
            uuidIdPojoCollection.insertOne(new UuidIdPojo(uuid));
            fail();
        } catch (CodecConfigurationException e) {
        // all good
        }
    } else {
        uuidIdPojoCollection.insertOne(new UuidIdPojo(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 : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Test(org.junit.Test)

Example 59 with BsonBinary

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

the class AbstractUuidRepresentationTest method shouldEncodeDocumentWithUuidRepresentation.

@Test
public void shouldEncodeDocumentWithUuidRepresentation() {
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        try {
            documentCollection.insertOne(new Document("_id", uuid));
            fail();
        } catch (CodecConfigurationException e) {
        // all good
        }
    } else {
        documentCollection.insertOne(new Document("_id", uuid));
        BsonDocument document = bsonDocumentCollection.find().first();
        assertNotNull(document);
        BsonBinary uuidAsBinary = document.getBinary("_id");
        assertEquals(subType.getValue(), uuidAsBinary.getType());
        assertArrayEquals(encodedValue, uuidAsBinary.getData());
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 60 with BsonBinary

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

the class AbstractUuidRepresentationTest method shouldDecodeDbObjectWithUuidRepresentation.

@Test
public void shouldDecodeDbObjectWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
    DBObject document;
    try {
        document = dbObjectCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
            throw e;
        }
        return;
    }
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else if (uuidRepresentation == 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, standardEncodedValue), document.get("legacy"));
    } 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) UUID(java.util.UUID) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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