|
15 | 15 | */
|
16 | 16 | package org.springframework.data.keyvalue.repository.query;
|
17 | 17 |
|
| 18 | +import static org.hamcrest.core.Is.*; |
18 | 19 | import static org.hamcrest.core.IsNot.*;
|
| 20 | +import static org.hamcrest.core.IsNull.*; |
19 | 21 | import static org.hamcrest.core.IsSame.*;
|
20 | 22 | import static org.junit.Assert.*;
|
21 | 23 | import static org.mockito.Matchers.*;
|
|
24 | 26 | import java.lang.reflect.Method;
|
25 | 27 | import java.util.List;
|
26 | 28 |
|
| 29 | +import org.hamcrest.core.IsInstanceOf; |
27 | 30 | import org.junit.Test;
|
28 | 31 | import org.junit.runner.RunWith;
|
29 | 32 | import org.mockito.Mock;
|
30 | 33 | import org.mockito.runners.MockitoJUnitRunner;
|
| 34 | +import org.springframework.data.domain.PageRequest; |
| 35 | +import org.springframework.data.domain.Pageable; |
31 | 36 | import org.springframework.data.keyvalue.Person;
|
32 | 37 | import org.springframework.data.keyvalue.core.KeyValueOperations;
|
| 38 | +import org.springframework.data.keyvalue.core.SpelCriteria; |
| 39 | +import org.springframework.data.keyvalue.core.query.KeyValueQuery; |
33 | 40 | import org.springframework.data.projection.ProjectionFactory;
|
34 | 41 | import org.springframework.data.repository.core.RepositoryMetadata;
|
35 | 42 | import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
|
@@ -69,8 +76,81 @@ public void spelExpressionAndContextShouldNotBeReused() throws NoSuchMethodExcep
|
69 | 76 | assertThat(first, not(sameInstance(second)));
|
70 | 77 | }
|
71 | 78 |
|
| 79 | + /** |
| 80 | + * @see DATAKV-142 |
| 81 | + */ |
| 82 | + @Test |
| 83 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 84 | + public void shouldApplyPageableParameterToCollectionQuery() throws SecurityException, NoSuchMethodException { |
| 85 | + |
| 86 | + when(metadataMock.getDomainType()).thenReturn((Class) Person.class); |
| 87 | + when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class); |
| 88 | + |
| 89 | + QueryMethod qm = new QueryMethod(Repo.class.getMethod("findBy", Pageable.class), metadataMock, |
| 90 | + projectionFactoryMock); |
| 91 | + |
| 92 | + KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, |
| 93 | + kvOpsMock, SpelQueryCreator.class); |
| 94 | + |
| 95 | + KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { new PageRequest(2, 3) }); |
| 96 | + |
| 97 | + assertThat(query.getOffset(), is(6)); |
| 98 | + assertThat(query.getRows(), is(3)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @see DATAKV-142 |
| 103 | + */ |
| 104 | + @Test |
| 105 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 106 | + public void shouldApplyDerivedMaxResultsToQuery() throws SecurityException, NoSuchMethodException { |
| 107 | + |
| 108 | + when(metadataMock.getDomainType()).thenReturn((Class) Person.class); |
| 109 | + when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class); |
| 110 | + |
| 111 | + QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3By"), metadataMock, projectionFactoryMock); |
| 112 | + |
| 113 | + KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, |
| 114 | + kvOpsMock, SpelQueryCreator.class); |
| 115 | + |
| 116 | + KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] {}); |
| 117 | + |
| 118 | + assertThat(query.getRows(), is(3)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @see DATAKV-142 |
| 123 | + */ |
| 124 | + @Test |
| 125 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 126 | + public void shouldApplyDerivedMaxResultsToQueryWithParameters() throws SecurityException, NoSuchMethodException { |
| 127 | + |
| 128 | + when(metadataMock.getDomainType()).thenReturn((Class) Person.class); |
| 129 | + when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class); |
| 130 | + |
| 131 | + QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3ByFirstname", String.class), metadataMock, |
| 132 | + projectionFactoryMock); |
| 133 | + |
| 134 | + KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, |
| 135 | + kvOpsMock, SpelQueryCreator.class); |
| 136 | + |
| 137 | + KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" }); |
| 138 | + |
| 139 | + assertThat(query.getCritieria(), is(notNullValue())); |
| 140 | + assertThat(query.getCritieria(), IsInstanceOf.instanceOf(SpelCriteria.class)); |
| 141 | + assertThat(((SpelCriteria) query.getCritieria()).getExpression().getExpressionString(), |
| 142 | + is("#it?.firstname?.equals([0])")); |
| 143 | + assertThat(query.getRows(), is(3)); |
| 144 | + } |
| 145 | + |
72 | 146 | static interface Repo {
|
73 | 147 |
|
74 | 148 | List<Person> findByFirstname(String firstname);
|
| 149 | + |
| 150 | + List<Person> findBy(Pageable page); |
| 151 | + |
| 152 | + List<Person> findTop3By(); |
| 153 | + |
| 154 | + List<Person> findTop3ByFirstname(String firstname); |
75 | 155 | }
|
76 | 156 | }
|
0 commit comments