Search in sources :

Example 16 with MongoNamespace

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"));
    }
}
Also used : Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) DocumentCodec(org.bson.codecs.DocumentCodec) ArrayList(java.util.ArrayList) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) ServerSettings(com.mongodb.connection.ServerSettings) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 17 with MongoNamespace

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;
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) MongoNamespace(com.mongodb.MongoNamespace) File(java.io.File) BsonValue(org.bson.BsonValue)

Example 18 with MongoNamespace

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);
}
Also used : MongoClient(com.mongodb.client.MongoClient) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace)

Example 19 with MongoNamespace

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");
    });
}
Also used : RenameCollectionOptions(com.mongodb.client.model.RenameCollectionOptions) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.jupiter.api.Test)

Example 20 with MongoNamespace

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();
}
Also used : HashMap(java.util.HashMap) IndexOptions(com.mongodb.client.model.IndexOptions) BsonBinary(org.bson.BsonBinary) ClientEncryption(com.mongodb.client.vault.ClientEncryption) SecureRandom(java.security.SecureRandom) BsonString(org.bson.BsonString) ConnectionString(com.mongodb.ConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) MongoClient(com.mongodb.client.MongoClient) ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) BsonString(org.bson.BsonString) ConnectionString(com.mongodb.ConnectionString) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)55 BsonDocument (org.bson.BsonDocument)34 BsonString (org.bson.BsonString)21 Document (org.bson.Document)20 Before (org.junit.Before)17 MongoClientSettings (com.mongodb.MongoClientSettings)15 BsonValue (org.bson.BsonValue)13 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 DocumentCodec (org.bson.codecs.DocumentCodec)8 CollectionHelper (com.mongodb.client.test.CollectionHelper)7 Test (org.junit.jupiter.api.Test)7 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 Test (org.junit.Test)6 IndexOptions (com.mongodb.client.model.IndexOptions)5 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)5 DisplayName (org.junit.jupiter.api.DisplayName)5