Search in sources :

Example 41 with MongoMappingContext

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

the class UpdateMapperUnitTests method mappingShouldConsiderCustomConvertersForEnumMapKeys.

// DATAMONGO-1423, DATAMONGO-2155
@Test
@SuppressWarnings("unchecked")
void mappingShouldConsiderCustomConvertersForEnumMapKeys() {
    CustomConversions conversions = new MongoCustomConversions(Arrays.asList(ClassWithEnum.AllocationToStringConverter.INSTANCE, ClassWithEnum.StringToAllocationConverter.INSTANCE));
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
    converter.setCustomConversions(conversions);
    converter.afterPropertiesSet();
    UpdateMapper mapper = new UpdateMapper(converter);
    Update update = new Update().set("enumAsMapKey", Collections.singletonMap(ClassWithEnum.Allocation.AVAILABLE, 100));
    Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(), mappingContext.getPersistentEntity(ClassWithEnum.class));
    Document $set = DocumentTestUtils.getAsDocument(mappedUpdate, "$set");
    assertThat($set.containsKey("enumAsMapKey")).isTrue();
    Map enumAsMapKey = $set.get("enumAsMapKey", Map.class);
    assertThat(enumAsMapKey.get("V")).isEqualTo(100);
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 42 with MongoMappingContext

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

the class UpdateMapperUnitTests method mapsUpdateWithBothReadingAndWritingConverterRegistered.

// DATAMONGO-1250
@Test
@SuppressWarnings("unchecked")
void mapsUpdateWithBothReadingAndWritingConverterRegistered() {
    CustomConversions conversions = new MongoCustomConversions(Arrays.asList(ClassWithEnum.AllocationToStringConverter.INSTANCE, ClassWithEnum.StringToAllocationConverter.INSTANCE));
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
    converter.setCustomConversions(conversions);
    converter.afterPropertiesSet();
    UpdateMapper mapper = new UpdateMapper(converter);
    Update update = new Update().set("allocation", ClassWithEnum.Allocation.AVAILABLE);
    Document result = mapper.getMappedObject(update.getUpdateObject(), mappingContext.getPersistentEntity(ClassWithEnum.class));
    assertThat(result).containsEntry("$set.allocation", ClassWithEnum.Allocation.AVAILABLE.code);
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 43 with MongoMappingContext

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

the class MongoDbMessageSourceTests method validateSuccessfulQueryWithCustomConverter.

@SuppressWarnings("unchecked")
@Test
@MongoDbAvailable
public void validateSuccessfulQueryWithCustomConverter() throws Exception {
    MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
    MongoTemplate template = new MongoTemplate(mongoDbFactory);
    template.save(this.createPerson("Manny"), "data");
    template.save(this.createPerson("Moe"), "data");
    template.save(this.createPerson("Jack"), "data");
    Expression queryExpression = new LiteralExpression("{'address.state' : 'PA'}");
    MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
    MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
    messageSource.setBeanFactory(mock(BeanFactory.class));
    converter.afterPropertiesSet();
    converter = spy(converter);
    messageSource.setMongoConverter(converter);
    messageSource.afterPropertiesSet();
    List<Person> persons = (List<Person>) messageSource.receive().getPayload();
    assertEquals(3, persons.size());
    verify(converter, times(3)).read((Class<Person>) Mockito.any(), Mockito.any(Bson.class));
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) LiteralExpression(org.springframework.expression.common.LiteralExpression) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Bson(org.bson.conversions.Bson) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) BeanFactory(org.springframework.beans.factory.BeanFactory) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) List(java.util.List) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 44 with MongoMappingContext

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

the class AbstractConfigurableMongoDbMessageStore method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.mongoTemplate == null) {
        if (this.mappingMongoConverter == null) {
            this.mappingMongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(this.mongoDbFactory), new MongoMappingContext());
            this.mappingMongoConverter.setApplicationContext(this.applicationContext);
            List<Object> customConverters = new ArrayList<Object>();
            customConverters.add(new MessageToBinaryConverter());
            customConverters.add(new BinaryToMessageConverter());
            this.mappingMongoConverter.setCustomConversions(new MongoCustomConversions(customConverters));
            this.mappingMongoConverter.afterPropertiesSet();
        }
        this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mappingMongoConverter);
    }
    this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.applicationContext);
    IndexOperations indexOperations = this.mongoTemplate.indexOps(this.collectionName);
    indexOperations.ensureIndex(new Index(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC));
    indexOperations.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC).on(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC).unique());
    indexOperations.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC).on(MessageDocumentFields.LAST_MODIFIED_TIME, Sort.Direction.DESC).on(MessageDocumentFields.SEQUENCE, Sort.Direction.DESC));
}
Also used : MessageToBinaryConverter(org.springframework.integration.mongodb.support.MessageToBinaryConverter) IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) ArrayList(java.util.ArrayList) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Index(org.springframework.data.mongodb.core.index.Index) BinaryToMessageConverter(org.springframework.integration.mongodb.support.BinaryToMessageConverter) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate)

Example 45 with MongoMappingContext

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

the class MongoReactiveRepositoriesAutoConfigurationTests method testDefaultRepositoryConfiguration.

@Test
void testDefaultRepositoryConfiguration() {
    this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(ReactiveCityRepository.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)

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