Search in sources :

Example 31 with PageRequest

use of org.candlepin.common.paging.PageRequest in project candlepin by candlepin.

the class PageRequestFilter method filter.

@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));
        }
        /* We'll leave it to the curator layer to figure out what to sort by if
             * sortBy is null. */
        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);
        }
    }
    ResteasyProviderFactory.pushContext(PageRequest.class, p);
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) BadRequestException(org.candlepin.common.exceptions.BadRequestException) I18n(org.xnap.commons.i18n.I18n)

Example 32 with PageRequest

use of org.candlepin.common.paging.PageRequest in project candlepin by candlepin.

the class PageRequestFilterTest method testNoAnything.

@Test
public void testNoAnything() throws Exception {
    mockReq = MockHttpRequest.create("GET", "http://localhost/candlepin/status");
    when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri());
    interceptor.filter(mockRequestContext);
    PageRequest p = ResteasyProviderFactory.getContextData(PageRequest.class);
    assertNull(p);
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Test(org.junit.Test)

Example 33 with PageRequest

use of org.candlepin.common.paging.PageRequest in project candlepin by candlepin.

the class PageRequestFilterTest method testBothLimitAndPage.

@Test
public void testBothLimitAndPage() throws Exception {
    mockReq = MockHttpRequest.create("GET", "http://localhost/candlepin/status?per_page=10&page=4");
    when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri());
    interceptor.filter(mockRequestContext);
    PageRequest p = ResteasyProviderFactory.getContextData(PageRequest.class);
    assertEquals(Integer.valueOf(10), p.getPerPage());
    assertEquals(Integer.valueOf(4), p.getPage());
    assertEquals(PageRequest.DEFAULT_ORDER, p.getOrder());
    assertNull(p.getSortBy());
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Test(org.junit.Test)

Example 34 with PageRequest

use of org.candlepin.common.paging.PageRequest in project candlepin by candlepin.

the class ConsumerResourceIntegrationTest method testConsumerCannotListWithUuidsAndOtherParameters.

@Test(expected = BadRequestException.class)
public void testConsumerCannotListWithUuidsAndOtherParameters() {
    Consumer consumer = TestUtil.createConsumer(standardSystemType, owner);
    consumerCurator.create(consumer);
    setupAdminPrincipal("admin");
    securityInterceptor.enable();
    List<String> uuidList = new ArrayList<>();
    uuidList.add(consumer.getUuid());
    consumerResource.list("username", toSet("typeLabel"), owner.getKey(), uuidList, null, null, new PageRequest());
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 35 with PageRequest

use of org.candlepin.common.paging.PageRequest in project candlepin by candlepin.

the class EntitlementResourceTest method getAllEntitlements.

@Test
public void getAllEntitlements() {
    PageRequest req = new PageRequest();
    req.setPage(1);
    req.setPerPage(10);
    Entitlement e = TestUtil.createEntitlement();
    e.setId("getEntitlementList");
    List<Entitlement> entitlements = new ArrayList<>();
    entitlements.add(e);
    Page<List<Entitlement>> page = new Page<>();
    page.setPageData(entitlements);
    EntitlementDTO entitlementDTO = new EntitlementDTO();
    entitlementDTO.setId("getEntitlementList");
    when(entitlementCurator.listAll(isA(EntitlementFilterBuilder.class), isA(PageRequest.class))).thenReturn(page);
    when(modelTranslator.translate(isA(Entitlement.class), eq(EntitlementDTO.class))).thenReturn(entitlementDTO);
    List<EntitlementDTO> result = entResource.listAllForConsumer(null, null, null, req);
    assertEquals(1, result.size());
    assertEquals("getEntitlementList", result.get(0).getId());
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) EntitlementDTO(org.candlepin.dto.api.v1.EntitlementDTO) EntitlementFilterBuilder(org.candlepin.model.EntitlementFilterBuilder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Page(org.candlepin.common.paging.Page) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Aggregations

PageRequest (org.candlepin.common.paging.PageRequest)62 Test (org.junit.Test)54 List (java.util.List)35 ArrayList (java.util.ArrayList)32 LinkedList (java.util.LinkedList)26 Date (java.util.Date)14 Page (org.candlepin.common.paging.Page)13 Pool (org.candlepin.model.Pool)9 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)7 Consumer (org.candlepin.model.Consumer)6 Owner (org.candlepin.model.Owner)5 EntitlementDTO (org.candlepin.dto.api.v1.EntitlementDTO)4 Entitlement (org.candlepin.model.Entitlement)4 Product (org.candlepin.model.Product)4 Transactional (com.google.inject.persist.Transactional)3 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)3 EntitlementFilterBuilder (org.candlepin.model.EntitlementFilterBuilder)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 BadRequestException (org.candlepin.common.exceptions.BadRequestException)2