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());
}
}
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"));
}
}
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());
}
}
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());
}
}
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"));
}
}
Aggregations