DATAMONGO-2351 - Return zero deleted count for unacknowledged deleteBy.
Original Pull Request: #781
This commit is contained in:
committed by
Christoph Strobl
parent
4be53ac952
commit
db9428cebe
@@ -228,6 +228,9 @@ interface MongoQueryExecution {
|
||||
/**
|
||||
* {@link MongoQueryExecution} removing documents matching the query.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Artyom Gabeev
|
||||
* @since 1.5
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@@ -251,7 +254,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,7 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository.query;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@@ -46,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;
|
||||
@@ -55,11 +57,14 @@ import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
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)
|
||||
@@ -167,6 +172,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.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
@@ -39,20 +40,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 {
|
||||
@@ -98,6 +104,28 @@ public class ReactiveMongoQueryExecutionUnitTests {
|
||||
assertThat(nearQuery.getMaxDistance(), is(nullValue()));
|
||||
}
|
||||
|
||||
@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