Search in sources :

Example 11 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldGenerateIdIfAbsent.

@Test
public void shouldGenerateIdIfAbsent() {
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject document = new BasicDBObject();
    assertFalse(dbObjectCodec.documentHasId(document));
    document = dbObjectCodec.generateIdIfAbsentFromDocument(document);
    assertTrue(dbObjectCodec.documentHasId(document));
    assertEquals(BsonObjectId.class, dbObjectCodec.getDocumentId(document).getClass());
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 12 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldNotGenerateIdIfPresent.

@Test
public void shouldNotGenerateIdIfPresent() {
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject document = new BasicDBObject("_id", 1);
    assertTrue(dbObjectCodec.documentHasId(document));
    document = dbObjectCodec.generateIdIfAbsentFromDocument(document);
    assertTrue(dbObjectCodec.documentHasId(document));
    assertEquals(new BsonInt32(1), dbObjectCodec.getDocumentId(document));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 13 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldEncodedNestedMapsListsAndDocuments.

@Test
public void shouldEncodedNestedMapsListsAndDocuments() {
    //  {"0" : 0, "1", 1}
    byte[] zeroOneDocumentBytes = new byte[] { 19, 0, 0, 0, 16, 48, 0, 0, 0, 0, 0, 16, 49, 0, 1, 0, 0, 0, 0 };
    Map<String, Object> zeroOneMap = new HashMap<String, Object>();
    zeroOneMap.put("0", 0);
    zeroOneMap.put("1", 1);
    DBObject zeroOneDBObject = new BasicDBObject();
    zeroOneDBObject.putAll(zeroOneMap);
    DBObject zeroOneDBList = new BasicDBList();
    zeroOneDBList.putAll(zeroOneMap);
    List<Integer> zeroOneList = asList(0, 1);
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject().append("map", zeroOneMap).append("dbDocument", zeroOneDBObject).append("dbList", zeroOneDBList).append("list", zeroOneList).append("array", new int[] { 0, 1 }).append("lazyDoc", new LazyDBObject(zeroOneDocumentBytes, new LazyBSONCallback())).append("lazyArray", new LazyDBList(zeroOneDocumentBytes, new LazyBSONCallback()));
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonDocument zeroOneBsonDocument = new BsonDocument().append("0", new BsonInt32(0)).append("1", new BsonInt32(1));
    BsonArray zeroOneBsonArray = new BsonArray(asList(new BsonInt32(0), new BsonInt32(1)));
    assertEquals(new BsonDocument("map", zeroOneBsonDocument).append("dbDocument", zeroOneBsonDocument).append("dbList", zeroOneBsonArray).append("list", zeroOneBsonArray).append("array", zeroOneBsonArray).append("lazyDoc", zeroOneBsonDocument).append("lazyArray", zeroOneBsonArray), writer.getDocument());
}
Also used : HashMap(java.util.HashMap) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) LazyBSONCallback(org.bson.LazyBSONCallback) BsonArray(org.bson.BsonArray) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Aggregations

BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)13 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)13 Test (org.junit.Test)12 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)7 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)7 Document (org.bson.Document)6 ObjectId (org.bson.types.ObjectId)5 BsonDocument (org.bson.BsonDocument)4 BsonDocumentWriter (org.bson.BsonDocumentWriter)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)3 Worker (com.mongodb.client.test.Worker)3 WorkerCodecProvider (com.mongodb.client.test.WorkerCodecProvider)3 Date (java.util.Date)3 BsonInt32 (org.bson.BsonInt32)3 BsonArray (org.bson.BsonArray)2 Function (com.mongodb.Function)1 FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)1 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1