Search in sources :

Example 51 with MongoNamespace

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

the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarOutWithHintPlusHintString.

@DisplayName("Should build the expected AggregateOperation for $out when both hint and hint string are set")
@Test
void shouldBuildTheExpectedOperationsForDollarOutWithHintPlusHintString() {
    String collectionName = "collectionName";
    List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$out': '%s'}", collectionName)));
    MongoNamespace collectionNamespace = new MongoNamespace(NAMESPACE.getDatabaseName(), collectionName);
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor(), getBatchCursor(), null));
    AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
    AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
    publisher.hint(new Document("x", 1)).hintString("x_1");
    expectedOperation.hint(new BsonDocument("x", new BsonInt32(1)));
    Flux.from(publisher).blockFirst();
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    VoidReadOperationThenCursorReadOperation operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
    assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 52 with MongoNamespace

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

the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarMergeDocument.

@DisplayName("Should build the expected AggregateOperation for $merge document")
@Test
void shouldBuildTheExpectedOperationsForDollarMergeDocument() {
    String collectionName = "collectionName";
    List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$merge': {into: '%s'}}", collectionName)));
    MongoNamespace collectionNamespace = new MongoNamespace(NAMESPACE.getDatabaseName(), collectionName);
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor(), getBatchCursor(), null));
    AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
    AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
    // default input should be as expected
    Flux.from(publisher).blockFirst();
    VoidReadOperationThenCursorReadOperation operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
    // Should apply settings
    publisher.allowDiskUse(true).batchSize(// Used in Find
    100).bypassDocumentValidation(true).collation(COLLATION).comment("my comment").hint(BsonDocument.parse("{a: 1}")).maxAwaitTime(20, // Ignored on $out
    SECONDS).maxTime(10, SECONDS);
    expectedOperation.allowDiskUse(true).bypassDocumentValidation(true).collation(COLLATION).comment("my comment").hint(BsonDocument.parse("{a: 1}")).maxTime(10, SECONDS);
    Flux.from(publisher).blockFirst();
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
    assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
    FindOperation<Document> expectedFindOperation = new FindOperation<>(collectionNamespace, getDefaultCodecRegistry().get(Document.class)).batchSize(100).collation(COLLATION).filter(new BsonDocument()).maxAwaitTime(0, SECONDS).maxTime(0, SECONDS).retryReads(true);
    assertOperationIsTheSameAs(expectedFindOperation, operation.getCursorReadOperation());
    // Should handle database level aggregations
    publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.DATABASE);
    expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
    Flux.from(publisher).blockFirst();
    operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
    // Should handle toCollection
    publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
    expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
    // default input should be as expected
    Flux.from(publisher.toCollection()).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocument(org.bson.BsonDocument) FindOperation(com.mongodb.internal.operation.FindOperation) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 53 with MongoNamespace

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

the class ClientSideEncryptionBypassAutoEncryptionTest method setUp.

@Before
public void setUp() throws Throwable {
    assumeTrue(serverVersionAtLeast(4, 2));
    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(Fixture.getDefaultDatabaseName(), "testKeyVault");
    Fixture.dropDatabase(Fixture.getDefaultDatabaseName());
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(Fixture.getMongoClientSettings()).keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
    clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).bypassAutoEncryption(true).build();
    MongoClientSettings clientSettings = Fixture.getMongoClientSettingsBuilder().autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) HashMap(java.util.HashMap) SecureRandom(java.security.SecureRandom) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 54 with MongoNamespace

use of com.mongodb.MongoNamespace in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method beforeEach.

@BeforeEach
void beforeEach() {
    when(findIterable.iterator()).thenReturn(cursor);
    when(factory.getMongoDatabase()).thenReturn(db);
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(factory.getCodecRegistry()).thenReturn(MongoClientSettings.getDefaultCodecRegistry());
    when(db.getCollection(any(String.class), eq(Document.class))).thenReturn(collection);
    when(db.runCommand(any(), any(Class.class))).thenReturn(commandResultDocument);
    when(collection.find(any(org.bson.Document.class), any(Class.class))).thenReturn(findIterable);
    when(collection.mapReduce(any(), any(), eq(Document.class))).thenReturn(mapReduceIterable);
    when(collection.countDocuments(any(Bson.class), any(CountOptions.class))).thenReturn(1L);
    when(collection.estimatedDocumentCount(any())).thenReturn(1L);
    when(collection.getNamespace()).thenReturn(new MongoNamespace("db.mock-collection"));
    when(collection.aggregate(any(List.class), any())).thenReturn(aggregateIterable);
    when(collection.withReadPreference(any())).thenReturn(collection);
    when(collection.replaceOne(any(), any(), any(ReplaceOptions.class))).thenReturn(updateResult);
    when(collection.withWriteConcern(any())).thenReturn(collectionWithWriteConcern);
    when(collection.distinct(anyString(), any(Document.class), any())).thenReturn(distinctIterable);
    when(collectionWithWriteConcern.deleteOne(any(Bson.class), any())).thenReturn(deleteResult);
    when(findIterable.projection(any())).thenReturn(findIterable);
    when(findIterable.sort(any(org.bson.Document.class))).thenReturn(findIterable);
    when(findIterable.collation(any())).thenReturn(findIterable);
    when(findIterable.limit(anyInt())).thenReturn(findIterable);
    when(mapReduceIterable.collation(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.collectionName(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.databaseName(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.action(any())).thenReturn(mapReduceIterable);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.maxTime(anyLong(), any())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(distinctIterable.collation(any())).thenReturn(distinctIterable);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    this.mappingContext = new MongoMappingContext();
    mappingContext.setAutoIndexCreation(true);
    mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    this.converter = spy(new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext));
    converter.afterPropertiesSet();
    this.template = new MongoTemplate(factory, converter);
}
Also used : DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) MongoNamespace(com.mongodb.MongoNamespace) Bson(org.bson.conversions.Bson) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ArrayList(java.util.ArrayList) List(java.util.List) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 55 with MongoNamespace

use of com.mongodb.MongoNamespace in project morphia by mongodb.

the class TestQuery method testAlternateCollections.

@Test
public void testAlternateCollections() {
    getDs().save(new Photo(List.of("i", "am", "keywords")));
    getDs().getCollection(Photo.class).renameCollection(new MongoNamespace(getDatabase().getName(), "alternate"));
    assertEquals(getDs().find(Photo.class).count(), 0);
    assertEquals(getDs().find("alternate", Photo.class).count(), 1);
}
Also used : MongoNamespace(com.mongodb.MongoNamespace) Test(org.testng.annotations.Test)

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