Search in sources :

Example 71 with BsonInt32

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

the class WriteConcernDocumentTest method getWriteConcern.

private WriteConcern getWriteConcern(final BsonDocument writeConcernDocument) {
    BsonValue wValue = writeConcernDocument.get("w");
    WriteConcern retVal;
    if (wValue == null) {
        retVal = WriteConcern.ACKNOWLEDGED;
    } else if (wValue instanceof BsonNumber) {
        retVal = new WriteConcern(wValue.asNumber().intValue());
    } else if (wValue instanceof BsonString) {
        retVal = new WriteConcern(wValue.asString().getValue());
    } else {
        throw new IllegalArgumentException("Unexpected w value: " + wValue);
    }
    if (writeConcernDocument.containsKey("wtimeoutMS")) {
        retVal = retVal.withWTimeout(writeConcernDocument.getNumber("wtimeoutMS", new BsonInt32(0)).intValue(), TimeUnit.MILLISECONDS);
    }
    if (writeConcernDocument.containsKey("journal")) {
        retVal = retVal.withJournal(writeConcernDocument.getBoolean("journal", BsonBoolean.FALSE).getValue());
    }
    return retVal;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonNumber(org.bson.BsonNumber) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 72 with BsonInt32

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

the class CommandMonitoringTestHelper method massageActualCommandStartedEvent.

private static CommandStartedEvent massageActualCommandStartedEvent(final CommandStartedEvent event, @Nullable final Map<String, BsonDocument> lsidMap, final CommandStartedEvent expectedCommandStartedEvent) {
    BsonDocument command = getWritableCloneOfCommand(event.getCommand());
    massageCommand(event, command);
    if (command.containsKey("readConcern") && (command.getDocument("readConcern").containsKey("afterClusterTime"))) {
        command.getDocument("readConcern").put("afterClusterTime", new BsonInt32(42));
    }
    // Tests expect maxTimeMS to be int32, but Java API requires maxTime to be a long.  This massage seems preferable to casting
    if (command.containsKey("maxTimeMS")) {
        command.put("maxTimeMS", new BsonInt32(command.getNumber("maxTimeMS").intValue()));
    }
    // Tests do not expect the "ns" field in a result after running createIndex.
    if (command.containsKey("createIndexes") && command.containsKey("indexes")) {
        massageCommandIndexes(command.getArray("indexes"));
    }
    massageActualCommand(command, expectedCommandStartedEvent.getCommand());
    return new CommandStartedEvent(event.getRequestId(), event.getConnectionDescription(), event.getDatabaseName(), event.getCommandName(), command);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent)

Example 73 with BsonInt32

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

the class MongoWriteExceptionTest method testExceptionProperties.

@Test
public void testExceptionProperties() {
    WriteError writeError = new WriteError(11000, "Duplicate key", new BsonDocument("x", new BsonInt32(1)));
    MongoWriteException e = new MongoWriteException(writeError, new ServerAddress("host1"));
    assertEquals("Write operation error on server host1:27017. Write error: WriteError{code=11000, message='Duplicate key', " + "details={\"x\": 1}}.", e.getMessage());
    assertEquals(writeError.getCode(), e.getCode());
    assertEquals(writeError, e.getError());
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 74 with BsonInt32

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

the class WriteConcernConnectionStringTest method getExpectedWriteConcern.

private WriteConcern getExpectedWriteConcern() {
    BsonDocument writeConcernDocument = definition.getDocument("writeConcern");
    BsonValue wValue = writeConcernDocument.get("w");
    WriteConcern retVal;
    if (wValue == null) {
        retVal = WriteConcern.ACKNOWLEDGED;
    } else if (wValue instanceof BsonNumber) {
        retVal = new WriteConcern(wValue.asNumber().intValue());
    } else if (wValue instanceof BsonString) {
        retVal = new WriteConcern(wValue.asString().getValue());
    } else {
        throw new IllegalArgumentException("Unexpected w value: " + wValue);
    }
    if (writeConcernDocument.containsKey("wtimeoutMS")) {
        retVal = retVal.withWTimeout(writeConcernDocument.getNumber("wtimeoutMS", new BsonInt32(0)).intValue(), TimeUnit.MILLISECONDS);
    }
    if (writeConcernDocument.containsKey("journal")) {
        retVal = retVal.withJournal(writeConcernDocument.getBoolean("journal", BsonBoolean.FALSE).getValue());
    }
    return retVal;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonNumber(org.bson.BsonNumber) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 75 with BsonInt32

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

the class MapReducePublisherImplTest method shouldBuildTheExpectedMapReduceWithInlineResultsOperation.

@DisplayName("Should build the expected MapReduceWithInlineResultsOperation")
@Test
void shouldBuildTheExpectedMapReduceWithInlineResultsOperation() {
    configureBatchCursor();
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor()));
    com.mongodb.reactivestreams.client.MapReducePublisher<Document> publisher = new MapReducePublisherImpl<>(null, createMongoOperationPublisher(executor), MAP_FUNCTION, REDUCE_FUNCTION);
    MapReduceWithInlineResultsOperation<Document> expectedOperation = new MapReduceWithInlineResultsOperation<>(NAMESPACE, new BsonJavaScript(MAP_FUNCTION), new BsonJavaScript(REDUCE_FUNCTION), getDefaultCodecRegistry().get(Document.class)).verbose(true);
    // default input should be as expected
    Flux.from(publisher).blockFirst();
    MapReducePublisherImpl.WrappedMapReduceReadOperation operation = (MapReducePublisherImpl.WrappedMapReduceReadOperation) executor.getReadOperation();
    assertNotNull(operation);
    assertOperationIsTheSameAs(expectedOperation, operation.getOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    // Should apply settings
    publisher.batchSize(100).bypassDocumentValidation(true).collation(COLLATION).filter(new Document("filter", 1)).finalizeFunction(FINALIZE_FUNCTION).limit(999).maxTime(10, SECONDS).scope(new Document("scope", 1)).sort(Sorts.ascending("sort")).verbose(false);
    expectedOperation.collation(COLLATION).collation(COLLATION).filter(BsonDocument.parse("{filter: 1}")).finalizeFunction(new BsonJavaScript(FINALIZE_FUNCTION)).limit(999).maxTime(10, SECONDS).maxTime(10, SECONDS).scope(new BsonDocument("scope", new BsonInt32(1))).sort(new BsonDocument("sort", new BsonInt32(1))).verbose(false);
    configureBatchCursor();
    Flux.from(publisher).blockFirst();
    operation = (MapReducePublisherImpl.WrappedMapReduceReadOperation) executor.getReadOperation();
    assertNotNull(operation);
    assertOperationIsTheSameAs(expectedOperation, operation.getOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonJavaScript(org.bson.BsonJavaScript) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

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