Search in sources :

Example 51 with MongoMappingContext

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

the class UpdateMapperUnitTests method mapsUpdateWithBothReadingAndWritingConverterRegistered.

// DATAMONGO-1250
@Test
@SuppressWarnings("unchecked")
void mapsUpdateWithBothReadingAndWritingConverterRegistered() {
    CustomConversions conversions = new MongoCustomConversions(Arrays.asList(ClassWithEnum.AllocationToStringConverter.INSTANCE, ClassWithEnum.StringToAllocationConverter.INSTANCE));
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
    converter.setCustomConversions(conversions);
    converter.afterPropertiesSet();
    UpdateMapper mapper = new UpdateMapper(converter);
    Update update = new Update().set("allocation", ClassWithEnum.Allocation.AVAILABLE);
    Document result = mapper.getMappedObject(update.getUpdateObject(), mappingContext.getPersistentEntity(ClassWithEnum.class));
    assertThat(result).containsEntry("$set.allocation", ClassWithEnum.Allocation.AVAILABLE.code);
}
Also used : CustomConversions(org.springframework.data.convert.CustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 52 with MongoMappingContext

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

the class MongoDataAutoConfigurationTests method testFieldNamingStrategy.

public void testFieldNamingStrategy(String strategy, Class<? extends FieldNamingStrategy> expectedType) {
    this.context = new AnnotationConfigApplicationContext();
    if (strategy != null) {
        EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.field-naming-strategy:" + strategy);
    }
    this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
    this.context.refresh();
    MongoMappingContext mappingContext = this.context.getBean(MongoMappingContext.class);
    FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils.getField(mappingContext, "fieldNamingStrategy");
    assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) FieldNamingStrategy(org.springframework.data.mapping.model.FieldNamingStrategy) CamelCaseAbbreviatingFieldNamingStrategy(org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy) PropertyNameFieldNamingStrategy(org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy)

Example 53 with MongoMappingContext

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

the class ReactiveMongoRepositoriesAutoConfigurationTests method testDefaultRepositoryConfiguration.

@Test
public void testDefaultRepositoryConfiguration() throws Exception {
    prepareApplicationContext(TestConfiguration.class);
    assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull();
    MongoClient client = this.context.getBean(MongoClient.class);
    assertThat(client).isInstanceOf(MongoClient.class);
    MongoMappingContext mappingContext = this.context.getBean(MongoMappingContext.class);
    @SuppressWarnings("unchecked") Set<? extends Class<?>> entities = (Set<? extends Class<?>>) ReflectionTestUtils.getField(mappingContext, "initialEntitySet");
    assertThat(entities).hasSize(1);
}
Also used : MongoClient(com.mongodb.reactivestreams.client.MongoClient) Set(java.util.Set) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.Test)

Example 54 with MongoMappingContext

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

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

the class MongoConfigurationSupport method mongoMappingContext.

/**
 * Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package.
 *
 * @see #getMappingBasePackages()
 * @return
 * @throws ClassNotFoundException
 */
@Bean
public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setInitialEntitySet(getInitialEntitySet());
    mappingContext.setSimpleTypeHolder(customConversions().getSimpleTypeHolder());
    mappingContext.setFieldNamingStrategy(fieldNamingStrategy());
    return mappingContext;
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Bean(org.springframework.context.annotation.Bean)

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