/** * @param <T> the type of the underlying model * @param offset The offset supplied by the client or the server default. * @param total The total number of results matching the request. * @param results The mocked result. * @return the paged query result. * @deprecated PagedQueryResult should remain as a read model, and be constructed only by deserialization. */ @Deprecated static <T> PagedQueryResultDsl<T> of(final Long offset, final Long total, final List<T> results) { return of(offset, -1L, total, results); }
/** * @param <T> the type of the underlying model * @param results The mocked result. * @return the paged query result. * @deprecated PagedQueryResult should remain as a read model, and be constructed only by deserialization. */ @Deprecated static <T> PagedQueryResultDsl<T> of(final List<T> results) { final long size = results.size(); return of(0L, size, size, results); }
@JsonIgnore public static <T> PagedQueryResultDsl<T> of(final T singleResult) { return of(Arrays.asList(singleResult)); } }
/** * @param <T> the type of the underlying model * @param singleResult the single result expected in the return. * @deprecated PagedQueryResult should remain as a read model, and be constructed only by deserialization. * @return the paged query result. */ @JsonIgnore static <T> PagedQueryResultDsl<T> of(final T singleResult) { return of(Collections.singletonList(singleResult)); }
/** * Creates a copy of this item with the given offset. * @param offset the offset of the new copy * @return the copy */ public PagedQueryResultDsl<T> withOffset(final int offset) { return PagedQueryResult.of(offset, getTotal(), getResults()); }
/** * Creates a copy of this with the given total items count. * @param total the number of total items in the backend. * @return a copy with total as new total. */ public PagedQueryResultDsl<T> withTotal(final int total) { return PagedQueryResult.of(getOffset(), total, getResults()); } }
/** * Creates a copy of this with the given total items count. * @param total the number of total items in the backend. * @return a copy with total as new total. */ public PagedQueryResultDsl<T> withTotal(final Long total) { return PagedQueryResult.of(getOffset(), getLimit(), total, getResults()); } }
/** * Creates a copy of this item with the given offset. * @param offset the offset of the new copy * @return the copy */ public PagedQueryResultDsl<T> withOffset(final Long offset) { return PagedQueryResult.of(offset, getLimit(), getTotal(), getResults()); }
@SuppressWarnings("unchecked") private <T> PagedQueryResult<T> generatePagedQueryResult(final long offset) { final long total = totalResults + deviation; final long count = min(PAGE_SIZE, max(totalResults - offset, 0)); final List<T> results = (List<T>) generateSortedResultList(offset, count); return PagedQueryResult.of(offset, (long) PAGE_SIZE, total, results); }
@Test public void firstOfManyResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(0L, PAGE_SIZE, TOTAL, listOfSize(PAGE_SIZE)); assertThat(queryResult.isFirst()).isTrue(); assertThat(queryResult.isLast()).isFalse(); }
@Test public void lastFilledOfManyResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(TOTAL - PAGE_SIZE, PAGE_SIZE, TOTAL, listOfSize(PAGE_SIZE)); assertThat(queryResult.isFirst()).isFalse(); assertThat(queryResult.isLast()).isTrue(); }
@Test public void lastNotFullyFilledOfManyResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(95L - PAGE_SIZE, PAGE_SIZE, 95L, listOfSize(PAGE_SIZE)); assertThat(queryResult.isFirst()).isFalse(); assertThat(queryResult.isLast()).isTrue(); }
@Test public void singleValueResult() throws Exception { final PagedQueryResult<String> result = PagedQueryResult.of("hello"); assertThat(result.getCount()).isEqualTo(1); assertThat(result.getOffset()).isEqualTo(0); assertThat(result.getResults()).isEqualTo(Collections.singletonList("hello")); assertThat(result.getTotal()).isEqualTo(1); }
@Test public void middleOfManyResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(PAGE_SIZE, PAGE_SIZE, TOTAL, listOfSize(PAGE_SIZE)); assertThat(queryResult.isFirst()).isFalse(); assertThat(queryResult.isLast()).isFalse(); }
@Test public void oneFilledResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(listOfSize(4L)); assertThat(queryResult.isFirst()).isTrue(); assertThat(queryResult.isLast()).isTrue(); }
@Test public void lastOneResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(100L, PAGE_SIZE, 101L, listOfSize(1L)); assertThat(queryResult.isFirst()).isFalse(); assertThat(queryResult.isLast()).isTrue(); }
@Test public void beforeLastOneResult() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of(99L, PAGE_SIZE, 101L, listOfSize(1L)); assertThat(queryResult.isFirst()).isFalse(); assertThat(queryResult.isLast()).isFalse(); }
@Test public void withTotal() throws Exception { final PagedQueryResult<String> result = PagedQueryResult.of("hello").withTotal(500L); assertThat(result.getCount()).isEqualTo(1); assertThat(result.getOffset()).isEqualTo(0); assertThat(result.getResults()).isEqualTo(Collections.singletonList("hello")); assertThat(result.getTotal()).isEqualTo(500); }