Search in sources :

Example 11 with DefaultDbRefResolver

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

the class AbstractMongoConfiguration method mappingMongoConverter.

/**
 * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and
 * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied.
 *
 * @see #customConversions()
 * @see #mongoMappingContext()
 * @see #mongoDbFactory()
 * @return
 * @throws Exception
 */
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext());
    converter.setCustomConversions(customConversions());
    return converter;
}
Also used : 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) Bean(org.springframework.context.annotation.Bean)

Example 12 with DefaultDbRefResolver

use of org.springframework.data.mongodb.core.convert.DefaultDbRefResolver 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 13 with DefaultDbRefResolver

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

the class SessionBoundMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(client.getDatabase(anyString())).thenReturn(database);
    when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
    when(database.getCodecRegistry()).thenReturn(codecRegistry);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(mongoIterable);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findIterable);
    when(collection.aggregate(any(ClientSession.class), anyList(), any())).thenReturn(aggregateIterable);
    when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctIterable);
    when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReduceIterable);
    when(findIterable.iterator()).thenReturn(cursor);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.useCursor(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(mongoIterable.iterator()).thenReturn(cursor);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.map(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(findIterable.projection(any())).thenReturn(findIterable);
    factory = new SimpleMongoDbFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
    this.template = new SessionBoundMongoTemplate(clientSession, new MongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.session.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Before(org.junit.Before)

Example 14 with DefaultDbRefResolver

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

the class MongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(findIterable.iterator()).thenReturn(cursor);
    when(factory.getDb()).thenReturn(db);
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(db.getCollection(Mockito.any(String.class), eq(Document.class))).thenReturn(collection);
    when(db.runCommand(Mockito.any(), Mockito.any(Class.class))).thenReturn(commandResultDocument);
    when(collection.find(Mockito.any(org.bson.Document.class), any(Class.class))).thenReturn(findIterable);
    when(collection.mapReduce(Mockito.any(), Mockito.any(), eq(Document.class))).thenReturn(mapReduceIterable);
    when(collection.count(any(Bson.class), any(CountOptions.class))).thenReturn(1L);
    when(collection.aggregate(any(List.class), any())).thenReturn(aggregateIterable);
    when(collection.withReadPreference(any())).thenReturn(collection);
    when(findIterable.projection(Mockito.any())).thenReturn(findIterable);
    when(findIterable.sort(Mockito.any(org.bson.Document.class))).thenReturn(findIterable);
    when(findIterable.modifiers(Mockito.any(org.bson.Document.class))).thenReturn(findIterable);
    when(findIterable.collation(Mockito.any())).thenReturn(findIterable);
    when(findIterable.limit(anyInt())).thenReturn(findIterable);
    when(mapReduceIterable.collation(Mockito.any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.sort(Mockito.any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(mapReduceIterable.filter(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.into(any())).thenReturn(Collections.emptyList());
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
    this.template = new MongoTemplate(factory, converter);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) CountOptions(com.mongodb.client.model.CountOptions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) List(java.util.List) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Before(org.junit.Before)

Example 15 with DefaultDbRefResolver

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

the class SpelExpressionTransformerIntegrationTests method setUp.

@Before
public void setUp() {
    this.transformer = new SpelExpressionTransformer();
    this.dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
}
Also used : DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) Before(org.junit.Before)

Aggregations

DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)15 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)14 Before (org.junit.Before)10 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)10 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)8 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)3 Bean (org.springframework.context.annotation.Bean)2 MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)2 MongoClient (com.mongodb.MongoClient)1 CountOptions (com.mongodb.client.model.CountOptions)1 ClientSession (com.mongodb.session.ClientSession)1 List (java.util.List)1 Document (org.bson.Document)1 BsonValueCodec (org.bson.codecs.BsonValueCodec)1 Bson (org.bson.conversions.Bson)1 ObjectId (org.bson.types.ObjectId)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 CustomConversions (org.springframework.data.convert.CustomConversions)1 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)1