Search in sources :

Example 66 with BsonBinary

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

the class JsonWriterTest method testBinaryRelaxed.

@Test
public void testBinaryRelaxed() {
    List<TestData<BsonBinary>> tests = asList(new TestData<BsonBinary>(new BsonBinary(new byte[0]), "{\"$binary\": {\"base64\": \"\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1 }), "{\"$binary\": {\"base64\": \"AQ==\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1, 2 }), "{\"$binary\": {\"base64\": \"AQI=\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1, 2, 3 }), "{\"$binary\": {\"base64\": \"AQID\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary((byte) 0x80, new byte[] { 1, 2, 3 }), "{\"$binary\": {\"base64\": \"AQID\", " + "\"subType\": \"80\"}}"));
    for (final TestData<BsonBinary> cur : tests) {
        stringWriter = new StringWriter();
        writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
        writer.writeStartDocument();
        writer.writeBinaryData("binary", cur.value);
        writer.writeEndDocument();
        String expected = "{\"binary\": " + cur.expected + "}";
        assertEquals(expected, stringWriter.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 67 with BsonBinary

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

the class JsonWriterTest method shouldThrowAnErrorIfTryingToWriteValueIntoAJavascriptScope.

@Test(expected = BsonInvalidOperationException.class)
public void shouldThrowAnErrorIfTryingToWriteValueIntoAJavascriptScope() {
    writer.writeStartDocument();
    writer.writeJavaScriptWithScope("js1", "var i = 1");
    writer.writeBinaryData(new BsonBinary(new byte[] { 0, 0, 1, 0 }));
}
Also used : BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 68 with BsonBinary

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

the class JsonReaderTest method testLegacyUserDefinedBinaryWithNumericType.

@Test
public void testLegacyUserDefinedBinaryWithNumericType() {
    String json = "{ \"$binary\" : \"AQID\", \"$type\" : 128 }";
    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 69 with BsonBinary

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

the class JsonReaderTest method testUuid.

@Test
public void testUuid() {
    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 70 with BsonBinary

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

the class JsonReaderTest method testBinDataWithNew.

@Test
public void testBinDataWithNew() {
    String json = "{ \"a\" : new BinData(3, AQID) }";
    testStringAndStream(json, bsonReader -> {
        bsonReader.readStartDocument();
        assertEquals(BsonType.BINARY, bsonReader.readBsonType());
        BsonBinary binary = bsonReader.readBinaryData();
        assertEquals(3, binary.getType());
        assertArrayEquals(new byte[] { 1, 2, 3 }, binary.getData());
        bsonReader.readEndDocument();
        assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
        return null;
    });
}
Also used : 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