Search in sources :

Example 66 with MongoMappingContext

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

the class MongoConfigurationSupport method mongoMappingContext.

/**
 * Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package.
 *
 * @see #getMappingBasePackages()
 * @return
 * @throws ClassNotFoundException
 */
@Bean
public MongoMappingContext mongoMappingContext(MongoCustomConversions customConversions) throws ClassNotFoundException {
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setInitialEntitySet(getInitialEntitySet());
    mappingContext.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
    mappingContext.setFieldNamingStrategy(fieldNamingStrategy());
    mappingContext.setAutoIndexCreation(autoIndexCreation());
    return mappingContext;
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Bean(org.springframework.context.annotation.Bean)

Example 67 with MongoMappingContext

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

the class SessionBoundMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(client.getDatabase(anyString())).thenReturn(database);
    when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
    when(database.getCodecRegistry()).thenReturn(codecRegistry);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(mongoIterable);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findIterable);
    when(collection.aggregate(any(ClientSession.class), anyList(), any())).thenReturn(aggregateIterable);
    when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctIterable);
    when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReduceIterable);
    when(findIterable.iterator()).thenReturn(cursor);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(mongoIterable.iterator()).thenReturn(cursor);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.map(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(findIterable.projection(any())).thenReturn(findIterable);
    factory = new SimpleMongoClientDatabaseFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
    this.template = new SessionBoundMongoTemplate(clientSession, new MongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.client.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Before(org.junit.Before)

Example 68 with MongoMappingContext

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

the class ReactiveMongoTemplateUnitTests method beforeEach.

@BeforeEach
void beforeEach() {
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(factory.getCodecRegistry()).thenReturn(MongoClientSettings.getDefaultCodecRegistry());
    when(factory.getMongoDatabase()).thenReturn(Mono.just(db));
    when(db.getCollection(any())).thenReturn(collection);
    when(db.getCollection(any(), any())).thenReturn(collection);
    when(db.runCommand(any(), any(Class.class))).thenReturn(runCommandPublisher);
    when(db.createCollection(any(), any(CreateCollectionOptions.class))).thenReturn(runCommandPublisher);
    when(collection.withReadPreference(any())).thenReturn(collection);
    when(collection.find(any(Class.class))).thenReturn(findPublisher);
    when(collection.find(any(Document.class), any(Class.class))).thenReturn(findPublisher);
    when(collection.aggregate(anyList())).thenReturn(aggregatePublisher);
    when(collection.aggregate(anyList(), any(Class.class))).thenReturn(aggregatePublisher);
    when(collection.countDocuments(any(), any(CountOptions.class))).thenReturn(Mono.just(0L));
    when(collection.estimatedDocumentCount(any())).thenReturn(Mono.just(0L));
    when(collection.updateOne(any(), any(Bson.class), any(UpdateOptions.class))).thenReturn(updateResultPublisher);
    when(collection.updateMany(any(Bson.class), any(Bson.class), any())).thenReturn(updateResultPublisher);
    when(collection.updateOne(any(), anyList(), any())).thenReturn(updateResultPublisher);
    when(collection.updateMany(any(), anyList(), any())).thenReturn(updateResultPublisher);
    when(collection.findOneAndUpdate(any(), any(Bson.class), any(FindOneAndUpdateOptions.class))).thenReturn(findAndUpdatePublisher);
    when(collection.findOneAndReplace(any(Bson.class), any(), any())).thenReturn(findPublisher);
    when(collection.findOneAndDelete(any(), any(FindOneAndDeleteOptions.class))).thenReturn(findPublisher);
    when(collection.distinct(anyString(), any(Document.class), any())).thenReturn(distinctPublisher);
    when(collection.deleteMany(any(Bson.class), any())).thenReturn(deletePublisher);
    when(collection.findOneAndUpdate(any(), any(Bson.class), any(FindOneAndUpdateOptions.class))).thenReturn(findAndUpdatePublisher);
    when(collection.mapReduce(anyString(), anyString(), any())).thenReturn(mapReducePublisher);
    when(collection.replaceOne(any(Bson.class), any(), any(ReplaceOptions.class))).thenReturn(updateResultPublisher);
    when(collection.insertOne(any(Bson.class))).thenReturn(successPublisher);
    when(collection.insertMany(anyList())).thenReturn(successPublisher);
    when(findPublisher.projection(any())).thenReturn(findPublisher);
    when(findPublisher.limit(anyInt())).thenReturn(findPublisher);
    when(findPublisher.collation(any())).thenReturn(findPublisher);
    when(findPublisher.first()).thenReturn(findPublisher);
    when(findPublisher.allowDiskUse(anyBoolean())).thenReturn(findPublisher);
    when(aggregatePublisher.allowDiskUse(anyBoolean())).thenReturn(aggregatePublisher);
    when(aggregatePublisher.collation(any())).thenReturn(aggregatePublisher);
    when(aggregatePublisher.maxTime(anyLong(), any())).thenReturn(aggregatePublisher);
    when(aggregatePublisher.first()).thenReturn(findPublisher);
    this.mappingContext = new MongoMappingContext();
    this.mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
    this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
    this.template = new ReactiveMongoTemplate(factory, converter);
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) Bson(org.bson.conversions.Bson) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 69 with MongoMappingContext

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

the class AddFieldsOperationUnitTests method contextFor.

private static AggregationOperationContext contextFor(@Nullable Class<?> type) {
    if (type == null) {
        return Aggregation.DEFAULT_CONTEXT;
    }
    MappingMongoConverter mongoConverter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext());
    mongoConverter.afterPropertiesSet();
    return new TypeBasedAggregationOperationContext(type, mongoConverter.getMappingContext(), new QueryMapper(mongoConverter)).continueOnMissingFieldReference();
}
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)

Example 70 with MongoMappingContext

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

the class MongoTemplateUnitTests method registersDefaultEntityIndexCreatorIfApplicationContextHasOneForDifferentMappingContext.

// DATAMONGO-533
@Test
void registersDefaultEntityIndexCreatorIfApplicationContextHasOneForDifferentMappingContext() {
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.getBeanFactory().registerSingleton("foo", new MongoPersistentEntityIndexCreator(new MongoMappingContext(), template));
    applicationContext.refresh();
    GenericApplicationContext spy = spy(applicationContext);
    MongoTemplate mongoTemplate = new MongoTemplate(factory, converter);
    mongoTemplate.setApplicationContext(spy);
    verify(spy, times(1)).addApplicationListener(argThat(new ArgumentMatcher<MongoPersistentEntityIndexCreator>() {

        @Override
        public boolean matches(MongoPersistentEntityIndexCreator argument) {
            return argument.isIndexCreatorFor(mappingContext);
        }
    }));
}
Also used : MongoPersistentEntityIndexCreator(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) ArgumentMatcher(org.mockito.ArgumentMatcher) Test(org.junit.jupiter.api.Test)

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