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);
}
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)));
}
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());
}
}
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);
}
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);
}
Aggregations