Search in sources :

Example 1 with PagingAndSortingRepository

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));
}
Also used : IntStream(java.util.stream.IntStream) Query(com.vaadin.flow.data.provider.Query) PagingAndSortingRepository(org.springframework.data.repository.PagingAndSortingRepository) PageRequest(org.springframework.data.domain.PageRequest) CallbackDataProvider(com.vaadin.flow.data.provider.CallbackDataProvider) Test(org.junit.Test) Collectors(java.util.stream.Collectors) QuerySortOrder(com.vaadin.flow.data.provider.QuerySortOrder) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) Sort(org.springframework.data.domain.Sort) Assert(org.junit.Assert) Collections(java.util.Collections) PageImpl(org.springframework.data.domain.PageImpl) PageImpl(org.springframework.data.domain.PageImpl) Query(com.vaadin.flow.data.provider.Query) CallbackDataProvider(com.vaadin.flow.data.provider.CallbackDataProvider) PageRequest(org.springframework.data.domain.PageRequest) PagingAndSortingRepository(org.springframework.data.repository.PagingAndSortingRepository) Sort(org.springframework.data.domain.Sort) Test(org.junit.Test)

Aggregations

CallbackDataProvider (com.vaadin.flow.data.provider.CallbackDataProvider)1 Query (com.vaadin.flow.data.provider.Query)1 QuerySortOrder (com.vaadin.flow.data.provider.QuerySortOrder)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1 Mockito (org.mockito.Mockito)1 PageImpl (org.springframework.data.domain.PageImpl)1 PageRequest (org.springframework.data.domain.PageRequest)1 Sort (org.springframework.data.domain.Sort)1 PagingAndSortingRepository (org.springframework.data.repository.PagingAndSortingRepository)1