Search in sources :

Example 1 with CustomConversions

use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.

the class TypeBasedAggregationOperationContextUnitTests method shouldUseCustomConversionIfPresentAndConversionIsRequiredInFirstStage.

// DATAMONGO-912
@Test
public void shouldUseCustomConversionIfPresentAndConversionIsRequiredInFirstStage() {
    CustomConversions customConversions = customAgeConversions();
    converter.setCustomConversions(customConversions);
    customConversions.registerConvertersIn((GenericConversionService) converter.getConversionService());
    AggregationOperationContext context = getContext(FooPerson.class);
    MatchOperation matchStage = match(Criteria.where("age").is(new Age(10)));
    ProjectionOperation projectStage = project("age", "name");
    org.bson.Document agg = newAggregation(matchStage, projectStage).toDocument("test", context);
    org.bson.Document age = getValue(getValue(getPipelineElementFromAggregationAt(agg, 0), "$match"), "age");
    assertThat(age, is(new org.bson.Document("v", 10)));
}
Also used : Document(org.bson.Document) CustomConversions(org.springframework.data.convert.CustomConversions) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) Document(org.bson.Document) Test(org.junit.Test)

Example 2 with CustomConversions

use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.

the class UpdateMapperUnitTests method mapsUpdateWithBothReadingAndWritingConverterRegistered.

// DATAMONGO-1250
@Test
@SuppressWarnings("unchecked")
public 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.Test)

Example 3 with CustomConversions

use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.

the class UpdateMapperUnitTests method mappingShouldConsiderCustomConvertersForEnumMapKeys.

// DATAMONGO-1423
@Test
@SuppressWarnings("unchecked")
public 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();
    Document enumAsMapKey = $set.get("enumAsMapKey", Document.class);
    assertThat(enumAsMapKey.get("AVAILABLE")).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) Test(org.junit.Test)

Example 4 with CustomConversions

use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterParserIntegrationTests method scansForConverterAndSetsUpCustomConversionsAccordingly.

// DATAMONGO-301
@Test
public void scansForConverterAndSetsUpCustomConversionsAccordingly() {
    loadValidConfiguration();
    CustomConversions conversions = factory.getBean(CustomConversions.class);
    assertThat(conversions.hasCustomWriteTarget(Person.class), is(true));
    assertThat(conversions.hasCustomWriteTarget(Account.class), is(true));
}
Also used : Account(org.springframework.data.mongodb.core.mapping.Account) CustomConversions(org.springframework.data.convert.CustomConversions) Person(org.springframework.data.mongodb.repository.Person) Test(org.junit.Test)

Example 5 with CustomConversions

use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method setMongo.

@Autowired
public void setMongo(Mongo mongo) throws Exception {
    CustomConversions conversions = new MongoCustomConversions(Arrays.asList(DateToDateTimeConverter.INSTANCE, DateTimeToDateConverter.INSTANCE));
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class, PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class, PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class, PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class, PersonWithIdPropertyOfTypeUUID.class)));
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.initialize();
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
    MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
    mappingConverter.setCustomConversions(conversions);
    mappingConverter.afterPropertiesSet();
    this.mappingTemplate = new MongoTemplate(factory, mappingConverter);
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) Autowired(org.springframework.beans.factory.annotation.Autowired)

Aggregations

CustomConversions (org.springframework.data.convert.CustomConversions)10 Test (org.junit.Test)7 Document (org.bson.Document)6 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)6 MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)4 Update (org.springframework.data.mongodb.core.query.Update)3 Before (org.junit.Before)2 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Converter (org.springframework.core.convert.converter.Converter)1 ReadingConverter (org.springframework.data.convert.ReadingConverter)1 WritingConverter (org.springframework.data.convert.WritingConverter)1 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)1 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)1 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)1 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)1 Account (org.springframework.data.mongodb.core.mapping.Account)1 Document (org.springframework.data.mongodb.core.mapping.Document)1 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)1