private PaginatedList<ViewDTO> searchPaginated(SearchUser searchUser,
                                                   SearchQuery query,
                                                   Predicate<ViewDTO> filter,
                                                   SortOrder order,
                                                   String sortField,
                                                   Bson grandTotalQuery,
                                                   int page,
                                                   int perPage) {
        final PaginatedList<ViewDTO> viewsList = findPaginatedWithQueryFilterAndSortWithGrandTotal(searchUser, query, filter,
                Sorts.orderBy(order.toBsonSort(sortField), order.toBsonSort(ViewDTO.SECONDARY_SORT)),
                grandTotalQuery, page, perPage);
        return viewsList.withList(viewsList.delegate().stream()
                .map(this::requirementsForView)
                .toList());
    }
@Test
    public void searchPaginated() {
        final ImmutableMap<String, SearchQueryField> searchFieldMapping = ImmutableMap.<String, SearchQueryField>builder()
                .put("id", SearchQueryField.create(ViewDTO.FIELD_ID))
                .put("title", SearchQueryField.create(ViewDTO.FIELD_TITLE))
                .put("summary", SearchQueryField.create(ViewDTO.FIELD_DESCRIPTION))
                .build();

        dbService.save(ViewDTO.builder().title("View A").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
        dbService.save(ViewDTO.builder().title("View B").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
        dbService.save(ViewDTO.builder().title("View C").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
        dbService.save(ViewDTO.builder().title("View D").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
        dbService.save(ViewDTO.builder().title("View E").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());

        final SearchQueryParser queryParser = new SearchQueryParser(ViewDTO.FIELD_TITLE, searchFieldMapping);

        final PaginatedList<ViewDTO> result1 = dbService.searchPaginated(
                searchUser,
                queryParser.parse("A B D"),
                view -> true, SortOrder.DESCENDING,
                "title",
                1,
                5
        );

        assertThat(result1)
                .hasSize(3)
                .extracting("title")
                .containsExactly("View D", "View B", "View A");

        assertThat(result1.grandTotal()).hasValue(5L);

        final PaginatedList<ViewDTO> result2 = dbService.searchPaginated(
                searchUser,
                queryParser.parse("A B D"),
                view -> view.title().contains("B") || view.title().contains("D"), SortOrder.DESCENDING,
                "title",
                1,
                5
        );

        assertThat(result2)
                .hasSize(2)
                .extracting("title")
                .containsExactly("View D", "View B");

        assertThat(result2.grandTotal()).hasValue(5L);
    }