Search in sources :

Example 26 with BsonBinary

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

the class JsonReaderTest method testLegacyUserDefinedBinaryWithKeyOrderReversed.

@Test
public void testLegacyUserDefinedBinaryWithKeyOrderReversed() {
    String json = "{ \"$type\" : \"80\", \"$binary\" : \"AQID\" }";
    testStringAndStream(json, bsonReader -> {
        assertEquals(BsonType.BINARY, bsonReader.readBsonType());
        BsonBinary binary = bsonReader.readBinaryData();
        assertEquals(BsonBinarySubType.USER_DEFINED.getValue(), binary.getType());
        assertArrayEquals(new byte[] { 1, 2, 3 }, binary.getData());
        assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
        return null;
    });
}
Also used : BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 27 with BsonBinary

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

the class JsonReaderTest method testUuidConstructor.

@Test
public void testUuidConstructor() {
    String json = "UUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")";
    testStringAndStream(json, bsonReader -> {
        assertEquals(BsonType.BINARY, bsonReader.readBsonType());
        BsonBinary binary = bsonReader.readBinaryData();
        assertEquals(BsonBinarySubType.UUID_STANDARD.getValue(), binary.getType());
        assertArrayEquals(new byte[] { -75, -14, 30, 12, 42, 13, 66, -42, -83, 3, -40, 39, 0, -115, -118, -74 }, binary.getData());
        assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
        return null;
    });
}
Also used : BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 28 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDocumentWithUuidRepresentation.

@Test
public void shouldDecodeDocumentWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    Document document;
    try {
        document = documentCollection.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) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 29 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodePojoWithStandardUuidRepresentation.

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

Example 30 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldEncodePojoWithUuidRepresentation.

@Test
public void shouldEncodePojoWithUuidRepresentation() {
    uuidIdPojoCollection.insertOne(new UuidIdPojo(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) 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