use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldEncodeIterableMapAsMap.
@Test
public void shouldEncodeIterableMapAsMap() {
IterableMap iterableMap = new IterableMap();
iterableMap.put("first", 1);
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject doc = new BasicDBObject("map", iterableMap);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
assertEquals(new BsonDocument("map", new BsonDocument("first", new BsonInt32(1))), writer.getDocument());
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class AbstractUnifiedTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
assumeTrue("Skipping test: " + definition.getString("skipReason", new BsonString("")).getValue(), !definition.containsKey("skipReason"));
assumeFalse("Skipping test of count", filename.equals("count.json"));
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
collectionHelper.killAllSessions();
if (!isDataLakeTest()) {
collectionHelper.create(collectionName, new CreateCollectionOptions(), WriteConcern.MAJORITY);
}
if (!data.isEmpty()) {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collectionHelper.insertDocuments(documents, WriteConcern.MAJORITY);
}
if (definition.containsKey("failPoint")) {
collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
}
final BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
connectionString = getConnectionString();
useMultipleMongoses = definition.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue();
if (useMultipleMongoses) {
assumeTrue(isSharded());
connectionString = getMultiMongosConnectionString();
assumeTrue("The system property org.mongodb.test.transaction.uri is not set.", connectionString != null);
}
MongoClientSettings.Builder builder = getMongoClientSettingsBuilder().applyConnectionString(connectionString).addCommandListener(commandListener).applyToClusterSettings(clusterSettingsBuilder -> {
if (clientOptions.containsKey("serverSelectionTimeoutMS")) {
clusterSettingsBuilder.serverSelectionTimeout(clientOptions.getNumber("serverSelectionTimeoutMS").longValue(), MILLISECONDS);
}
if (clientOptions.containsKey("directConnection")) {
setDirectConnection(clusterSettingsBuilder);
}
}).applyToSocketSettings(new Block<SocketSettings.Builder>() {
@Override
public void apply(final SocketSettings.Builder builder) {
builder.readTimeout(clientOptions.getInt32("socketTimeoutMS", new BsonInt32(toIntExact(SECONDS.toMillis(5)))).getValue(), MILLISECONDS);
if (clientOptions.containsKey("connectTimeoutMS")) {
builder.connectTimeout(clientOptions.getNumber("connectTimeoutMS").intValue(), MILLISECONDS);
}
}
}).writeConcern(getWriteConcern(clientOptions)).readConcern(getReadConcern(clientOptions)).readPreference(getReadPreference(clientOptions)).retryWrites(clientOptions.getBoolean("retryWrites", BsonBoolean.FALSE).getValue()).retryReads(false).applyToConnectionPoolSettings(poolSettingsBuilder -> {
poolSettingsBuilder.addConnectionPoolListener(connectionPoolListener);
if (clientOptions.containsKey("minPoolSize")) {
poolSettingsBuilder.minSize(clientOptions.getInt32("minPoolSize").getValue());
}
}).applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(50, MILLISECONDS);
builder.minHeartbeatFrequency(MIN_HEARTBEAT_FREQUENCY_MS, MILLISECONDS);
builder.addServerListener(serverListener);
}
});
if (clientOptions.containsKey("heartbeatFrequencyMS")) {
builder.applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(clientOptions.getInt32("heartbeatFrequencyMS").intValue(), MILLISECONDS);
}
});
}
if (clientOptions.containsKey("appname")) {
builder.applicationName(clientOptions.getString("appname").getValue());
}
if (clientOptions.containsKey("w")) {
if (clientOptions.isString("w")) {
builder.writeConcern(new WriteConcern(clientOptions.getString("w").getValue()));
} else if (clientOptions.isNumber("w")) {
builder.writeConcern(new WriteConcern(clientOptions.getNumber("w").intValue()));
}
}
StreamFactoryFactory streamFactoryFactory = getStreamFactoryFactory();
if (streamFactoryFactory != null) {
builder.streamFactoryFactory(streamFactoryFactory);
}
mongoClient = createMongoClient(builder.build());
database = mongoClient.getDatabase(databaseName);
if (useMultipleMongoses) {
// non-transactional distinct operation to avoid StaleDbVersion error
runDistinctOnEachNode();
}
helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection(collectionName, BsonDocument.class), null, mongoClient);
sessionsMap = new HashMap<>();
lsidMap = new HashMap<>();
if (createSessions && serverVersionAtLeast(3, 6)) {
ClientSession sessionZero = createSession("session0");
ClientSession sessionOne = createSession("session1");
sessionsMap.put("session0", sessionZero);
sessionsMap.put("session1", sessionOne);
lsidMap.put("session0", sessionZero.getServerSession().getIdentifier());
lsidMap.put("session1", sessionOne.getServerSession().getIdentifier());
}
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class ChangeStreamProseTest method setFailPoint.
private void setFailPoint(final String command, final int errCode) {
failPointDocument = new BsonDocument("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument("times", new BsonInt32(1))).append("data", new BsonDocument("failCommands", new BsonArray(asList(new BsonString(command)))).append("errorCode", new BsonInt32(errCode)).append("errorLabels", new BsonArray(asList(new BsonString("ResumableChangeStreamError")))));
getCollectionHelper().runAdminCommand(failPointDocument);
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class AbstractClientSideEncryptionDeadlockTest method shouldPassAllOutcomes.
@ParameterizedTest
@MethodSource("testArgumentProvider")
public void shouldPassAllOutcomes(final int maxPoolSize, final int expectedNumberOfClientsCreated, final boolean bypassAutoEncryption, final boolean externalKeyVaultClient, final List<ExpectedEvent> expectedEncryptingClientEvents, final List<ExpectedEvent> expectedExternalKeyVaultsClientEvents) {
AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).bypassAutoEncryption(bypassAutoEncryption);
TestCommandListener externalKeyVaultClientCommandListener = new TestCommandListener(singletonList("commandStartedEvent"), emptyList());
if (externalKeyVaultClient) {
autoEncryptionSettingsBuilder.keyVaultMongoClientSettings(getKeyVaultClientSettings(externalKeyVaultClientCommandListener));
}
TestCommandListener encryptingClientCommandListener = new TestCommandListener(singletonList("commandStartedEvent"), emptyList());
encryptingClient = createMongoClient(getClientSettings(maxPoolSize, encryptingClientCommandListener, autoEncryptionSettingsBuilder.build()));
BsonDocument unencryptedDocument = new BsonDocument("_id", new BsonInt32(0)).append("encrypted", new BsonString("string0"));
if (bypassAutoEncryption) {
getMongoClient().getDatabase("db").getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(new BsonDocument("_id", new BsonInt32(0)).append("encrypted", cipherText));
} else {
encryptingClient.getDatabase("db").getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(unencryptedDocument);
}
BsonDocument result = encryptingClient.getDatabase("db").getCollection("coll", BsonDocument.class).find().filter(Filters.eq("_id", 0)).first();
assertEquals(unencryptedDocument, result);
assertEquals(expectedNumberOfClientsCreated, getNumUniqueClients(encryptingClientCommandListener), "Unique clients");
assertEventEquality(encryptingClientCommandListener, expectedEncryptingClientEvents);
assertEventEquality(externalKeyVaultClientCommandListener, expectedExternalKeyVaultsClientEvents);
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class AbstractExplainTest method testExplainOfAggregateWithNewResponseStructure.
@Test
public void testExplainOfAggregateWithNewResponseStructure() {
// Aggregate explain is supported on earlier versions, but the structure of the response on which we're asserting in this test
// changed radically in 4.2.
assumeTrue(serverVersionAtLeast(4, 2));
MongoCollection<BsonDocument> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("explainTest", BsonDocument.class);
collection.drop();
collection.insertOne(new BsonDocument("_id", new BsonInt32(1)));
AggregateIterable<BsonDocument> iterable = collection.aggregate(singletonList(Aggregates.match(Filters.eq("_id", 1))));
Document explainDocument = iterable.explain();
assertNotNull(explainDocument);
assertTrue(explainDocument.containsKey("queryPlanner"));
assertTrue(explainDocument.containsKey("executionStats"));
explainDocument = iterable.explain(ExplainVerbosity.QUERY_PLANNER);
assertNotNull(explainDocument);
assertTrue(explainDocument.containsKey("queryPlanner"));
assertFalse(explainDocument.containsKey("executionStats"));
BsonDocument explainBsonDocument = iterable.explain(BsonDocument.class);
assertNotNull(explainBsonDocument);
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
assertTrue(explainBsonDocument.containsKey("executionStats"));
explainBsonDocument = iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER);
assertNotNull(explainBsonDocument);
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
assertFalse(explainBsonDocument.containsKey("executionStats"));
}
Aggregations