Search in sources :

Example 61 with MongoMappingContext

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

the class MongoDbStoringMessageHandlerTests method validateMessageHandlingWithMongoTemplate.

@Test
@MongoDbAvailable
public void validateMessageHandlingWithMongoTemplate() throws Exception {
    MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
    MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
    converter.afterPropertiesSet();
    converter = spy(converter);
    MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);
    MongoDbStoringMessageHandler handler = new MongoDbStoringMessageHandler(template);
    handler.setCollectionNameExpression(new LiteralExpression("foo"));
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).build();
    handler.handleMessage(message);
    MongoTemplate readingTemplate = new MongoTemplate(mongoDbFactory);
    Query query = new BasicQuery("{'name' : 'Bob'}");
    Person person = readingTemplate.findOne(query, Person.class, "foo");
    assertEquals("Bob", person.getName());
    assertEquals("PA", person.getAddress().getState());
    verify(converter, times(1)).write(Mockito.any(), Mockito.any(Bson.class));
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) LiteralExpression(org.springframework.expression.common.LiteralExpression) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Bson(org.bson.conversions.Bson) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) BeanFactory(org.springframework.beans.factory.BeanFactory) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 62 with MongoMappingContext

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

the class MongoDbStoringMessageHandlerTests method validateMessageHandlingWithMongoConverter.

@Test
@MongoDbAvailable
public void validateMessageHandlingWithMongoConverter() throws Exception {
    MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
    MongoDbStoringMessageHandler handler = new MongoDbStoringMessageHandler(mongoDbFactory);
    handler.setCollectionNameExpression(new LiteralExpression("foo"));
    MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
    converter.afterPropertiesSet();
    converter = spy(converter);
    handler.setMongoConverter(converter);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).build();
    handler.handleMessage(message);
    MongoTemplate template = new MongoTemplate(mongoDbFactory);
    Query query = new BasicQuery("{'name' : 'Bob'}");
    Person person = template.findOne(query, Person.class, "foo");
    assertEquals("Bob", person.getName());
    assertEquals("PA", person.getAddress().getState());
    verify(converter, times(1)).write(Mockito.any(), Mockito.any(Bson.class));
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) LiteralExpression(org.springframework.expression.common.LiteralExpression) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Bson(org.bson.conversions.Bson) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) BeanFactory(org.springframework.beans.factory.BeanFactory) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 63 with MongoMappingContext

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

the class MongoDataConfiguration method mongoMappingContext.

@Bean
@ConditionalOnMissingBean
MongoMappingContext mongoMappingContext(ApplicationContext applicationContext, MongoProperties properties, MongoCustomConversions conversions) throws ClassNotFoundException {
    PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
    MongoMappingContext context = new MongoMappingContext();
    mapper.from(properties.isAutoIndexCreation()).to(context::setAutoIndexCreation);
    context.setInitialEntitySet(new EntityScanner(applicationContext).scan(Document.class));
    Class<?> strategyClass = properties.getFieldNamingStrategy();
    if (strategyClass != null) {
        context.setFieldNamingStrategy((FieldNamingStrategy) BeanUtils.instantiateClass(strategyClass));
    }
    context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    return context;
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) PropertyMapper(org.springframework.boot.context.properties.PropertyMapper) Document(org.springframework.data.mongodb.core.mapping.Document) EntityScanner(org.springframework.boot.autoconfigure.domain.EntityScanner) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 64 with MongoMappingContext

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

the class MongoRepositoriesAutoConfigurationTests method testDefaultRepositoryConfiguration.

@Test
void testDefaultRepositoryConfiguration() {
    this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(CityRepository.class);
        assertThat(context).hasSingleBean(MongoClient.class);
        MongoMappingContext mappingContext = context.getBean(MongoMappingContext.class);
        @SuppressWarnings("unchecked") Set<? extends Class<?>> entities = (Set<? extends Class<?>>) ReflectionTestUtils.getField(mappingContext, "initialEntitySet");
        assertThat(entities).hasSize(1);
    });
}
Also used : Set(java.util.Set) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

Example 65 with MongoMappingContext

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

the class MongoDataAutoConfigurationTests method customFieldNamingStrategy.

@Test
void customFieldNamingStrategy() {
    this.contextRunner.withPropertyValues("spring.data.mongodb.field-naming-strategy:" + CamelCaseAbbreviatingFieldNamingStrategy.class.getName()).run((context) -> {
        MongoMappingContext mappingContext = context.getBean(MongoMappingContext.class);
        FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils.getField(mappingContext, "fieldNamingStrategy");
        assertThat(fieldNamingStrategy.getClass()).isEqualTo(CamelCaseAbbreviatingFieldNamingStrategy.class);
    });
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) CamelCaseAbbreviatingFieldNamingStrategy(org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy) FieldNamingStrategy(org.springframework.data.mapping.model.FieldNamingStrategy) CamelCaseAbbreviatingFieldNamingStrategy(org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy) PropertyNameFieldNamingStrategy(org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy) 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