Search in sources :

Example 1 with PaginationLinks

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

the class SubscriptionControllerTest method whenListSubscriptionsWithQueryParamsThenOk.

@Test
public void whenListSubscriptionsWithQueryParamsThenOk() 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(ImmutableSet.of("et1", "et2"), "app", 0, 30).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(subscriptionList)));
    verify(subscriptionRepository, times(1)).listSubscriptions(ImmutableSet.of("et1", "et2"), Optional.of("app"), 0, 30);
}
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 2 with PaginationLinks

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

the class SubscriptionControllerTest method whenListSubscriptionsThenPaginationIsOk.

@Test
public void whenListSubscriptionsThenPaginationIsOk() throws Exception {
    final List<Subscription> subscriptions = createRandomSubscriptions(10);
    when(subscriptionRepository.listSubscriptions(any(), any(), anyInt(), anyInt())).thenReturn(subscriptions);
    final PaginationLinks.Link prevLink = new PaginationLinks.Link("/subscriptions?event_type=et1&event_type=et2&owning_application=app&offset=0&limit=10");
    final PaginationLinks.Link nextLink = new PaginationLinks.Link("/subscriptions?event_type=et1&event_type=et2&owning_application=app&offset=15&limit=10");
    final PaginationLinks links = new PaginationLinks(Optional.of(prevLink), Optional.of(nextLink));
    final PaginationWrapper expectedResult = new PaginationWrapper(subscriptions, links);
    getSubscriptions(ImmutableSet.of("et1", "et2"), "app", 5, 10).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedResult)));
}
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 3 with PaginationLinks

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

the class SubscriptionService method listSubscriptions.

public Result listSubscriptions(@Nullable final String owningApplication, @Nullable final Set<String> eventTypes, final int limit, final int offset) {
    if (limit < 1 || limit > 1000) {
        final Problem problem = Problem.valueOf(Response.Status.BAD_REQUEST, "'limit' parameter should have value from 1 to 1000");
        return Result.problem(problem);
    }
    if (offset < 0) {
        final Problem problem = Problem.valueOf(Response.Status.BAD_REQUEST, "'offset' parameter can't be lower than 0");
        return Result.problem(problem);
    }
    try {
        final Set<String> eventTypesFilter = eventTypes == null ? ImmutableSet.of() : eventTypes;
        final Optional<String> owningAppOption = Optional.ofNullable(owningApplication);
        final List<Subscription> subscriptions = subscriptionRepository.listSubscriptions(eventTypesFilter, owningAppOption, offset, limit);
        final PaginationLinks paginationLinks = SubscriptionsUriHelper.createSubscriptionPaginationLinks(owningAppOption, eventTypesFilter, offset, limit, subscriptions.size());
        return Result.ok(new PaginationWrapper<>(subscriptions, paginationLinks));
    } catch (final ServiceUnavailableException e) {
        LOG.error("Error occurred during listing of subscriptions", e);
        return Result.problem(e.asProblem());
    }
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) Problem(org.zalando.problem.Problem) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Subscription(org.zalando.nakadi.domain.Subscription)

Example 4 with PaginationLinks

use of org.zalando.nakadi.domain.PaginationLinks 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 5 with PaginationLinks

use of org.zalando.nakadi.domain.PaginationLinks 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)

Aggregations

PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)6 PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)5 Subscription (org.zalando.nakadi.domain.Subscription)5 Test (org.junit.Test)4 List (java.util.List)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)1 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)1 Problem (org.zalando.problem.Problem)1