Search in sources :

Example 1 with BulkOperationContext

use of org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext in project spring-data-mongodb by spring-projects.

the class MongoTemplate method bulkOps.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.ExecutableInsertOperation#bulkOps(org.springframework.data.mongodb.core.BulkMode, java.lang.Class, java.lang.String)
	 */
public BulkOperations bulkOps(BulkMode mode, @Nullable Class<?> entityType, String collectionName) {
    Assert.notNull(mode, "BulkMode must not be null!");
    Assert.hasText(collectionName, "Collection name must not be null or empty!");
    DefaultBulkOperations operations = new DefaultBulkOperations(this, collectionName, new BulkOperationContext(mode, Optional.ofNullable(getPersistentEntity(entityType)), queryMapper, updateMapper, eventPublisher, entityCallbacks));
    operations.setDefaultWriteConcern(writeConcern);
    return operations;
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext)

Example 2 with BulkOperationContext

use of org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext 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 3 with BulkOperationContext

use of org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext 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 4 with BulkOperationContext

use of org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext 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 5 with BulkOperationContext

use of org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsIntegrationTests method createBulkOps.

private BulkOperations createBulkOps(BulkMode mode, Class<?> entityType) {
    Optional<? extends MongoPersistentEntity<?>> entity = entityType != null ? Optional.of(operations.getConverter().getMappingContext().getPersistentEntity(entityType)) : Optional.empty();
    BulkOperationContext bulkOperationContext = new BulkOperationContext(mode, entity, new QueryMapper(operations.getConverter()), new UpdateMapper(operations.getConverter()), null, null);
    DefaultBulkOperations bulkOps = new DefaultBulkOperations(operations, COLLECTION_NAME, bulkOperationContext);
    bulkOps.setDefaultWriteConcern(WriteConcern.ACKNOWLEDGED);
    return bulkOps;
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper)

Aggregations

BulkOperationContext (org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext)8 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)7 UpdateMapper (org.springframework.data.mongodb.core.convert.UpdateMapper)7 Test (org.junit.jupiter.api.Test)5 BsonDocument (org.bson.BsonDocument)4 Document (org.bson.Document)3 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)3 BeforeConvertEvent (org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent)3 BeforeSaveEvent (org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)3 AfterSaveEvent (org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent)2 MongoWriteException (com.mongodb.MongoWriteException)1 WriteError (com.mongodb.WriteError)1 BulkWriteError (com.mongodb.bulk.BulkWriteError)1 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)1 InsertOneModel (com.mongodb.client.model.InsertOneModel)1 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)1 BsonString (org.bson.BsonString)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)1 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)1