|
36 | 36 | import org.springframework.data.domain.Page;
|
37 | 37 | import org.springframework.data.domain.PageRequest;
|
38 | 38 | import org.springframework.data.domain.Pageable;
|
| 39 | +import org.springframework.data.domain.Slice; |
| 40 | +import org.springframework.data.domain.Sort; |
39 | 41 | import org.springframework.data.mongodb.MongoDbFactory;
|
40 | 42 | import org.springframework.data.mongodb.core.MongoOperations;
|
41 | 43 | import org.springframework.data.mongodb.core.Person;
|
|
50 | 52 | import org.springframework.data.mongodb.repository.MongoRepository;
|
51 | 53 | import org.springframework.data.repository.core.RepositoryMetadata;
|
52 | 54 |
|
| 55 | +import com.mongodb.BasicDBObjectBuilder; |
| 56 | +import com.mongodb.DBObject; |
53 | 57 | import com.mongodb.WriteResult;
|
54 | 58 |
|
55 | 59 | /**
|
@@ -211,6 +215,73 @@ public void metadataShouldBeAddedToStringBasedQueryCorrectly() {
|
211 | 215 | assertThat(captor.getValue().getMeta().getComment(), is("comment"));
|
212 | 216 | }
|
213 | 217 |
|
| 218 | + /** |
| 219 | + * @see DATAMONGO-1057 |
| 220 | + */ |
| 221 | + @Test |
| 222 | + public void slicedExecutionShouldRetainNrOfElementsToSkip() { |
| 223 | + |
| 224 | + MongoQueryFake query = createQueryForMethod("findByLastname", String.class, Pageable.class); |
| 225 | + Pageable page1 = new PageRequest(0, 10); |
| 226 | + Pageable page2 = page1.next(); |
| 227 | + |
| 228 | + query.execute(new Object[] { "fake", page1 }); |
| 229 | + query.execute(new Object[] { "fake", page2 }); |
| 230 | + |
| 231 | + ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class); |
| 232 | + |
| 233 | + verify(this.mongoOperationsMock, times(2)) |
| 234 | + .find(captor.capture(), Matchers.eq(Person.class), Matchers.eq("persons")); |
| 235 | + |
| 236 | + assertThat(captor.getAllValues().get(0).getSkip(), is(0)); |
| 237 | + assertThat(captor.getAllValues().get(1).getSkip(), is(10)); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * @see DATAMONGO-1057 |
| 242 | + */ |
| 243 | + @Test |
| 244 | + public void slicedExecutionShouldIncrementLimitByOne() { |
| 245 | + |
| 246 | + MongoQueryFake query = createQueryForMethod("findByLastname", String.class, Pageable.class); |
| 247 | + Pageable page1 = new PageRequest(0, 10); |
| 248 | + Pageable page2 = page1.next(); |
| 249 | + |
| 250 | + query.execute(new Object[] { "fake", page1 }); |
| 251 | + query.execute(new Object[] { "fake", page2 }); |
| 252 | + |
| 253 | + ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class); |
| 254 | + |
| 255 | + verify(this.mongoOperationsMock, times(2)) |
| 256 | + .find(captor.capture(), Matchers.eq(Person.class), Matchers.eq("persons")); |
| 257 | + |
| 258 | + assertThat(captor.getAllValues().get(0).getLimit(), is(11)); |
| 259 | + assertThat(captor.getAllValues().get(1).getLimit(), is(11)); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * @see DATAMONGO-1057 |
| 264 | + */ |
| 265 | + @Test |
| 266 | + public void slicedExecutionShouldRetainSort() { |
| 267 | + |
| 268 | + MongoQueryFake query = createQueryForMethod("findByLastname", String.class, Pageable.class); |
| 269 | + Pageable page1 = new PageRequest(0, 10, Sort.Direction.DESC, "bar"); |
| 270 | + Pageable page2 = page1.next(); |
| 271 | + |
| 272 | + query.execute(new Object[] { "fake", page1 }); |
| 273 | + query.execute(new Object[] { "fake", page2 }); |
| 274 | + |
| 275 | + ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class); |
| 276 | + |
| 277 | + verify(this.mongoOperationsMock, times(2)) |
| 278 | + .find(captor.capture(), Matchers.eq(Person.class), Matchers.eq("persons")); |
| 279 | + |
| 280 | + DBObject expectedSortObject = new BasicDBObjectBuilder().add("bar", -1).get(); |
| 281 | + assertThat(captor.getAllValues().get(0).getSortObject(), is(expectedSortObject)); |
| 282 | + assertThat(captor.getAllValues().get(1).getSortObject(), is(expectedSortObject)); |
| 283 | + } |
| 284 | + |
214 | 285 | private MongoQueryFake createQueryForMethod(String methodName, Class<?>... paramTypes) {
|
215 | 286 |
|
216 | 287 | try {
|
@@ -272,5 +343,8 @@ private interface Repo extends MongoRepository<Person, Long> {
|
272 | 343 | @org.springframework.data.mongodb.repository.Query("{}")
|
273 | 344 | Page<Person> findByAnnotatedQuery(String firstnanme, Pageable pageable);
|
274 | 345 |
|
| 346 | + /** @see DATAMONGO-1057 */ |
| 347 | + Slice<Person> findByLastname(String lastname, Pageable page); |
| 348 | + |
275 | 349 | }
|
276 | 350 | }
|
0 commit comments