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());
}
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());
}
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);
}
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);
}
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);
}
Aggregations