Search in sources :

Example 86 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests method createIndexShouldUsePersistenceExceptionTranslatorForNonDataIntegrityConcerns.

// DATAMONGO-1125
@Test
void createIndexShouldUsePersistenceExceptionTranslatorForNonDataIntegrityConcerns() {
    doThrow(new MongoException(6, "HostUnreachable")).when(collection).createIndex(any(org.bson.Document.class), any(IndexOptions.class));
    MongoMappingContext mappingContext = prepareMappingContext(Person.class);
    assertThatThrownBy(() -> new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate)).isInstanceOf(DataAccessException.class);
}
Also used : MongoException(com.mongodb.MongoException) IndexOptions(com.mongodb.client.model.IndexOptions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 87 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests method indexCreationShouldNotCreateNewCollectionForNestedGeoSpatialIndexStructures.

// DATAMONGO-367
@Test
void indexCreationShouldNotCreateNewCollectionForNestedGeoSpatialIndexStructures() {
    MongoMappingContext mappingContext = prepareMappingContext(Wrapper.class);
    new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
    ArgumentCaptor<String> collectionNameCapturer = ArgumentCaptor.forClass(String.class);
    verify(db, times(1)).getCollection(collectionNameCapturer.capture(), any());
    assertThat(collectionNameCapturer.getValue()).isEqualTo("wrapper");
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

Example 88 with MongoMappingContext

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

the class UnsetOperationUnitTests method contextFor.

private static AggregationOperationContext contextFor(@Nullable Class<?> type) {
    if (type == null) {
        return Aggregation.DEFAULT_CONTEXT;
    }
    MappingMongoConverter mongoConverter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext());
    mongoConverter.afterPropertiesSet();
    return new TypeBasedAggregationOperationContext(type, mongoConverter.getMappingContext(), new QueryMapper(mongoConverter));
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper)

Example 89 with MongoMappingContext

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

the class ProjectionOperationUnitTests method typeProjectionShouldMapFieldNames.

// DATAMONGO-2200
@Test
void typeProjectionShouldMapFieldNames() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    MongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
    Document document = Aggregation.project(BookRenamed.class).toDocument(new TypeBasedAggregationOperationContext(Book.class, mappingContext, new QueryMapper(converter)));
    Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
    // 
    assertThat(projectClause).hasSize(// 
    2).containsEntry("ti_tl_e", // 
    1).containsEntry("author", 1);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Test(org.junit.jupiter.api.Test)

Example 90 with MongoMappingContext

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

the class GeoNearOperationUnitTests method typedAggregationOperationContext.

private TypeBasedAggregationOperationContext typedAggregationOperationContext(Class<?> type) {
    MongoMappingContext mappingContext = new MongoMappingContext();
    MongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
    return new TypeBasedAggregationOperationContext(type, mappingContext, new QueryMapper(converter));
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper)

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