use of org.bson.BsonBinary in project drill by apache.
the class TestBsonRecordReader method testBinaryTypes.
@Test
public void testBinaryTypes() throws IOException {
// test with different binary types
BsonDocument bsonDoc = new BsonDocument();
// Binary
// String
byte[] bytes = "binaryValue".getBytes();
bsonDoc.append("binaryKey", new BsonBinary(BsonBinarySubType.BINARY, bytes));
// String
byte[] bytesString = "binaryStringValue".getBytes();
bsonDoc.append("binaryStringKey", new BsonBinary((byte) 2, bytesString));
// Double
byte[] bytesDouble = new byte[8];
java.nio.ByteBuffer.wrap(bytesDouble).putDouble(23.0123);
BsonBinary bsonDouble = new BsonBinary((byte) 1, bytesDouble);
bsonDoc.append("binaryDouble", bsonDouble);
// Boolean
byte[] booleanBytes = new byte[8];
java.nio.ByteBuffer.wrap(booleanBytes).put((byte) 1);
BsonBinary bsonBoolean = new BsonBinary((byte) 8, booleanBytes);
bsonDoc.append("bsonBoolean", bsonBoolean);
writer.reset();
bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
assertArrayEquals(bytes, mapReader.reader("binaryKey").readByteArray());
assertEquals("binaryStringValue", mapReader.reader("binaryStringKey").readText().toString());
assertEquals(23.0123, mapReader.reader("binaryDouble").readDouble(), 0);
FieldReader reader = mapReader.reader("bsonBoolean");
assertEquals(true, reader.readBoolean());
}
use of org.bson.BsonBinary in project pinpoint by naver.
the class WritecontextTest method parseTestAbbreviation_BsonValueArray.
@Test
public void parseTestAbbreviation_BsonValueArray() throws IOException {
BsonInt32[] bsonInt32s = new BsonInt32[40];
for (int i = 0; i < 40; i++) {
bsonInt32s[i] = new BsonInt32(i + 1);
}
BsonDocument document = new BsonDocument().append("double", new BsonDouble(12.3)).append("arrayInt", new BsonArray(Arrays.asList(bsonInt32s))).append("binary1", new BsonBinary(new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte) 0x9d })).append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[] { 1, 1, 1, 1, 1 }));
BasicDBObject query = new BasicDBObject();
query.put("ComplexBson", document);
logger.debug("document:{}", document);
NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[] { query }, true);
logger.debug("val:{}", stringStringValue);
List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
Assert.assertEquals(list.size(), 1);
Map<String, ?> query1Map = (Map<String, ?>) list.get(0);
checkValue(query1Map);
}
use of org.bson.BsonBinary in project pinpoint by naver.
the class WritecontextTest method parseBsonArrayWithValues.
@Test
public void parseBsonArrayWithValues() throws IOException {
BsonValue a = new BsonString("stest");
BsonValue b = new BsonDouble(111);
BsonValue c = new BsonBoolean(true);
BsonDocument document = new BsonDocument().append("int32", new BsonInt32(12)).append("int64", new BsonInt64(77L)).append("bo\"olean", new BsonBoolean(true)).append("date", new BsonDateTime(new Date().getTime())).append("double", new BsonDouble(12.3)).append("string", new BsonString("pinpoint")).append("objectId", new BsonObjectId(new ObjectId())).append("code", new BsonJavaScript("int i = 10;")).append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1)))).append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big")).append("symbol", new BsonSymbol("wow")).append("timestamp", new BsonTimestamp(0x12345678, 5)).append("undefined", new BsonUndefined()).append("binary1", new BsonBinary(new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20 })).append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[] { 1, 1, 1, 1, 1 })).append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7)))).append("document", new BsonDocument("a", new BsonInt32(77))).append("dbPointer", new BsonDbPointer("db.coll", new ObjectId())).append("null", new BsonNull()).append("decimal128", new BsonDecimal128(new Decimal128(55)));
BasicDBObject query = new BasicDBObject();
query.put("ComplexBson", document);
logger.debug("document:{}", document);
NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[] { query }, true);
logger.debug("val:{}", stringStringValue);
List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
Assert.assertEquals(list.size(), 1);
Map<String, ?> query1Map = (Map<String, ?>) list.get(0);
checkValue(query1Map);
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldDecodePojoWithStandardUuidRepresentation.
@Test
public void shouldDecodePojoWithStandardUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, UuidRepresentation.STANDARD)));
try {
UuidIdPojo document = uuidIdPojoCollection.find().first();
assertNotNull(document);
assertEquals(uuid, document.getId());
} catch (BSONException e) {
assertNotEquals(UuidRepresentation.STANDARD, uuidRepresentation);
}
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class ClientEncryptionDataKeyAndDoubleEncryptionTest method testProvider.
@Test
public void testProvider() {
String keyAltName = format("%s_altname", providerName);
BsonBinary dataKeyId = clientEncryption.createDataKey(providerName, new DataKeyOptions().keyAltNames(singletonList(keyAltName)).masterKey(getMasterKey()));
assertEquals(4, dataKeyId.getType());
ArrayList<Document> dataKeys = client.getDatabase("keyvault").getCollection("datakeys").find(eq("_id", dataKeyId)).into(new ArrayList<>());
assertEquals(1, dataKeys.size());
Document dataKey = dataKeys.get(0);
assertEquals(providerName, dataKey.get("masterKey", new Document()).get("provider", ""));
String insertWriteConcern = commandListener.getCommandStartedEvent("insert").getCommand().getDocument("writeConcern", new BsonDocument()).getString("w", new BsonString("")).getValue();
assertEquals("majority", insertWriteConcern);
String stringToEncrypt = format("hello %s", providerName);
BsonBinary encrypted = clientEncryption.encrypt(new BsonString(stringToEncrypt), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(dataKeyId));
assertEquals(6, encrypted.getType());
Document insertDocument = new Document("_id", providerName);
insertDocument.put("value", encrypted);
clientEncrypted.getDatabase("db").getCollection("coll").insertOne(insertDocument);
Document decryptedDocument = clientEncrypted.getDatabase("db").getCollection("coll").find(eq("_id", providerName)).first();
assertNotNull(decryptedDocument);
assertEquals(stringToEncrypt, decryptedDocument.get("value", ""));
BsonBinary encryptedKeyAltName = clientEncryption.encrypt(new BsonString(stringToEncrypt), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyAltName(keyAltName));
assertEquals(encrypted, encryptedKeyAltName);
assertThrows(MongoClientException.class, () -> clientEncrypted.getDatabase("db").getCollection("coll").insertOne(new Document("encrypted_placeholder", encrypted)));
}
Aggregations