Search in sources :

Example 6 with Page

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

the class LinkHeaderResponseFilterTest method testGetPrevPage.

@Test
public void testGetPrevPage() {
    Page<Object> p = new Page<>();
    p.setMaxRecords(55);
    PageRequest pr = new PageRequest();
    p.setPageRequest(pr);
    pr.setPerPage(10);
    pr.setPage(3);
    assertEquals(Integer.valueOf(2), interceptor.getPrevPage(p));
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Page(org.candlepin.common.paging.Page) Test(org.junit.Test)

Example 7 with Page

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

the class LinkHeaderResponseFilterTest method testGetPrevPageWhenOnFirstPage.

@Test
public void testGetPrevPageWhenOnFirstPage() {
    Page<Object> p = new Page<>();
    p.setMaxRecords(55);
    PageRequest pr = new PageRequest();
    p.setPageRequest(pr);
    pr.setPerPage(10);
    pr.setPage(1);
    assertNull(interceptor.getPrevPage(p));
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Page(org.candlepin.common.paging.Page) Test(org.junit.Test)

Example 8 with Page

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

the class PoolManagerTest method testEntitlebyProductRetry.

@Test
public void testEntitlebyProductRetry() throws Exception {
    Product product = TestUtil.createProduct();
    List<Pool> pools = new ArrayList<>();
    Pool pool1 = TestUtil.createPool(product);
    pool1.setId("poolId1");
    pools.add(pool1);
    Pool pool2 = TestUtil.createPool(product);
    pool2.setId("poolId2");
    pools.add(pool2);
    Date now = new Date();
    Map<String, ValidationResult> resultMap = new HashMap<>();
    ValidationResult result = mock(ValidationResult.class);
    resultMap.put("poolId1", result);
    resultMap.put("poolId2", result);
    Page page = mock(Page.class);
    when(page.getPageData()).thenReturn(pools);
    when(mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), any(String.class), any(String.class), any(String.class), eq(now), any(PoolFilterBuilder.class), any(PageRequest.class), anyBoolean(), anyBoolean(), anyBoolean(), any(Date.class))).thenReturn(page);
    CandlepinQuery mockQuery = mock(CandlepinQuery.class);
    when(mockPoolCurator.listAllByIds(any(List.class))).thenReturn(mockQuery);
    List<Pool> poolList = Arrays.asList(pool1);
    when(mockQuery.iterator()).thenReturn(poolList.listIterator()).thenReturn(poolList.listIterator()).thenReturn(poolList.listIterator()).thenReturn(poolList.listIterator());
    when(enforcerMock.preEntitlement(any(Consumer.class), anyCollectionOf(PoolQuantity.class), any(CallerType.class))).thenReturn(resultMap);
    when(result.isSuccessful()).thenReturn(false);
    List<ValidationError> errors = new ArrayList<>();
    errors.add(new ValidationError("rulefailed.no.entitlements.available"));
    when(result.getErrors()).thenReturn(errors);
    List<PoolQuantity> bestPools = new ArrayList<>();
    bestPools.add(new PoolQuantity(pool1, 1));
    when(autobindRules.selectBestPools(any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class), eq(false))).thenReturn(bestPools);
    AutobindData data = AutobindData.create(TestUtil.createConsumer(owner), owner).forProducts(new String[] { product.getUuid() }).on(now);
    doNothing().when(mockPoolCurator).flush();
    try {
        List<Entitlement> e = manager.entitleByProducts(data);
        fail();
    } catch (EntitlementRefusedException e) {
        assertNotNull(e);
        verify(autobindRules, times(4)).selectBestPools(any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class), eq(false));
    }
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Page(org.candlepin.common.paging.Page) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ValidationResult(org.candlepin.policy.ValidationResult) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) AutobindData(org.candlepin.resource.dto.AutobindData) ValidationError(org.candlepin.policy.ValidationError) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) Date(java.util.Date) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 9 with Page

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

the class PoolManagerTest method testEntitleWithADate.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testEntitleWithADate() throws Exception {
    Product product = TestUtil.createProduct();
    List<Pool> pools = new ArrayList<>();
    Pool pool1 = TestUtil.createPool(product);
    pools.add(pool1);
    Pool pool2 = TestUtil.createPool(product);
    pools.add(pool2);
    Date now = new Date();
    ValidationResult result = mock(ValidationResult.class);
    Page page = mock(Page.class);
    when(page.getPageData()).thenReturn(pools);
    when(mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), any(String.class), any(String.class), any(String.class), eq(now), any(PoolFilterBuilder.class), any(PageRequest.class), anyBoolean(), anyBoolean(), anyBoolean(), any(Date.class))).thenReturn(page);
    CandlepinQuery mockQuery = mock(CandlepinQuery.class);
    when(mockPoolCurator.listAllByIds(any(List.class))).thenReturn(mockQuery);
    when(mockQuery.iterator()).thenReturn(Arrays.asList(pool1).listIterator());
    when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt(), any(CallerType.class))).thenReturn(result);
    when(result.isSuccessful()).thenReturn(true);
    List<PoolQuantity> bestPools = new ArrayList<>();
    bestPools.add(new PoolQuantity(pool1, 1));
    when(autobindRules.selectBestPools(any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class), eq(false))).thenReturn(bestPools);
    ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
    Consumer consumer = TestUtil.createConsumer(ctype, owner);
    AutobindData data = AutobindData.create(consumer, owner).forProducts(new String[] { product.getUuid() }).on(now);
    doNothing().when(mockPoolCurator).flush();
    doNothing().when(mockPoolCurator).clear();
    List<Entitlement> e = manager.entitleByProducts(data);
    assertNotNull(e);
    assertEquals(e.size(), 1);
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Page(org.candlepin.common.paging.Page) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ValidationResult(org.candlepin.policy.ValidationResult) Date(java.util.Date) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) AutobindData(org.candlepin.resource.dto.AutobindData) ConsumerType(org.candlepin.model.ConsumerType) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 10 with Page

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

the class AbstractHibernateCurator method listByCriteria.

@SuppressWarnings("unchecked")
@Transactional
public Page<List<E>> listByCriteria(Criteria c, PageRequest pageRequest) {
    Page<List<E>> page = new Page<>();
    if (pageRequest != null) {
        // see https://forum.hibernate.org/viewtopic.php?t=974802
        // Save original Projection and ResultTransformer
        CriteriaImpl cImpl = (CriteriaImpl) c;
        Projection origProjection = cImpl.getProjection();
        ResultTransformer origRt = cImpl.getResultTransformer();
        // Get total number of records by setting a rowCount projection
        page.setMaxRecords(findRowCount(c));
        // Restore original Projection and ResultTransformer
        c.setProjection(origProjection);
        c.setResultTransformer(origRt);
        page.setPageData(loadPageData(c, pageRequest));
        page.setPageRequest(pageRequest);
    } else {
        List<E> pageData = listByCriteria(c);
        page.setMaxRecords(pageData.size());
        page.setPageData(pageData);
    }
    return page;
}
Also used : CriteriaImpl(org.hibernate.internal.CriteriaImpl) ResultTransformer(org.hibernate.transform.ResultTransformer) Projection(org.hibernate.criterion.Projection) List(java.util.List) ArrayList(java.util.ArrayList) Page(org.candlepin.common.paging.Page) Transactional(com.google.inject.persist.Transactional)

Aggregations

Page (org.candlepin.common.paging.Page)20 PageRequest (org.candlepin.common.paging.PageRequest)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)10 List (java.util.List)10 LinkedList (java.util.LinkedList)6 Consumer (org.candlepin.model.Consumer)6 Entitlement (org.candlepin.model.Entitlement)6 Pool (org.candlepin.model.Pool)6 CandlepinQuery (org.candlepin.model.CandlepinQuery)5 Transactional (com.google.inject.persist.Transactional)4 Date (java.util.Date)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 EntitlementDTO (org.candlepin.dto.api.v1.EntitlementDTO)4 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)4 Product (org.candlepin.model.Product)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)3 ConsumerType (org.candlepin.model.ConsumerType)3 EntitlementFilterBuilder (org.candlepin.model.EntitlementFilterBuilder)3