PageRequestFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) { PageRequest p = null; MultivaluedMap<String, String> params = requestContext.getUriInfo().getQueryParameters(); String page = params.getFirst(PageRequest.PAGE_PARAM); String perPage = params.getFirst(PageRequest.PER_PAGE_PARAM); String order = params.getFirst(PageRequest.ORDER_PARAM); String sortBy = params.getFirst(PageRequest.SORT_BY_PARAM); if (page != null || perPage != null || order != null || sortBy != null) { p = new PageRequest(); if (order == null) { p.setOrder(PageRequest.DEFAULT_ORDER); } else { p.setOrder(readOrder(order)); } p.setSortBy(sortBy); try { if (page == null && perPage != null) { p.setPage(PageRequest.DEFAULT_PAGE); p.setPerPage(readInteger(perPage)); } else if (page != null && perPage == null) { p.setPage(readInteger(page)); p.setPerPage(PageRequest.DEFAULT_PER_PAGE); } else { p.setPage(readInteger(page)); p.setPerPage(readInteger(perPage)); } } catch (NumberFormatException nfe) { I18n i18n = this.i18nProvider.get(); throw new BadRequestException(i18n.tr("offset and limit parameters" + " must be positive integers"), nfe); } } ResteasyContext.pushContext(PageRequest.class, p); } @Inject  PageRequestFilter(javax.inject.Provider<I18n> i18nProvider); @Override void filter(ContainerRequestContext requestContext);  }
@Test public void testNoAnything() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertNull(p); }
@Test public void testBothLimitAndPage() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertEquals(Integer.valueOf(10), p.getPerPage()); assertEquals(Integer.valueOf(4), p.getPage()); assertEquals(PageRequest.DEFAULT_ORDER, p.getOrder()); assertNull(p.getSortBy()); }
@Test public void testNoLimitButPage() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertEquals(PageRequest.DEFAULT_PER_PAGE, p.getPerPage()); assertEquals(Integer.valueOf(5), p.getPage()); assertEquals(PageRequest.DEFAULT_ORDER, p.getOrder()); assertNull(p.getSortBy()); }
@Test public void testLimitButNoPage() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertEquals(Integer.valueOf(10), p.getPerPage()); assertEquals(PageRequest.DEFAULT_PAGE, p.getPage()); assertEquals(PageRequest.DEFAULT_ORDER, p.getOrder()); assertNull(p.getSortBy()); }
@Test public void testBadIntegerValue() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); assertThrows(BadRequestException.class, () -> interceptor.filter(mockRequestContext)); }
@Test public void testDoesNotAllowPageZero() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); assertThrows(BadRequestException.class, () -> interceptor.filter(mockRequestContext)); }
@Test public void testNoPagingIfJustOrderAndSortBy() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertFalse(p.isPaging()); assertEquals(PageRequest.Order.ASCENDING, p.getOrder()); assertEquals("id", p.getSortBy()); }
@Test public void testUsesDefaultOrderIfNoOrderProvided() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertFalse(p.isPaging()); assertEquals(PageRequest.DEFAULT_ORDER, p.getOrder()); assertEquals("id", p.getSortBy()); }
@Test public void testDescendingOrder() throws Exception { mockReq = MockHttpRequest.create("GET", "http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); PageRequest p = ResteasyContext.getContextData(PageRequest.class); assertFalse(p.isPaging()); assertEquals(PageRequest.Order.DESCENDING, p.getOrder()); assertEquals("id", p.getSortBy()); }