use of org.bson.Document in project mongo-java-driver by mongodb.
the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.
static BsonBinaryReader prepareReaderWithObjectToBeDecoded(final Object objectToDecode) {
//Need to encode it wrapped in a document to conform to the validation
Document document = new Document("wrapperDocument", objectToDecode);
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
byte[] documentAsByteArrayForReader;
try {
new DocumentCodec().encode(writer, document, EncoderContext.builder().build());
documentAsByteArrayForReader = outputBuffer.toByteArray();
} finally {
writer.close();
}
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
//have to read off the wrapper document so the reader is in the correct position for the test
reader.readStartDocument();
reader.readName();
return reader;
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DocumentCodecTest method shouldGenerateIdIfAbsent.
@Test
public void shouldGenerateIdIfAbsent() {
DocumentCodec documentCodec = new DocumentCodec();
Document document = new Document();
assertFalse(documentCodec.documentHasId(document));
document = documentCodec.generateIdIfAbsentFromDocument(document);
assertTrue(documentCodec.documentHasId(document));
assertEquals(BsonObjectId.class, documentCodec.getDocumentId(document).getClass());
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DocumentCodecTest method shouldNotGenerateIdIfPresent.
@Test
public void shouldNotGenerateIdIfPresent() {
DocumentCodec documentCodec = new DocumentCodec();
Document document = new Document("_id", 1);
assertTrue(documentCodec.documentHasId(document));
document = documentCodec.generateIdIfAbsentFromDocument(document);
assertTrue(documentCodec.documentHasId(document));
assertEquals(new BsonInt32(1), documentCodec.getDocumentId(document));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testNestedDocumentEncoding.
@Test
public void testNestedDocumentEncoding() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document();
doc.put("nested", new Document("x", 1));
documentCodec.encode(writer, doc, EncoderContext.builder().build());
BsonInput bsonInput = createInputBuffer();
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
assertEquals(doc, decodedDocument);
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testPrimitiveBSONTypeCodecs.
@Test
public void testPrimitiveBSONTypeCodecs() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document();
doc.put("oid", new ObjectId());
doc.put("integer", 1);
doc.put("long", 2L);
doc.put("string", "hello");
doc.put("double", 3.2);
doc.put("decimal", Decimal128.parse("0.100"));
doc.put("binary", new Binary(BsonBinarySubType.USER_DEFINED, new byte[] { 0, 1, 2, 3 }));
doc.put("date", new Date(1000));
doc.put("boolean", true);
doc.put("code", new Code("var i = 0"));
doc.put("minkey", new MinKey());
doc.put("maxkey", new MaxKey());
// doc.put("pattern", Pattern.compile("^hello")); // TODO: Pattern doesn't override equals method!
doc.put("null", null);
documentCodec.encode(writer, doc, EncoderContext.builder().build());
BsonInput bsonInput = createInputBuffer();
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
assertEquals(doc, decodedDocument);
}
Aggregations