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