Search in sources :

Example 31 with BsonInt32

use of org.bson.BsonInt32 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)

Example 32 with BsonInt32

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

the class CommandResultTest method testCommandFailure.

@Test
public void testCommandFailure() throws UnknownHostException {
    try {
        new CommandResult(new BsonDocument("ok", new BsonInt32(0)).append("errmsg", new BsonString("ns not found")).append("code", new BsonInt32(5000)), new ServerAddress()).throwOnError();
        fail("Should throw");
    } catch (MongoCommandException e) {
        assertEquals(5000, e.getCode());
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.Test)

Aggregations

BsonInt32 (org.bson.BsonInt32)32 BsonDocument (org.bson.BsonDocument)28 BsonString (org.bson.BsonString)18 Test (org.junit.Test)8 BsonArray (org.bson.BsonArray)6 BsonInt64 (org.bson.BsonInt64)6 BsonValue (org.bson.BsonValue)6 Document (org.bson.Document)3 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)3 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)3 HashMap (java.util.HashMap)2 BsonDocumentWriter (org.bson.BsonDocumentWriter)2 BsonDouble (org.bson.BsonDouble)2 BsonNumber (org.bson.BsonNumber)2 ServerAddress (com.mongodb.ServerAddress)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 UpdateRequest (com.mongodb.bulk.UpdateRequest)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 DescriptionHelper.createServerDescription (com.mongodb.connection.DescriptionHelper.createServerDescription)1 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)1