Search in sources :

Example 6 with CustomConversions

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());
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) CustomConversions(org.springframework.data.convert.CustomConversions) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) Update(org.springframework.data.mongodb.core.query.Update) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Test(org.junit.Test)

Example 7 with CustomConversions

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)));
}
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 8 with CustomConversions

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();
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Document(org.bson.Document) Before(org.junit.Before)

Example 9 with CustomConversions

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"));
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) Document(org.springframework.data.mongodb.core.mapping.Document) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) WritingConverter(org.springframework.data.convert.WritingConverter) Converter(org.springframework.core.convert.converter.Converter) ReadingConverter(org.springframework.data.convert.ReadingConverter) Test(org.junit.Test)

Example 10 with CustomConversions

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);
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Before(org.junit.Before)

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