DATAMONGO-2351 - Return zero deleted count for unacknowledged deleteBy.
Original Pull Request: #781
This commit is contained in:
committed by
Christoph Strobl
parent
79921a7260
commit
b78569374a
@@ -229,6 +229,9 @@ interface MongoQueryExecution {
|
||||
/**
|
||||
* {@link MongoQueryExecution} removing documents matching the query.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Artyom Gabeev
|
||||
* @since 1.5
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@@ -252,7 +255,7 @@ interface MongoQueryExecution {
|
||||
}
|
||||
|
||||
DeleteResult writeResult = operations.remove(query, type, collectionName);
|
||||
return writeResult != null ? writeResult.getDeletedCount() : 0L;
|
||||
return writeResult != null && writeResult.wasAcknowledged() ? writeResult.getDeletedCount() : 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +109,7 @@ interface ReactiveMongoQueryExecution {
|
||||
* {@link ReactiveMongoQueryExecution} removing documents matching the query.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Artyom Gabeev
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
final class DeleteExecution implements ReactiveMongoQueryExecution {
|
||||
@@ -127,7 +128,8 @@ interface ReactiveMongoQueryExecution {
|
||||
return operations.findAllAndRemove(query, type, collection);
|
||||
}
|
||||
|
||||
return operations.remove(query, type, collection).map(DeleteResult::getDeletedCount);
|
||||
return operations.remove(query, type, collection)
|
||||
.map(deleteResult -> deleteResult.wasAcknowledged() ? deleteResult.getDeletedCount() : 0L);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository.query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -27,7 +28,6 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.geo.Distance;
|
||||
@@ -47,6 +47,7 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.repository.Person;
|
||||
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.DeleteExecution;
|
||||
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagedExecution;
|
||||
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagingGeoNearExecution;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
@@ -58,11 +59,14 @@ import org.springframework.data.repository.query.QueryMethodEvaluationContextPro
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MongoQueryExecution}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Oliver Gierke
|
||||
* @author Artyom Gabeev
|
||||
* @soundtrack U Can't Touch This - MC Hammer
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -173,6 +177,30 @@ public class MongoQueryExecutionUnitTests {
|
||||
verify(terminatingMock).count();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2351
|
||||
public void acknowledgedDeleteReturnsDeletedCount() {
|
||||
when(mongoOperationsMock.remove(any(Query.class), any(Class.class), anyString()))
|
||||
.thenReturn(DeleteResult.acknowledged(10));
|
||||
|
||||
DeleteExecution execution = new DeleteExecution(mongoOperationsMock, queryMethod);
|
||||
Object result = execution.execute(new Query());
|
||||
|
||||
assertThat(result).isEqualTo(10L);
|
||||
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2351
|
||||
public void unacknowledgedDeleteReturnsZeroDeletedCount() {
|
||||
when(mongoOperationsMock.remove(any(Query.class), any(Class.class), anyString()))
|
||||
.thenReturn(DeleteResult.unacknowledged());
|
||||
|
||||
DeleteExecution execution = new DeleteExecution(mongoOperationsMock, queryMethod);
|
||||
Object result = execution.execute(new Query());
|
||||
|
||||
assertThat(result).isEqualTo(0L);
|
||||
|
||||
}
|
||||
|
||||
interface PersonRepository extends Repository<Person, Long> {
|
||||
|
||||
GeoPage<Person> findByLocationNear(Point point, Distance distance, Pageable pageable);
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
@@ -29,7 +30,6 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Range;
|
||||
@@ -41,20 +41,25 @@ import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.repository.Person;
|
||||
import org.springframework.data.mongodb.repository.query.ReactiveMongoQueryExecution.DeleteExecution;
|
||||
import org.springframework.data.mongodb.repository.query.ReactiveMongoQueryExecution.GeoNearExecution;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReactiveMongoQueryExecution}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Artyom Gabeev
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ReactiveMongoQueryExecutionUnitTests {
|
||||
|
||||
@Mock private ReactiveMongoOperations operations;
|
||||
@Mock private MongoParameterAccessor parameterAccessor;
|
||||
@Mock private MongoQueryMethod method;
|
||||
|
||||
@Test // DATAMONGO-1444
|
||||
public void geoNearExecutionShouldApplyQuerySettings() throws Exception {
|
||||
@@ -101,6 +106,28 @@ public class ReactiveMongoQueryExecutionUnitTests {
|
||||
assertThat(nearQuery.getMaxDistance()).isNull();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2351
|
||||
public void acknowledgedDeleteReturnsDeletedCount() {
|
||||
when(operations.remove(any(Query.class), any(Class.class), anyString()))
|
||||
.thenReturn(Mono.just(DeleteResult.acknowledged(10)));
|
||||
|
||||
DeleteExecution execution = new DeleteExecution(operations, method);
|
||||
Object result = ((Mono) execution.execute(new Query(), Class.class, "")).block();
|
||||
|
||||
assertThat(result).isEqualTo(10L);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2351
|
||||
public void unacknowledgedDeleteReturnsZeroDeletedCount() {
|
||||
when(operations.remove(any(Query.class), any(Class.class), anyString()))
|
||||
.thenReturn(Mono.just(DeleteResult.unacknowledged()));
|
||||
|
||||
DeleteExecution execution = new DeleteExecution(operations, method);
|
||||
Object result = ((Mono) execution.execute(new Query(), Class.class, "")).block();
|
||||
|
||||
assertThat(result).isEqualTo(0L);
|
||||
}
|
||||
|
||||
interface GeoRepo {
|
||||
Flux<GeoResult<Person>> geoNear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user