Search in sources :

Example 6 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions 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)

Example 7 with MongoCustomConversions

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

the class SessionBoundMongoTemplateTests method getDefaultMongoConverter.

private MongoConverter getDefaultMongoConverter(MongoDbFactory factory) {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
    MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    converter.setCustomConversions(conversions);
    converter.afterPropertiesSet();
    return converter;
}
Also used : DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)

Example 8 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions in project JavaForFun by gumartinm.

the class MongoConfiguration method customConversions.

// IT DOES NOT WORK!!!
// I THINK THERE IS A BUG BECAUSE MappingMongoConverter.this.conversionService never includes my custom converters!!!!
@Bean
@Override
public CustomConversions customConversions() {
    List<Converter<?, ?>> converterList = new ArrayList<>();
    converterList.add(new OffsetDateTimeToDateConverter());
    converterList.add(new DateToOffsetDateTimeConverter());
    return new MongoCustomConversions(converterList);
}
Also used : DateToOffsetDateTimeConverter(de.spring.example.persistence.converters.DateToOffsetDateTimeConverter) OffsetDateTimeToDateConverter(de.spring.example.persistence.converters.OffsetDateTimeToDateConverter) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) ArrayList(java.util.ArrayList) OffsetDateTimeToDateConverter(de.spring.example.persistence.converters.OffsetDateTimeToDateConverter) Converter(org.springframework.core.convert.converter.Converter) DateToOffsetDateTimeConverter(de.spring.example.persistence.converters.DateToOffsetDateTimeConverter) Bean(org.springframework.context.annotation.Bean)

Example 9 with MongoCustomConversions

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

the class MongoTemplateUnitTests method beforeEach.

@BeforeEach
void beforeEach() {
    when(findIterable.iterator()).thenReturn(cursor);
    when(factory.getMongoDatabase()).thenReturn(db);
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(factory.getCodecRegistry()).thenReturn(MongoClientSettings.getDefaultCodecRegistry());
    when(db.getCollection(any(String.class), eq(Document.class))).thenReturn(collection);
    when(db.runCommand(any(), any(Class.class))).thenReturn(commandResultDocument);
    when(collection.find(any(org.bson.Document.class), any(Class.class))).thenReturn(findIterable);
    when(collection.mapReduce(any(), any(), eq(Document.class))).thenReturn(mapReduceIterable);
    when(collection.countDocuments(any(Bson.class), any(CountOptions.class))).thenReturn(1L);
    when(collection.estimatedDocumentCount(any())).thenReturn(1L);
    when(collection.getNamespace()).thenReturn(new MongoNamespace("db.mock-collection"));
    when(collection.aggregate(any(List.class), any())).thenReturn(aggregateIterable);
    when(collection.withReadPreference(any())).thenReturn(collection);
    when(collection.replaceOne(any(), any(), any(ReplaceOptions.class))).thenReturn(updateResult);
    when(collection.withWriteConcern(any())).thenReturn(collectionWithWriteConcern);
    when(collection.distinct(anyString(), any(Document.class), any())).thenReturn(distinctIterable);
    when(collectionWithWriteConcern.deleteOne(any(Bson.class), any())).thenReturn(deleteResult);
    when(findIterable.projection(any())).thenReturn(findIterable);
    when(findIterable.sort(any(org.bson.Document.class))).thenReturn(findIterable);
    when(findIterable.collation(any())).thenReturn(findIterable);
    when(findIterable.limit(anyInt())).thenReturn(findIterable);
    when(mapReduceIterable.collation(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.collectionName(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.databaseName(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.action(any())).thenReturn(mapReduceIterable);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.maxTime(anyLong(), any())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(distinctIterable.collation(any())).thenReturn(distinctIterable);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    this.mappingContext = new MongoMappingContext();
    mappingContext.setAutoIndexCreation(true);
    mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    this.converter = spy(new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext));
    converter.afterPropertiesSet();
    this.template = new MongoTemplate(factory, converter);
}
Also used : DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) MongoNamespace(com.mongodb.MongoNamespace) Bson(org.bson.conversions.Bson) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ArrayList(java.util.ArrayList) List(java.util.List) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with MongoCustomConversions

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

the class MongoTemplateUnitTests method convertsUpdateConstraintsUsingConverters.

// DATAMONGO-374
@Test
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(any(org.bson.Document.class), eq(reference), any(UpdateOptions.class));
}
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) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) CustomConversions(org.springframework.data.convert.CustomConversions) 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.jupiter.api.Test)

Aggregations

MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)12 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)10 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)9 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)6 Document (org.bson.Document)4 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)4 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 CountOptions (com.mongodb.client.model.CountOptions)2 FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)2 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)2 ReplaceOptions (com.mongodb.client.model.ReplaceOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 Bson (org.bson.conversions.Bson)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 CustomConversions (org.springframework.data.convert.CustomConversions)2 MongoNamespace (com.mongodb.MongoNamespace)1 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)1 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)1 DateToOffsetDateTimeConverter (de.spring.example.persistence.converters.DateToOffsetDateTimeConverter)1