Search in sources :

Example 56 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext 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 57 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext 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 58 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
    when(factory.getMongoDatabase()).thenReturn(db);
    when(db.getCollection(any())).thenReturn(collection);
    when(db.getCollection(any(), any())).thenReturn(collection);
    when(db.runCommand(any(), any(Class.class))).thenReturn(runCommandPublisher);
    when(collection.find(any(Class.class))).thenReturn(findPublisher);
    when(collection.find(any(Document.class), any(Class.class))).thenReturn(findPublisher);
    when(findPublisher.projection(any())).thenReturn(findPublisher);
    when(findPublisher.limit(anyInt())).thenReturn(findPublisher);
    when(findPublisher.collation(any())).thenReturn(findPublisher);
    when(findPublisher.first()).thenReturn(findPublisher);
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new NoOpDbRefResolver(), mappingContext);
    this.template = new ReactiveMongoTemplate(factory, converter);
}
Also used : NoOpDbRefResolver(org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) Before(org.junit.Before)

Example 59 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method setUp.

@Before
public void setUp() {
    MongoCustomConversions conversions = new MongoCustomConversions();
    mappingContext = new MongoMappingContext();
    mappingContext.setApplicationContext(context);
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    mappingContext.getPersistentEntity(Address.class);
    converter = new MappingMongoConverter(resolver, mappingContext);
    converter.setCustomConversions(conversions);
    converter.afterPropertiesSet();
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Before(org.junit.Before)

Example 60 with MongoMappingContext

use of org.springframework.data.mongodb.core.mapping.MongoMappingContext in project spring-integration by spring-projects.

the class MongoDbMessageSourceTests method validateSuccessfulQueryWithMongoTemplate.

@SuppressWarnings("unchecked")
@Test
@MongoDbAvailable
public void validateSuccessfulQueryWithMongoTemplate() throws Exception {
    MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
    MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
    converter.afterPropertiesSet();
    converter = spy(converter);
    MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);
    Expression queryExpression = new LiteralExpression("{'address.state' : 'PA'}");
    MongoDbMessageSource messageSource = new MongoDbMessageSource(template, queryExpression);
    messageSource.setBeanFactory(mock(BeanFactory.class));
    messageSource.afterPropertiesSet();
    MongoTemplate writingTemplate = new MongoTemplate(mongoDbFactory, converter);
    writingTemplate.save(this.createPerson("Manny"), "data");
    writingTemplate.save(this.createPerson("Moe"), "data");
    writingTemplate.save(this.createPerson("Jack"), "data");
    List<Person> persons = (List<Person>) messageSource.receive().getPayload();
    assertEquals(3, persons.size());
    verify(converter, times(3)).read((Class<Person>) Mockito.any(), Mockito.any(Bson.class));
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) LiteralExpression(org.springframework.expression.common.LiteralExpression) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Bson(org.bson.conversions.Bson) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) BeanFactory(org.springframework.beans.factory.BeanFactory) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) List(java.util.List) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Aggregations

MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)118 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)56 Test (org.junit.jupiter.api.Test)41 BeforeEach (org.junit.jupiter.api.BeforeEach)31 Document (org.bson.Document)19 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)19 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)13 MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)10 Bson (org.bson.conversions.Bson)8 Before (org.junit.Before)8 Test (org.junit.Test)8 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)8 IndexOptions (com.mongodb.client.model.IndexOptions)7 CustomConversions (org.springframework.data.convert.CustomConversions)6 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)6 Document (org.springframework.data.mongodb.core.mapping.Document)6 Set (java.util.Set)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5 CountOptions (com.mongodb.client.model.CountOptions)4 List (java.util.List)4