use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.
the class MongoTemplateUnitTests method convertsUpdateConstraintsUsingConverters.
// DATAMONGO-374
@Test
public void convertsUpdateConstraintsUsingConverters() {
CustomConversions conversions = new MongoCustomConversions(Collections.singletonList(MyConverter.INSTANCE));
this.converter.setCustomConversions(conversions);
this.converter.afterPropertiesSet();
Query query = new Query();
Update update = new Update().set("foo", new AutogenerateableId());
template.updateFirst(query, update, Wrapper.class);
QueryMapper queryMapper = new QueryMapper(converter);
Document reference = queryMapper.getMappedObject(update.getUpdateObject(), Optional.empty());
verify(collection, times(1)).updateOne(Mockito.any(org.bson.Document.class), eq(reference), // .update(Mockito.any(Document.class), eq(reference), anyBoolean(),
Mockito.any(UpdateOptions.class));
// anyBoolean());
}
use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.
the class TypeBasedAggregationOperationContextUnitTests method shouldUseCustomConversionIfPresentAndConversionIsRequiredInLaterStage.
// DATAMONGO-912
@Test
public void shouldUseCustomConversionIfPresentAndConversionIsRequiredInLaterStage() {
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(projectStage, matchStage).toDocument("test", context);
org.bson.Document age = getValue(getValue(getPipelineElementFromAggregationAt(agg, 1), "$match"), "age");
assertThat(age, is(new org.bson.Document("v", 10)));
}
use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.
the class CustomConvertersUnitTests method setUp.
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
when(barToDocumentConverter.convert(any(Bar.class))).thenReturn(new Document());
when(documentToBarConverter.convert(any(Document.class))).thenReturn(new Bar());
CustomConversions conversions = new MongoCustomConversions(Arrays.asList(barToDocumentConverter, documentToBarConverter));
context = new MongoMappingContext();
context.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(Foo.class, Bar.class)));
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
context.initialize();
converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory), context);
converter.setCustomConversions(conversions);
converter.afterPropertiesSet();
}
use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method mappingConsidersCustomConvertersNotWritingTypeInformation.
// DATAMONGO-724
@Test
public void mappingConsidersCustomConvertersNotWritingTypeInformation() {
Person person = new Person();
person.firstname = "Dave";
ClassWithMapProperty entity = new ClassWithMapProperty();
entity.mapOfPersons = new HashMap<String, Person>();
entity.mapOfPersons.put("foo", person);
entity.mapOfObjects = new HashMap<String, Object>();
entity.mapOfObjects.put("foo", person);
CustomConversions conversions = new MongoCustomConversions(Arrays.asList(new Converter<Person, org.bson.Document>() {
@Override
public org.bson.Document convert(Person source) {
return //
new org.bson.Document().append("firstname", source.firstname).append("_class", Person.class.getName());
}
}, new Converter<org.bson.Document, Person>() {
@Override
public Person convert(org.bson.Document source) {
Person person = new Person();
person.firstname = source.get("firstname").toString();
person.lastname = "converter";
return person;
}
}));
MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
context.afterPropertiesSet();
MappingMongoConverter mongoConverter = new MappingMongoConverter(resolver, context);
mongoConverter.setCustomConversions(conversions);
mongoConverter.afterPropertiesSet();
org.bson.Document document = new org.bson.Document();
mongoConverter.write(entity, document);
ClassWithMapProperty result = mongoConverter.read(ClassWithMapProperty.class, document);
assertThat(result.mapOfPersons, is(notNullValue()));
Person personCandidate = result.mapOfPersons.get("foo");
assertThat(personCandidate, is(notNullValue()));
assertThat(personCandidate.firstname, is("Dave"));
assertThat(result.mapOfObjects, is(notNullValue()));
Object value = result.mapOfObjects.get("foo");
assertThat(value, is(notNullValue()));
assertThat(value, is(instanceOf(Person.class)));
assertThat(((Person) value).firstname, is("Dave"));
assertThat(((Person) value).lastname, is("converter"));
}
use of org.springframework.data.convert.CustomConversions in project spring-data-mongodb by spring-projects.
the class UpdateMapperUnitTests method setUp.
@Before
@SuppressWarnings("unchecked")
public void setUp() {
this.writingConverterSpy = Mockito.spy(new NestedEntityWriteConverter());
CustomConversions conversions = new MongoCustomConversions(Collections.singletonList(writingConverterSpy));
this.context = new MongoMappingContext();
this.context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
this.context.initialize();
this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), context);
this.converter.setCustomConversions(conversions);
this.converter.afterPropertiesSet();
this.mapper = new UpdateMapper(converter);
}
Aggregations