DATAKV-142 - Consider PartTree.isLimiting() and PartTree.getMaxResults() when creating queries.

We now pick up result size limiting constraints from the query method name. This allows usage of `findTopN` and `findFirst` keywords.

Original pull request: #22.
This commit is contained in:
Christoph Strobl
2016-08-24 12:56:04 +02:00
committed by Mark Paluch
parent 734b85c485
commit 37c9aa4012
2 changed files with 93 additions and 2 deletions

View File

@@ -15,7 +15,9 @@
*/
package org.springframework.data.keyvalue.repository.query;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNot.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.core.IsSame.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
@@ -24,12 +26,17 @@ import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.List;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.keyvalue.Person;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.SpelCriteria;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
@@ -69,8 +76,81 @@ public class KeyValuePartTreeQueryUnitTests {
assertThat(first, not(sameInstance(second)));
}
/**
* @see DATAKV-142
*/
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyPageableParameterToCollectionQuery() throws SecurityException, NoSuchMethodException {
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findBy", Pageable.class), metadataMock,
projectionFactoryMock);
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE,
kvOpsMock, SpelQueryCreator.class);
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { new PageRequest(2, 3) });
assertThat(query.getOffset(), is(6));
assertThat(query.getRows(), is(3));
}
/**
* @see DATAKV-142
*/
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyDerivedMaxResultsToQuery() throws SecurityException, NoSuchMethodException {
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3By"), metadataMock, projectionFactoryMock);
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE,
kvOpsMock, SpelQueryCreator.class);
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] {});
assertThat(query.getRows(), is(3));
}
/**
* @see DATAKV-142
*/
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyDerivedMaxResultsToQueryWithParameters() throws SecurityException, NoSuchMethodException {
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3ByFirstname", String.class), metadataMock,
projectionFactoryMock);
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE,
kvOpsMock, SpelQueryCreator.class);
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" });
assertThat(query.getCritieria(), is(notNullValue()));
assertThat(query.getCritieria(), IsInstanceOf.instanceOf(SpelCriteria.class));
assertThat(((SpelCriteria) query.getCritieria()).getExpression().getExpressionString(),
is("#it?.firstname?.equals([0])"));
assertThat(query.getRows(), is(3));
}
static interface Repo {
List<Person> findByFirstname(String firstname);
List<Person> findBy(Pageable page);
List<Person> findTop3By();
List<Person> findTop3ByFirstname(String firstname);
}
}