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;
}
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);
}
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());
}
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;
}
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());
}
Aggregations