Search in sources :

Example 71 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext 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 72 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class AbstractMongoConfigurationUnitTests method lifecycleCallbacksAreInvokedInAppropriateOrder.

// DATAMONGO-717
@Test
public void lifecycleCallbacksAreInvokedInAppropriateOrder() {
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(SampleMongoConfiguration.class);
    MongoMappingContext mappingContext = context.getBean(MongoMappingContext.class);
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(Entity.class);
    EvaluationContextProvider provider = (EvaluationContextProvider) ReflectionTestUtils.getField(entity, "evaluationContextProvider");
    assertThat(provider).isInstanceOf(ExtensionAwareEvaluationContextProvider.class);
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) EvaluationContextProvider(org.springframework.data.spel.EvaluationContextProvider) ExtensionAwareEvaluationContextProvider(org.springframework.data.spel.ExtensionAwareEvaluationContextProvider) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

Example 73 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class AbstractReactiveMongoConfigurationUnitTests method returnsUninitializedMappingContext.

// DATAMONGO-1444
@Test
public void returnsUninitializedMappingContext() throws Exception {
    SampleMongoConfiguration configuration = new SampleMongoConfiguration();
    MongoMappingContext context = configuration.mongoMappingContext(configuration.customConversions());
    assertThat(context.getPersistentEntities()).isEmpty();
    context.initialize();
    assertThat(context.getPersistentEntities()).isNotEmpty();
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

Example 74 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class CountQueryUnitTests method setUp.

@BeforeEach
void setUp() {
    this.context = new MongoMappingContext();
    this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, context);
    this.converter.afterPropertiesSet();
    this.mapper = new QueryMapper(converter);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 75 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method setUp.

@BeforeEach
void setUp() {
    when(factory.getMongoDatabase()).thenReturn(database);
    when(factory.getExceptionTranslator()).thenReturn(new NullExceptionTranslator());
    when(database.getCollection(anyString(), eq(Document.class))).thenReturn(collection);
    mappingContext = new MongoMappingContext();
    mappingContext.afterPropertiesSet();
    converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    template = new MongoTemplate(factory, converter);
    ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(SomeDomainType.class)), new QueryMapper(converter), new UpdateMapper(converter), null, null));
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)118 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)56 Test (org.junit.jupiter.api.Test)41 BeforeEach (org.junit.jupiter.api.BeforeEach)31 Document (org.bson.Document)19 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)19 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)13 MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)10 Bson (org.bson.conversions.Bson)8 Before (org.junit.Before)8 Test (org.junit.Test)8 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)8 IndexOptions (com.mongodb.client.model.IndexOptions)7 CustomConversions (org.springframework.data.convert.CustomConversions)6 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)6 Document (org.springframework.data.mongodb.core.mapping.Document)6 Set (java.util.Set)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5 CountOptions (com.mongodb.client.model.CountOptions)4 List (java.util.List)4