Search in sources :

Example 31 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class MapReduceIterableImpl method execute.

MongoIterable<TResult> execute() {
    if (inline) {
        MapReduceWithInlineResultsOperation<TResult> operation = new MapReduceWithInlineResultsOperation<TResult>(namespace, new BsonJavaScript(mapFunction), new BsonJavaScript(reduceFunction), codecRegistry.get(resultClass)).filter(toBsonDocument(filter)).limit(limit).maxTime(maxTimeMS, MILLISECONDS).jsMode(jsMode).scope(toBsonDocument(scope)).sort(toBsonDocument(sort)).verbose(verbose).readConcern(readConcern).collation(collation);
        if (finalizeFunction != null) {
            operation.finalizeFunction(new BsonJavaScript(finalizeFunction));
        }
        return new OperationIterable<TResult>(operation, readPreference, executor);
    } else {
        MapReduceToCollectionOperation operation = createMapReduceToCollectionOperation();
        String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(dbName, collectionName), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation)).batchSize(batchSize);
        return new AwaitingWriteOperationIterable<TResult, MapReduceStatistics>(operation, executor, delegated);
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) MongoNamespace(com.mongodb.MongoNamespace) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.operation.MapReduceToCollectionOperation) BsonDocument(org.bson.BsonDocument)

Example 32 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class CollectionAcceptanceTest method shouldChangeACollectionNameWhenRenameIsCalled.

@Test
public void shouldChangeACollectionNameWhenRenameIsCalled() {
    //given
    collection.insertOne(new Document("someKey", "someValue"));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(true));
    //when
    String newCollectionName = "TheNewCollectionName";
    collection.renameCollection(new MongoNamespace(getDatabaseName(), newCollectionName));
    //then
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(false));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(newCollectionName), is(true));
    MongoCollection<Document> renamedCollection = database.getCollection(newCollectionName);
    assertThat("Renamed collection should have the same number of documents as original", renamedCollection.count(), is(1L));
}
Also used : BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.Test)

Example 33 with MongoNamespace

use of com.mongodb.MongoNamespace in project incubator-rya by apache.

the class AggregationPipelineQueryNodeTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() {
    collection = Mockito.mock(MongoCollection.class);
    Mockito.when(collection.getNamespace()).thenReturn(new MongoNamespace("db", "collection"));
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) MongoNamespace(com.mongodb.MongoNamespace) Before(org.junit.Before)

Example 34 with MongoNamespace

use of com.mongodb.MongoNamespace in project incubator-rya by apache.

the class SparqlToPipelineTransformVisitorTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() {
    collection = Mockito.mock(MongoCollection.class);
    Mockito.when(collection.getNamespace()).thenReturn(new MongoNamespace("db", "collection"));
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) MongoNamespace(com.mongodb.MongoNamespace) Before(org.junit.Before)

Example 35 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class AbstractClientSideEncryptionTest method setUp.

@Before
public void setUp() {
    assumeTrue("Client side encryption tests disabled", hasEncryptionTestsEnabled());
    assumeFalse("runOn requirements not satisfied", skipTest);
    assumeFalse("Skipping count tests", filename.startsWith("count."));
    assumeFalse(definition.getString("skipReason", new BsonString("")).getValue(), definition.containsKey("skipReason"));
    String databaseName = specDocument.getString("database_name").getValue();
    String collectionName = specDocument.getString("collection_name").getValue();
    collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), new MongoNamespace(databaseName, collectionName));
    MongoDatabase database = getMongoClient().getDatabase(databaseName);
    MongoCollection<BsonDocument> collection = database.getCollection(collectionName, BsonDocument.class);
    collection.drop();
    /* Create the collection for auto encryption. */
    if (specDocument.containsKey("json_schema")) {
        database.createCollection(collectionName, new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(new BsonDocument("$jsonSchema", specDocument.getDocument("json_schema")))));
    }
    /* Insert data into the collection */
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    if (!data.isEmpty()) {
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        database.getCollection(collectionName, BsonDocument.class).insertMany(documents);
    }
    /* Insert data into the "keyvault.datakeys" key vault. */
    BsonArray data = specDocument.getArray("key_vault_data", new BsonArray());
    collection = getMongoClient().getDatabase("keyvault").getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    collection.drop();
    if (!data.isEmpty()) {
        documents = new ArrayList<BsonDocument>();
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        collection.insertMany(documents);
    }
    commandListener = new TestCommandListener();
    BsonDocument clientOptions = definition.getDocument("clientOptions");
    BsonDocument cryptOptions = clientOptions.getDocument("autoEncryptOpts");
    BsonDocument kmsProviders = cryptOptions.getDocument("kmsProviders");
    boolean bypassAutoEncryption = cryptOptions.getBoolean("bypassAutoEncryption", BsonBoolean.FALSE).getValue();
    Map<String, BsonDocument> namespaceToSchemaMap = new HashMap<String, BsonDocument>();
    if (cryptOptions.containsKey("schemaMap")) {
        BsonDocument autoEncryptMapDocument = cryptOptions.getDocument("schemaMap");
        for (Map.Entry<String, BsonValue> entries : autoEncryptMapDocument.entrySet()) {
            final BsonDocument autoEncryptOptionsDocument = entries.getValue().asDocument();
            namespaceToSchemaMap.put(entries.getKey(), autoEncryptOptionsDocument);
        }
    }
    Map<String, Object> extraOptions = new HashMap<String, Object>();
    if (cryptOptions.containsKey("extraOptions")) {
        BsonDocument extraOptionsDocument = cryptOptions.getDocument("extraOptions");
        if (extraOptionsDocument.containsKey("mongocryptdSpawnArgs")) {
            List<String> mongocryptdSpawnArgsValue = new ArrayList<String>();
            for (BsonValue cur : extraOptionsDocument.getArray("mongocryptdSpawnArgs")) {
                mongocryptdSpawnArgsValue.add(cur.asString().getValue());
            }
            extraOptions.put("mongocryptdSpawnArgs", mongocryptdSpawnArgsValue);
        }
        if (extraOptionsDocument.containsKey("mongocryptdBypassSpawn")) {
            extraOptions.put("mongocryptdBypassSpawn", extraOptionsDocument.getBoolean("mongocryptdBypassSpawn").getValue());
        }
        if (extraOptionsDocument.containsKey("mongocryptdURI")) {
            extraOptions.put("mongocryptdURI", extraOptionsDocument.getString("mongocryptdURI").getValue());
        }
    }
    Map<String, Map<String, Object>> kmsProvidersMap = new HashMap<>();
    for (String kmsProviderKey : kmsProviders.keySet()) {
        BsonDocument kmsProviderOptions = kmsProviders.get(kmsProviderKey).asDocument();
        Map<String, Object> kmsProviderMap = new HashMap<>();
        kmsProvidersMap.put(kmsProviderKey.startsWith("aws") ? "aws" : kmsProviderKey, kmsProviderMap);
        switch(kmsProviderKey) {
            case "aws":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
                break;
            case "awsTemporary":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.tmpAwsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.tmpAwsSecretAccessKey"));
                kmsProviderMap.put("sessionToken", System.getProperty("org.mongodb.test.tmpAwsSessionToken"));
                break;
            case "awsTemporaryNoSessionToken":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.tmpAwsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.tmpAwsSecretAccessKey"));
                break;
            case "azure":
                kmsProviderMap.put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
                kmsProviderMap.put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
                kmsProviderMap.put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
                break;
            case "gcp":
                kmsProviderMap.put("email", System.getProperty("org.mongodb.test.gcpEmail"));
                kmsProviderMap.put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
                break;
            case "kmip":
                kmsProviderMap.put("endpoint", System.getProperty("org.mongodb.test.kmipEndpoint", "localhost:5698"));
                break;
            case "local":
                kmsProviderMap.put("key", kmsProviderOptions.getBinary("key").getData());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported KMS provider: " + kmsProviderKey);
        }
    }
    String keyVaultNamespace = "keyvault.datakeys";
    if (cryptOptions.containsKey("keyVaultNamespace")) {
        keyVaultNamespace = cryptOptions.getString("keyVaultNamespace").getValue();
    }
    createMongoClient(AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProvidersMap).schemaMap(namespaceToSchemaMap).bypassAutoEncryption(bypassAutoEncryption).extraOptions(extraOptions).build(), commandListener);
    database = getDatabase(databaseName);
    helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection("default", BsonDocument.class));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) ValidationOptions(com.mongodb.client.model.ValidationOptions) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

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