Search in sources :

Example 46 with BsonInt32

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

the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationForHintPlusHintString.

@DisplayName("Should build the expected AggregateOperation when both hint and hintString are set")
@Test
void shouldBuildTheExpectedOperationForHintPlusHintString() {
    List<BsonDocument> pipeline = singletonList(BsonDocument.parse("{'$match': 1}"));
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor()));
    AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
    AggregateOperation<Document> expectedOperation = new AggregateOperation<>(NAMESPACE, pipeline, getDefaultCodecRegistry().get(Document.class)).batchSize(Integer.MAX_VALUE).retryReads(true);
    publisher.hint(new Document("x", 1)).hintString("x_1");
    expectedOperation.hint(new BsonDocument("x", new BsonInt32(1)));
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) AggregateOperation(com.mongodb.internal.operation.AggregateOperation) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 47 with BsonInt32

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

the class TypeConversionTest method int32.

@Test
void int32() throws IOException {
    final BsonInt32 value = new BsonInt32(42);
    check(Parsers.parserAt(value).currentToken()).is(JsonToken.VALUE_NUMBER_INT);
    check(Parsers.parserAt(value).getNumberType()).is(JsonParser.NumberType.INT);
    check(Parsers.parserAt(value).getIntValue()).is(42);
    check(Parsers.parserAt(value).getLongValue()).is(42L);
    check(Parsers.parserAt(value).getDoubleValue()).is(42D);
    check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(42));
    check(Parsers.parserAt(value).getBigIntegerValue()).is(BigInteger.valueOf(42));
    check(Parsers.parserAt(value).getNumberValue()).is(42);
}
Also used : BsonInt32(org.bson.BsonInt32) Test(org.junit.jupiter.api.Test)

Example 48 with BsonInt32

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

the class BsonReaderStreamingTest method singleValue.

@Test
public void singleValue() throws IOException {
    BsonDocument document = new BsonDocument();
    document.append("value", new BsonInt32(42));
    JsonReader reader = createReader(document);
    check(reader.peek()).is(JsonToken.BEGIN_OBJECT);
    reader.beginObject();
    check(reader.hasNext());
    check(reader.peek()).is(JsonToken.NAME);
    check(reader.nextName()).is("value");
    check(reader.peek()).is(JsonToken.NUMBER);
    check(reader.nextInt()).is(42);
    check(reader.peek()).is(JsonToken.END_OBJECT);
    reader.endObject();
    check(!reader.hasNext());
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 49 with BsonInt32

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

the class BsonReaderTest method bsonToGson.

/**
 * Reading from BSON to GSON
 */
@Test
public void bsonToGson() throws Exception {
    BsonDocument document = new BsonDocument();
    document.append("boolean", new BsonBoolean(true));
    document.append("int32", new BsonInt32(32));
    document.append("int64", new BsonInt64(64));
    document.append("double", new BsonDouble(42.42D));
    document.append("string", new BsonString("foo"));
    document.append("null", new BsonNull());
    document.append("array", new BsonArray());
    document.append("object", new BsonDocument());
    JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document)));
    check(element.isJsonObject());
    check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean());
    check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean());
    check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32);
    check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L);
    check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D);
    check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString());
    check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo");
    check(element.getAsJsonObject().get("null").isJsonNull());
    check(element.getAsJsonObject().get("array").isJsonArray());
    check(element.getAsJsonObject().get("object").isJsonObject());
}
Also used : BsonNull(org.bson.BsonNull) BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) JsonElement(com.google.gson.JsonElement) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble) BsonDocumentReader(org.bson.BsonDocumentReader) BsonBoolean(org.bson.BsonBoolean) Test(org.junit.Test)

Example 50 with BsonInt32

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

the class AbstractServerDiscoveryAndMonitoringTest method applyResponse.

protected void applyResponse(final BsonArray response) {
    ServerAddress serverAddress = new ServerAddress(response.get(0).asString().getValue());
    BsonDocument isMasterResult = response.get(1).asDocument();
    ServerDescription serverDescription;
    if (isMasterResult.isEmpty()) {
        serverDescription = ServerDescription.builder().type(ServerType.UNKNOWN).state(CONNECTING).address(serverAddress).build();
    } else {
        serverDescription = createServerDescription(serverAddress, isMasterResult, getVersion(new BsonDocument("versionArray", new BsonArray(asList(new BsonInt32(2), new BsonInt32(6), new BsonInt32(0))))), 5000000);
    }
    factory.sendNotification(serverAddress, serverDescription);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) DescriptionHelper.createServerDescription(com.mongodb.connection.DescriptionHelper.createServerDescription) BsonArray(org.bson.BsonArray) ServerAddress(com.mongodb.ServerAddress)

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