Search in sources :

Example 1 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method bulkInsertInvokesEntityCallbacks.

// DATAMONGO-2261, DATAMONGO-2479
@Test
void bulkInsertInvokesEntityCallbacks() {
    BeforeConvertPersonCallback beforeConvertCallback = spy(new BeforeConvertPersonCallback());
    BeforeSavePersonCallback beforeSaveCallback = spy(new BeforeSavePersonCallback());
    AfterSavePersonCallback afterSaveCallback = spy(new AfterSavePersonCallback());
    ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(Person.class)), new QueryMapper(converter), new UpdateMapper(converter), null, EntityCallbacks.create(beforeConvertCallback, beforeSaveCallback, afterSaveCallback)));
    Person entity = new Person("init");
    ops.insert(entity);
    ArgumentCaptor<Person> personArgumentCaptor = ArgumentCaptor.forClass(Person.class);
    verify(beforeConvertCallback).onBeforeConvert(personArgumentCaptor.capture(), eq("collection-1"));
    verifyNoInteractions(beforeSaveCallback);
    ops.execute();
    verify(beforeSaveCallback).onBeforeSave(personArgumentCaptor.capture(), any(), eq("collection-1"));
    verify(afterSaveCallback).onAfterSave(personArgumentCaptor.capture(), any(), eq("collection-1"));
    assertThat(personArgumentCaptor.getAllValues()).extracting("firstName").containsExactly("init", "before-convert", "before-convert");
    verify(collection).bulkWrite(captor.capture(), any());
    InsertOneModel<Document> updateModel = (InsertOneModel<Document>) captor.getValue().get(0);
    assertThat(updateModel.getDocument()).containsEntry("firstName", "after-save");
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) InsertOneModel(com.mongodb.client.model.InsertOneModel) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 2 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method bulkReplaceOneEmitsEventsCorrectly.

// DATAMONGO-2290
@Test
void bulkReplaceOneEmitsEventsCorrectly() {
    ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
    ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(Person.class)), new QueryMapper(converter), new UpdateMapper(converter), eventPublisher, null));
    ops.replaceOne(query(where("firstName").is("danerys")), new SomeDomainType());
    verify(eventPublisher).publishEvent(any(BeforeConvertEvent.class));
    verify(eventPublisher, never()).publishEvent(any(BeforeSaveEvent.class));
    verify(eventPublisher, never()).publishEvent(any(AfterSaveEvent.class));
    ops.execute();
    verify(eventPublisher).publishEvent(any(BeforeSaveEvent.class));
    verify(eventPublisher).publishEvent(any(AfterSaveEvent.class));
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) AfterSaveEvent(org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) Test(org.junit.jupiter.api.Test)

Example 3 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method bulkInsertEmitsEventsCorrectly.

// DATAMONGO-2290
@Test
void bulkInsertEmitsEventsCorrectly() {
    ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
    ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(Person.class)), new QueryMapper(converter), new UpdateMapper(converter), eventPublisher, null));
    ops.insert(new SomeDomainType());
    verify(eventPublisher).publishEvent(any(BeforeConvertEvent.class));
    verify(eventPublisher, never()).publishEvent(any(BeforeSaveEvent.class));
    verify(eventPublisher, never()).publishEvent(any(AfterSaveEvent.class));
    ops.execute();
    verify(eventPublisher).publishEvent(any(BeforeSaveEvent.class));
    verify(eventPublisher).publishEvent(any(AfterSaveEvent.class));
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) AfterSaveEvent(org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) Test(org.junit.jupiter.api.Test)

Example 4 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsTests method shouldFavorExplicitMappingHintViaClass.

// DATAMONGO-1682, DATAMONGO-2198
@Test
public void shouldFavorExplicitMappingHintViaClass() {
    IndexDefinition id = new Index().named("partial-with-inheritance").on("k3y", Direction.ASC).partial(of(where("age").gte(10)));
    indexOps = new DefaultReactiveIndexOperations(template, this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class), new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    // 
    indexOps.getIndexInfo().filter(this.indexByName("partial-with-inheritance")).as(StepVerifier::create).consumeNextWith(indexInfo -> {
        assertThat(Document.parse(indexInfo.getPartialFilterExpression())).isEqualTo(Document.parse("{ \"a_g_e\" : { \"$gte\" : 10 } }"));
    }).verifyComplete();
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) ReactiveMongoTestTemplate(org.springframework.data.mongodb.test.util.ReactiveMongoTestTemplate) Collation(org.springframework.data.mongodb.core.query.Collation) Template(org.springframework.data.mongodb.test.util.Template) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) Assertions(org.assertj.core.api.Assertions) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) MongoTemplateExtension(org.springframework.data.mongodb.test.util.MongoTemplateExtension) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 5 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class AggregationUnitTests method shouldNotConvertIncludeExcludeValuesForProjectOperation.

// GH-3898
@Test
void shouldNotConvertIncludeExcludeValuesForProjectOperation() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    RelaxedTypeBasedAggregationOperationContext context = new RelaxedTypeBasedAggregationOperationContext(WithRetypedIdField.class, mappingContext, new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext)));
    Document document = project(WithRetypedIdField.class).toDocument(context);
    assertThat(document).isEqualTo(new Document("$project", new Document("_id", 1).append("renamed-field", 1)));
}
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) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)29 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)19 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)19 Test (org.junit.jupiter.api.Test)14 Document (org.bson.Document)11 BulkOperationContext (org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext)7 UpdateMapper (org.springframework.data.mongodb.core.convert.UpdateMapper)7 BsonDocument (org.bson.BsonDocument)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)3 Index (org.springframework.data.mongodb.core.index.Index)3 IndexDefinition (org.springframework.data.mongodb.core.index.IndexDefinition)3 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)3 BeforeConvertEvent (org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent)3 BeforeSaveEvent (org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)3 Test (org.junit.Test)2 MongoConverter (org.springframework.data.mongodb.core.convert.MongoConverter)2 AfterSaveEvent (org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 MongoWriteException (com.mongodb.MongoWriteException)1