use of org.springframework.data.repository.PagingAndSortingRepository in project flow by vaadin.
the class VaadinSpringDataHelpersTest method fromPagingRepository_fetchesItemsFromPagingRepoAccordingToVaadinQuery.
@Test
public void fromPagingRepository_fetchesItemsFromPagingRepoAccordingToVaadinQuery() {
PagingAndSortingRepository repo = Mockito.mock(PagingAndSortingRepository.class);
Mockito.doAnswer(mock -> {
PageRequest pageRequest = mock.getArgument(0);
int from = pageRequest.getPageNumber() * pageRequest.getPageSize();
Sort.Order order = pageRequest.getSort().getOrderFor("someSortField");
Stream<Integer> itemsStream = IntStream.range(0, 500).boxed();
if (order.isDescending()) {
itemsStream = itemsStream.sorted(Collections.reverseOrder());
}
// given string items 'Item XYZ' ordered according to a given query
List<String> items = itemsStream.skip(from).limit(pageRequest.getPageSize()).map(i -> "Item " + i).collect(Collectors.toList());
return new PageImpl<>(items);
}).when(repo).findAll(Mockito.any(PageRequest.class));
CallbackDataProvider.FetchCallback<String, Void> callback = VaadinSpringDataHelpers.fromPagingRepository(repo);
// when items with indexes 100..149 and descending order are fetched
Query<String, Void> query = new Query<>(100, 50, QuerySortOrder.desc("someSortField").build(), null, null);
List<String> result = callback.fetch(query).collect(Collectors.toList());
// then the result should contain items 'Item 399'...'Item 350'.
Assert.assertEquals(50, result.size());
Assert.assertEquals("Item 399", result.get(0));
Assert.assertEquals("Item 350", result.get(49));
}
Aggregations