Search in sources :

Example 36 with MongoMappingContext

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

the class ProjectionOperationUnitTests method mappedFieldReferenceAsArray.

// DATAMONGO-2312
@Test
void mappedFieldReferenceAsArray() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    org.bson.Document doc = Aggregation.newAggregation(BookWithFieldAnnotation.class, project("title", "author").asArray("myArray")).toDocument("coll", new TypeBasedAggregationOperationContext(BookWithFieldAnnotation.class, mappingContext, new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext))));
    assertThat(doc).isEqualTo(Document.parse("{\"aggregate\":\"coll\", \"pipeline\":[ { $project: { myArray: [ \"$ti_t_le\", \"$author\" ] } } ] }"));
}
Also used : Document(org.bson.Document) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Test(org.junit.jupiter.api.Test)

Example 37 with MongoMappingContext

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

the class MappingMongoConverterTests method setUp.

@BeforeEach
void setUp() {
    MongoDatabase database = client.getDatabase(DATABASE);
    database.getCollection("samples").deleteMany(new Document());
    database.getCollection("java-time-types").deleteMany(new Document());
    dbRefResolver = spy(new DefaultDbRefResolver(factory));
    mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
    mappingContext.setInitialEntitySet(new HashSet<>(Arrays.asList(WithLazyDBRefAsConstructorArg.class, WithLazyDBRef.class, WithJavaTimeTypes.class)));
    mappingContext.setAutoIndexCreation(false);
    mappingContext.afterPropertiesSet();
    converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    converter.afterPropertiesSet();
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 38 with MongoMappingContext

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

the class DbRefMappingMongoConverterUnitTests method setUp.

@BeforeEach
void setUp() {
    when(dbFactory.getExceptionTranslator()).thenReturn(new MongoExceptionTranslator());
    this.dbRefResolver = spy(new DefaultDbRefResolver(dbFactory));
    this.mappingContext = new MongoMappingContext();
    this.mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
    this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoExceptionTranslator(org.springframework.data.mongodb.core.MongoExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 39 with MongoMappingContext

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

the class MongoPersistentEntityIndexResolverUnitTests method prepareMappingContextAndResolveIndexForType.

private static List<IndexDefinitionHolder> prepareMappingContextAndResolveIndexForType(Class<?>... types) {
    MongoMappingContext mappingContext = prepareMappingContext(types);
    MongoPersistentEntityIndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
    return resolver.resolveIndexForEntity(mappingContext.getRequiredPersistentEntity(types[0]));
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext)

Example 40 with MongoMappingContext

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

the class ReactiveMongoPersistentEntityIndexCreatorUnitTests method prepareMappingContext.

private static MongoMappingContext prepareMappingContext(Class<?> type) {
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setInitialEntitySet(Collections.singleton(type));
    mappingContext.initialize();
    return mappingContext;
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext)

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