Search in sources :

Example 61 with BsonInt32

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

the class CommandMonitoringTest method massageActualCommandSucceededEvent.

private CommandSucceededEvent massageActualCommandSucceededEvent(final CommandSucceededEvent actual) {
    BsonDocument response = getWritableCloneOfCommand(actual.getResponse());
    // massage numbers that are the wrong BSON type
    response.put("ok", new BsonDouble(response.getNumber("ok").doubleValue()));
    if (response.containsKey("n")) {
        response.put("n", new BsonInt32(response.getNumber("n").intValue()));
    }
    if (actual.getCommandName().equals("find") || actual.getCommandName().equals("getMore")) {
        if (response.containsKey("cursor")) {
            if (response.getDocument("cursor").containsKey("id") && !response.getDocument("cursor").getInt64("id").equals(new BsonInt64(0))) {
                response.getDocument("cursor").put("id", new BsonInt64(42));
            }
        }
    } else if (actual.getCommandName().equals("killCursors")) {
        response.getArray("cursorsUnknown").set(0, new BsonInt64(42));
    } else if (isWriteCommand(actual.getCommandName())) {
        if (response.containsKey("writeErrors")) {
            for (Iterator<BsonValue> iter = response.getArray("writeErrors").iterator(); iter.hasNext(); ) {
                BsonDocument cur = iter.next().asDocument();
                cur.put("code", new BsonInt32(42));
                cur.put("errmsg", new BsonString(""));
            }
        }
        if (actual.getCommandName().equals("update")) {
            response.remove("nModified");
        }
    }
    return new CommandSucceededEvent(actual.getRequestId(), actual.getConnectionDescription(), actual.getCommandName(), response, actual.getElapsedTime(TimeUnit.NANOSECONDS));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonDouble(org.bson.BsonDouble) BsonValue(org.bson.BsonValue)

Example 62 with BsonInt32

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

the class CrudTest method toResult.

private BsonDocument toResult(final MongoOperationUpdateResult operation) {
    UpdateResult updateResult = operation.get();
    BsonDocument resultDoc = new BsonDocument("matchedCount", new BsonInt32((int) updateResult.getMatchedCount()));
    if (updateResult.isModifiedCountAvailable()) {
        resultDoc.append("modifiedCount", new BsonInt32((int) updateResult.getModifiedCount()));
    }
    // in replaceOne-pre_2.6
    if (updateResult.getUpsertedId() != null && !updateResult.getUpsertedId().isObjectId()) {
        resultDoc.append("upsertedId", updateResult.getUpsertedId());
    }
    resultDoc.append("upsertedCount", updateResult.getUpsertedId() == null ? new BsonInt32(0) : new BsonInt32(1));
    return toResult(resultDoc);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 63 with BsonInt32

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

the class CollectionAcceptanceTest method shouldBeAbleToUseBsonValueToDistinctDocumentsOfVaryingTypes.

@SuppressWarnings("unchecked")
@Test
public void shouldBeAbleToUseBsonValueToDistinctDocumentsOfVaryingTypes() {
    List<Object> mixedList = new ArrayList<Object>();
    mixedList.add(2);
    mixedList.add("d");
    mixedList.add(new Document("e", 3));
    collection.drop();
    collection.insertMany(asList(new Document("id", "a"), new Document("id", 1), new Document("id", new Document("b", "c")), new Document("id", new Document("list", mixedList))));
    List<BsonValue> distinct = collection.distinct("id", BsonValue.class).into(new ArrayList<BsonValue>());
    assertTrue(distinct.containsAll(asList(new BsonString("a"), new BsonInt32(1), new BsonDocument("b", new BsonString("c")), new BsonDocument("list", new BsonArray(asList(new BsonInt32(2), new BsonString("d"), new BsonDocument("e", new BsonInt32(3))))))));
    distinct = collection.distinct("id", new Document("id", new Document("$ne", 1)), BsonValue.class).into(new ArrayList<BsonValue>());
    assertTrue(distinct.containsAll(asList(new BsonString("a"), new BsonDocument("b", new BsonString("c")), new BsonDocument("list", new BsonArray(asList(new BsonInt32(2), new BsonString("d"), new BsonDocument("e", new BsonInt32(3))))))));
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 64 with BsonInt32

use of org.bson.BsonInt32 in project immutables by immutables.

the class TypeConversionTest method int32.

@Test
public void int32() throws IOException {
    BsonInt32 value = new BsonInt32(42);
    check(Jsons.readerAt(value).peek()).is(JsonToken.NUMBER);
    check(Jsons.readerAt(value).nextInt()).is(42);
    check(Jsons.readerAt(value).nextLong()).is(42L);
    check(Jsons.readerAt(value).nextDouble()).is(42D);
    check(Jsons.readerAt(value).nextString()).is("42");
}
Also used : BsonInt32(org.bson.BsonInt32) Test(org.junit.Test)

Example 65 with BsonInt32

use of org.bson.BsonInt32 in project immutables by immutables.

the class TypeConversionTest method int64.

@Test
public void int64() throws IOException {
    BsonInt32 value = new BsonInt32(64);
    check(Jsons.readerAt(value).peek()).is(JsonToken.NUMBER);
    check(Jsons.readerAt(new BsonInt64(64)).nextInt()).is(64);
    check(Jsons.readerAt(new BsonInt64(64)).nextLong()).is(64L);
    check(Jsons.readerAt(new BsonInt64(64)).nextDouble()).is(64D);
    check(Jsons.readerAt(new BsonInt64(64)).nextString()).is("64");
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) Test(org.junit.Test)

Aggregations

BsonInt32 (org.bson.BsonInt32)106 BsonDocument (org.bson.BsonDocument)91 BsonString (org.bson.BsonString)58 BsonArray (org.bson.BsonArray)26 Test (org.junit.Test)26 Document (org.bson.Document)23 BsonInt64 (org.bson.BsonInt64)20 Test (org.junit.jupiter.api.Test)16 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)13 List (java.util.List)9 BsonDouble (org.bson.BsonDouble)9 DisplayName (org.junit.jupiter.api.DisplayName)8 BsonNull (org.bson.BsonNull)7 MongoClientSettings (com.mongodb.MongoClientSettings)6 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)5 Map (java.util.Map)5 BsonBoolean (org.bson.BsonBoolean)5 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)4 ServerAddress (com.mongodb.ServerAddress)4