use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDBObjectWithUuidRepresentation.
@Test
public void shouldDecodeDBObjectWithUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
DBObject document;
try {
document = dbObjectCollection.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"));
}
}
use of org.bson.BSONException 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"));
}
}
use of org.bson.BSONException in project pulsar by yahoo.
the class MongoSink method flush.
private void flush() {
final List<Document> docsToInsert = new ArrayList<>();
final List<Record<byte[]>> recordsToInsert;
synchronized (this) {
if (incomingList.isEmpty()) {
return;
}
recordsToInsert = incomingList;
incomingList = Lists.newArrayList();
}
final Iterator<Record<byte[]>> iter = recordsToInsert.iterator();
while (iter.hasNext()) {
final Record<byte[]> record = iter.next();
try {
final byte[] docAsBytes = record.getValue();
final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
docsToInsert.add(doc);
} catch (JsonParseException | BSONException e) {
log.error("Bad message", e);
record.fail();
iter.remove();
}
}
if (docsToInsert.size() > 0) {
collection.insertMany(docsToInsert).subscribe(new DocsToInsertSubscriber(docsToInsert, recordsToInsert));
}
}
use of org.bson.BSONException in project pulsar by apache.
the class MongoSink method flush.
private void flush() {
final List<Document> docsToInsert = new ArrayList<>();
final List<Record<byte[]>> recordsToInsert;
synchronized (this) {
if (incomingList.isEmpty()) {
return;
}
recordsToInsert = incomingList;
incomingList = Lists.newArrayList();
}
final Iterator<Record<byte[]>> iter = recordsToInsert.iterator();
while (iter.hasNext()) {
final Record<byte[]> record = iter.next();
try {
final byte[] docAsBytes = record.getValue();
final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
docsToInsert.add(doc);
} catch (JsonParseException | BSONException e) {
log.error("Bad message", e);
record.fail();
iter.remove();
}
}
if (docsToInsert.size() > 0) {
collection.insertMany(docsToInsert).subscribe(new DocsToInsertSubscriber(docsToInsert, recordsToInsert));
}
}
Aggregations