Search in sources :

Example 21 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests 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)

Example 22 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests method doesNotCreateIndexForEntityComingFromDifferentMappingContext.

@Test
void doesNotCreateIndexForEntityComingFromDifferentMappingContext() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    MongoMappingContext personMappingContext = prepareMappingContext(Person.class);
    MongoPersistentEntityIndexCreator creator = new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
    MongoPersistentEntity<?> entity = personMappingContext.getRequiredPersistentEntity(Person.class);
    MappingContextEvent<MongoPersistentEntity<?>, MongoPersistentProperty> event = new MappingContextEvent<MongoPersistentEntity<?>, MongoPersistentProperty>(personMappingContext, entity);
    creator.onApplicationEvent(event);
    verifyNoInteractions(collection);
}
Also used : MappingContextEvent(org.springframework.data.mapping.context.MappingContextEvent) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

Example 23 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests method indexCreationShouldNotCreateNewCollectionForNestedIndexStructures.

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

Example 24 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests method buildsIndexDefinitionUsingFieldName.

@Test
void buildsIndexDefinitionUsingFieldName() {
    MongoMappingContext mappingContext = prepareMappingContext(Person.class);
    new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
    assertThat(keysCaptor.getValue()).isNotNull().containsKey("fieldname");
    assertThat(optionsCaptor.getValue().getName()).isEqualTo("indexName");
    assertThat(optionsCaptor.getValue().isBackground()).isFalse();
    assertThat(optionsCaptor.getValue().getExpireAfter(TimeUnit.SECONDS)).isNull();
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

Example 25 with MongoMappingContext

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

the class MongoPersistentEntityIndexCreatorUnitTests method triggersBackgroundIndexingIfConfigured.

// DATAMONGO-554
@Test
void triggersBackgroundIndexingIfConfigured() {
    MongoMappingContext mappingContext = prepareMappingContext(AnotherPerson.class);
    new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
    assertThat(keysCaptor.getValue()).isNotNull().containsKey("lastname");
    assertThat(optionsCaptor.getValue().getName()).isEqualTo("lastname");
    assertThat(optionsCaptor.getValue().isBackground()).isTrue();
    assertThat(optionsCaptor.getValue().getExpireAfter(TimeUnit.SECONDS)).isNull();
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.jupiter.api.Test)

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