Search in sources :

Example 6 with PaginationWrapper

use of org.zalando.nakadi.domain.PaginationWrapper in project nakadi by zalando.

the class PaginationService method paginate.

public PaginationWrapper paginate(final int offset, final int limit, final String path, final ItemsSupplier itemsSupplier, final Supplier<Integer> countSupplier) {
    final List items = itemsSupplier.queryOneMore(offset, limit);
    final PaginationLinks paginationLinks;
    if (items.isEmpty() && offset != 0 && limit != 0) {
        final int count = countSupplier.get();
        int latestOffset = count / limit;
        if (offset >= latestOffset) {
            latestOffset = 0;
        }
        paginationLinks = createLinks(path, latestOffset, limit, items.size());
    } else {
        paginationLinks = createLinks(path, offset, limit, items.size());
        if (items.size() > limit) {
            items.remove(items.size() - 1);
        }
    }
    return new PaginationWrapper(items, paginationLinks);
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) List(java.util.List) PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper)

Example 7 with PaginationWrapper

use of org.zalando.nakadi.domain.PaginationWrapper in project nakadi by zalando.

the class SubscriptionControllerTest method whenListSubscriptionsWithoutQueryParamsThenOk.

@Test
public void whenListSubscriptionsWithoutQueryParamsThenOk() throws Exception {
    final List<Subscription> subscriptions = createRandomSubscriptions(10);
    when(subscriptionRepository.listSubscriptions(any(), any(), anyInt(), anyInt())).thenReturn(subscriptions);
    final PaginationWrapper subscriptionList = new PaginationWrapper(subscriptions, new PaginationLinks());
    getSubscriptions().andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(subscriptionList)));
    verify(subscriptionRepository, times(1)).listSubscriptions(ImmutableSet.of(), Optional.empty(), 0, 20);
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) Subscription(org.zalando.nakadi.domain.Subscription) PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) Test(org.junit.Test)

Example 8 with PaginationWrapper

use of org.zalando.nakadi.domain.PaginationWrapper in project nakadi by zalando.

the class PaginationServiceTest method testPaginationEmpty.

@Test
public void testPaginationEmpty() {
    final PaginationService paginationService = new PaginationService();
    final PaginationWrapper paginationWrapper = paginationService.paginate(0, 5, "/schemas", (o, l) -> Collections.emptyList(), () -> 5);
    Assert.assertFalse(paginationWrapper.getLinks().getNext().isPresent());
    Assert.assertFalse(paginationWrapper.getLinks().getPrev().isPresent());
}
Also used : PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) Test(org.junit.Test)

Example 9 with PaginationWrapper

use of org.zalando.nakadi.domain.PaginationWrapper in project nakadi by zalando.

the class PaginationServiceTest method testPaginationPrev2.

@Test
public void testPaginationPrev2() {
    final PaginationService paginationService = new PaginationService();
    final PaginationWrapper paginationWrapper = paginationService.paginate(2, 5, "/schemas", (o, l) -> Collections.emptyList(), () -> 20);
    Assert.assertFalse(paginationWrapper.getLinks().getNext().isPresent());
    Assert.assertEquals("/schemas?offset=0&limit=5", paginationWrapper.getLinks().getPrev().get().getHref());
}
Also used : PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) Test(org.junit.Test)

Example 10 with PaginationWrapper

use of org.zalando.nakadi.domain.PaginationWrapper in project nakadi by zalando.

the class PaginationServiceTest method testPaginationPrev4.

@Test
public void testPaginationPrev4() {
    final PaginationService paginationService = new PaginationService();
    final PaginationWrapper paginationWrapper = paginationService.paginate(2, 5, "/schemas", (o, l) -> Collections.emptyList(), () -> 5);
    Assert.assertFalse(paginationWrapper.getLinks().getNext().isPresent());
    Assert.assertFalse(paginationWrapper.getLinks().getPrev().isPresent());
}
Also used : PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) Test(org.junit.Test)

Aggregations

PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)12 Test (org.junit.Test)11 PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)5 Subscription (org.zalando.nakadi.domain.Subscription)4 List (java.util.List)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)1