use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class AbstractRetryableWritesTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
MongoClientSettings.Builder builder = getMongoClientSettingsBuilder();
if (clientOptions.containsKey("retryWrites")) {
builder.retryWrites(clientOptions.getBoolean("retryWrites").getValue());
}
builder.applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(5, TimeUnit.MILLISECONDS);
}
});
mongoClient = createMongoClient(builder.build());
List<BsonDocument> documents = new ArrayList<>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collectionHelper.drop();
if (!documents.isEmpty()) {
collectionHelper.insertDocuments(documents);
}
MongoDatabase database = mongoClient.getDatabase(databaseName);
collection = database.getCollection(collectionName, BsonDocument.class);
helper = new JsonPoweredCrudTestHelper(description, database, collection);
if (definition.containsKey("failPoint")) {
collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
}
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class AbstractChangeStreamsTest method data.
@Parameterized.Parameters(name = "{0}: {1}")
public static Collection<Object[]> data() throws URISyntaxException, IOException {
List<Object[]> data = new ArrayList<Object[]>();
for (File file : JsonPoweredTestHelper.getTestFiles("/change-streams")) {
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
MongoNamespace namespace = new MongoNamespace(testDocument.getString("database_name").getValue(), testDocument.getString("collection_name").getValue());
MongoNamespace namespace2 = testDocument.containsKey("database2_name") ? new MongoNamespace(testDocument.getString("database2_name").getValue(), testDocument.getString("collection2_name").getValue()) : null;
for (BsonValue test : testDocument.getArray("tests")) {
data.add(new Object[] { file.getName(), test.asDocument().getString("description").getValue(), namespace, namespace2, test.asDocument(), skipTest(testDocument, test.asDocument()) });
}
}
return data;
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class Crypts method createCrypt.
public static Crypt createCrypt(final MongoClientImpl client, final AutoEncryptionSettings options) {
MongoClient internalClient = null;
MongoClientSettings keyVaultMongoClientSettings = options.getKeyVaultMongoClientSettings();
if (keyVaultMongoClientSettings == null || !options.isBypassAutoEncryption()) {
MongoClientSettings settings = MongoClientSettings.builder(client.getSettings()).applyToConnectionPoolSettings(builder -> builder.minSize(0)).autoEncryptionSettings(null).build();
internalClient = MongoClients.create(settings);
}
MongoClient collectionInfoRetrieverClient = internalClient;
MongoClient keyVaultClient = keyVaultMongoClientSettings == null ? internalClient : MongoClients.create(keyVaultMongoClientSettings);
return new Crypt(MongoCrypts.create(createMongoCryptOptions(options.getKmsProviders(), options.getSchemaMap())), options.isBypassAutoEncryption() ? null : new CollectionInfoRetriever(collectionInfoRetrieverClient), new CommandMarker(options.isBypassAutoEncryption(), options.getExtraOptions()), new KeyRetriever(keyVaultClient, new MongoNamespace(options.getKeyVaultNamespace())), createKeyManagementService(options.getKmsProviderSslContextMap()), options.isBypassAutoEncryption(), internalClient);
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class MongoCollectionImplTest method testRenameCollection.
@Test
public void testRenameCollection() {
MongoNamespace mongoNamespace = new MongoNamespace("db2.coll2");
RenameCollectionOptions options = new RenameCollectionOptions().dropTarget(true);
assertAll("renameCollection", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(mongoNamespace, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(clientSession, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(clientSession, mongoNamespace, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(null, mongoNamespace)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(null, mongoNamespace, options))), () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(clientSession, mongoNamespace, new RenameCollectionOptions());
assertPublisherIsTheSameAs(expected, collection.renameCollection(mongoNamespace), "Default");
}, () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(null, mongoNamespace, options);
assertPublisherIsTheSameAs(expected, collection.renameCollection(mongoNamespace, options), "With options");
}, () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(clientSession, mongoNamespace, new RenameCollectionOptions());
assertPublisherIsTheSameAs(expected, collection.renameCollection(clientSession, mongoNamespace), "With client session");
}, () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(clientSession, mongoNamespace, options);
assertPublisherIsTheSameAs(expected, collection.renameCollection(clientSession, mongoNamespace, options), "With client session & options");
});
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class ClientSideEncryptionExplicitEncryptionOnlyTour method main.
/**
* Run this main method to see the output of this quick example.
*
* @param args ignored args
*/
public static void main(final String[] args) {
// This would have to be the same master key as was used to create the encryption key
final byte[] localMasterKey = new byte[96];
new SecureRandom().nextBytes(localMasterKey);
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
{
put("local", new HashMap<String, Object>() {
{
put("key", localMasterKey);
}
});
}
};
MongoNamespace keyVaultNamespace = new MongoNamespace("encryption.testKeyVault");
MongoClientSettings clientSettings = MongoClientSettings.builder().autoEncryptionSettings(AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).bypassAutoEncryption(true).build()).build();
MongoClient mongoClient = MongoClients.create(clientSettings);
// Set up the key vault for this example
MongoCollection<Document> keyVaultCollection = mongoClient.getDatabase(keyVaultNamespace.getDatabaseName()).getCollection(keyVaultNamespace.getCollectionName());
keyVaultCollection.drop();
// Ensure that two data keys cannot share the same keyAltName.
keyVaultCollection.createIndex(Indexes.ascending("keyAltNames"), new IndexOptions().unique(true).partialFilterExpression(Filters.exists("keyAltNames")));
MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
// Clear old data
collection.drop();
// Create the ClientEncryption instance
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()).keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
BsonBinary dataKeyId = clientEncryption.createDataKey("local", new DataKeyOptions());
// Explicitly encrypt a field
BsonBinary encryptedFieldValue = clientEncryption.encrypt(new BsonString("123456789"), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(dataKeyId));
collection.insertOne(new Document("encryptedField", encryptedFieldValue));
// Automatically decrypts the encrypted field.
System.out.println(collection.find().first().toJson());
// release resources
clientEncryption.close();
mongoClient.close();
}
Aggregations