Search in sources :

Example 51 with Document

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;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) Document(org.bson.Document) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 52 with Document

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());
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Example 53 with Document

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));
}
Also used : BsonInt32(org.bson.BsonInt32) Document(org.bson.Document) Test(org.junit.Test)

Example 54 with 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);
}
Also used : BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) BsonBinaryReader(org.bson.BsonBinaryReader) Document(org.bson.Document) Test(org.junit.Test)

Example 55 with Document

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);
}
Also used : MinKey(org.bson.types.MinKey) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) MaxKey(org.bson.types.MaxKey) BsonBinaryReader(org.bson.BsonBinaryReader) Binary(org.bson.types.Binary) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)1104 Test (org.junit.Test)795 ArrayList (java.util.ArrayList)74 Update (org.springframework.data.mongodb.core.query.Update)71 List (java.util.List)55 BsonDocument (org.bson.BsonDocument)53 ObjectId (org.bson.types.ObjectId)41 MongoDatabase (com.mongodb.client.MongoDatabase)40 Query (org.springframework.data.mongodb.core.query.Query)40 BasicDBObject (com.mongodb.BasicDBObject)39 MongoClient (com.mongodb.MongoClient)32 Bson (org.bson.conversions.Bson)32 ReturnDocument (com.mongodb.client.model.ReturnDocument)31 DBObject (com.mongodb.DBObject)27 DBRef (com.mongodb.DBRef)25 UnknownHostException (java.net.UnknownHostException)25 HashMap (java.util.HashMap)24 FullDocument (com.mongodb.client.model.changestream.FullDocument)23 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)21 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)21