@ -16,13 +16,13 @@
package org.springframework.data.mongodb.repository.query ;
package org.springframework.data.mongodb.repository.query ;
import static org.assertj.core.api.Assertions.* ;
import static org.assertj.core.api.Assertions.* ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.* ;
import static org.mockito.ArgumentMatchers.anyString ;
import static org.mockito.Mockito.* ;
import static org.mockito.Mockito.* ;
import java.lang.reflect.Method ;
import java.lang.reflect.Method ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.Optional ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
@ -68,6 +68,7 @@ import com.mongodb.client.result.DeleteResult;
* @author Mark Paluch
* @author Mark Paluch
* @author Oliver Gierke
* @author Oliver Gierke
* @author Artyom Gabeev
* @author Artyom Gabeev
* @author Christoph Strobl
* @soundtrack U Can ' t Touch This - MC Hammer
* @soundtrack U Can ' t Touch This - MC Hammer
* /
* /
@RunWith ( MockitoJUnitRunner . class )
@RunWith ( MockitoJUnitRunner . class )
@ -181,6 +182,9 @@ public class MongoQueryExecutionUnitTests {
@Test // DATAMONGO-2351
@Test // DATAMONGO-2351
public void acknowledgedDeleteReturnsDeletedCount ( ) {
public void acknowledgedDeleteReturnsDeletedCount ( ) {
Method method = ReflectionUtils . findMethod ( PersonRepository . class , "deleteAllByLastname" , String . class ) ;
MongoQueryMethod queryMethod = new MongoQueryMethod ( method , metadata , factory , context ) ;
when ( mongoOperationsMock . remove ( any ( Query . class ) , any ( Class . class ) , anyString ( ) ) )
when ( mongoOperationsMock . remove ( any ( Query . class ) , any ( Class . class ) , anyString ( ) ) )
. thenReturn ( DeleteResult . acknowledged ( 10 ) ) ;
. thenReturn ( DeleteResult . acknowledged ( 10 ) ) ;
@ -190,14 +194,48 @@ public class MongoQueryExecutionUnitTests {
@Test // DATAMONGO-2351
@Test // DATAMONGO-2351
public void unacknowledgedDeleteReturnsZeroDeletedCount ( ) {
public void unacknowledgedDeleteReturnsZeroDeletedCount ( ) {
Method method = ReflectionUtils . findMethod ( PersonRepository . class , "deleteAllByLastname" , String . class ) ;
MongoQueryMethod queryMethod = new MongoQueryMethod ( method , metadata , factory , context ) ;
when ( mongoOperationsMock . remove ( any ( Query . class ) , any ( Class . class ) , anyString ( ) ) )
when ( mongoOperationsMock . remove ( any ( Query . class ) , any ( Class . class ) , anyString ( ) ) )
. thenReturn ( DeleteResult . unacknowledged ( ) ) ;
. thenReturn ( DeleteResult . unacknowledged ( ) ) ;
assertThat ( new DeleteExecution ( mongoOperationsMock , queryMethod ) . execute ( new Query ( ) ) ) . isEqualTo ( 0L ) ;
assertThat ( new DeleteExecution ( mongoOperationsMock , queryMethod ) . execute ( new Query ( ) ) ) . isEqualTo ( 0L ) ;
}
}
@Test // DATAMONGO-1997
public void deleteExecutionWithEntityReturnTypeTriggersFindAndRemove ( ) {
Method method = ReflectionUtils . findMethod ( PersonRepository . class , "deleteByLastname" , String . class ) ;
MongoQueryMethod queryMethod = new MongoQueryMethod ( method , metadata , factory , context ) ;
Person person = new Person ( ) ;
when ( mongoOperationsMock . findAndRemove ( any ( Query . class ) , any ( Class . class ) , anyString ( ) ) ) . thenReturn ( person ) ;
assertThat ( new DeleteExecution ( mongoOperationsMock , queryMethod ) . execute ( new Query ( ) ) ) . isEqualTo ( person ) ;
}
// @Test // DATAMONGO-1997
// public void deleteExecutionWrapsEmptyResultInOptionalCorrectly() {
//
// Method method = ReflectionUtils.findMethod(PersonRepository.class, "deleteByLastname", String.class);
// MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, factory, context);
//
// Person person = new Person();
//
// when(mongoOperationsMock.findAndRemove(any(Query.class), any(Class.class), anyString())).thenReturn(null);
//
// assertThat(new DeleteExecution(mongoOperationsMock, queryMethod).execute(new Query())).isEqualTo(Optional.empty());
// }
interface PersonRepository extends Repository < Person , Long > {
interface PersonRepository extends Repository < Person , Long > {
GeoPage < Person > findByLocationNear ( Point point , Distance distance , Pageable pageable ) ;
GeoPage < Person > findByLocationNear ( Point point , Distance distance , Pageable pageable ) ;
Long deleteAllByLastname ( String lastname ) ;
Person deleteByLastname ( String lastname ) ;
Optional < Person > deletePersonByLastname ( String lastname ) ;
}
}
}
}