Search in sources :

Example 6 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class ComplianceOp method preProcess.

/**
 * Computes the compliance status implemented in java script.
 * does not compute the hash or apply the status.
 * requires only the consumer to be locked.
 * @param context
 */
@Override
public boolean preProcess(BindContext context) {
    Consumer consumer = context.getLockedConsumer();
    Map<String, Entitlement> entitlementMap = context.getEntitlementMap();
    Map<String, PoolQuantity> poolQuantityMap = context.getPoolQuantities();
    for (Map.Entry<String, Entitlement> entry : entitlementMap.entrySet()) {
        PoolQuantity pq = poolQuantityMap.get(entry.getKey());
        entry.getValue().setPool(pq.getPool());
    }
    status = complianceRules.getStatus(consumer, entitlementMap.values(), null, false, false, false, false);
    for (Map.Entry<String, Entitlement> entry : entitlementMap.entrySet()) {
        PoolQuantity pq = poolQuantityMap.get(entry.getKey());
        entry.getValue().setPool(null);
    }
    return true;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Consumer(org.candlepin.model.Consumer) Entitlement(org.candlepin.model.Entitlement) Map(java.util.Map)

Example 7 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class EntitlementCertificateGeneratorTest method testGenerateEntitlementCertificate.

@Test
public void testGenerateEntitlementCertificate() throws GeneralSecurityException, IOException {
    this.ecGenerator = new EntitlementCertificateGenerator(this.mockEntCertCurator, this.mockEntCertAdapter, this.mockEntitlementCurator, this.mockPoolCurator, this.mockEventSink, this.mockEventFactory, this.mockProductCurator);
    Consumer consumer = mock(Consumer.class);
    Pool pool = mock(Pool.class);
    Product product = mock(Product.class);
    when(pool.getId()).thenReturn("Swift");
    when(pool.getProduct()).thenReturn(product);
    Entitlement entitlement = mock(Entitlement.class);
    when(entitlement.getConsumer()).thenReturn(consumer);
    Map<String, Product> expectedProducts = new HashMap<>();
    expectedProducts.put("Swift", product);
    Map<String, Entitlement> expected = new HashMap<>();
    expected.put("Swift", entitlement);
    Map<String, PoolQuantity> poolQuantityMap = new HashMap<>();
    poolQuantityMap.put("Swift", new PoolQuantity(pool, 0));
    ecGenerator.generateEntitlementCertificate(pool, entitlement);
    verify(mockEntCertAdapter).generateEntitlementCerts(eq(consumer), eq(poolQuantityMap), eq(expected), eq(expectedProducts), eq(true));
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 8 with PoolQuantity

use of org.candlepin.model.PoolQuantity 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 PoolQuantity

use of org.candlepin.model.PoolQuantity 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 PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class HostedVirtLimitEntitlementRulesTest method noBonusPoolsForHostedNonDistributorBinds.

/*
     * Bonus pools should not be created when we are in a hosted scenario without
     * distributor binds.
     */
@Test
public void noBonusPoolsForHostedNonDistributorBinds() {
    when(config.getBoolean(ConfigProperties.STANDALONE)).thenReturn(false);
    Subscription s = createVirtLimitSub("virtLimitProduct", 10, "unlimited");
    Pool p = TestUtil.copyFromSub(s);
    when(poolManagerMock.isManaged(eq(p))).thenReturn(true);
    List<Pool> pools = poolRules.createAndEnrichPools(p, new LinkedList<>());
    assertEquals(2, pools.size());
    Pool physicalPool = pools.get(0);
    physicalPool.setId("physical");
    Pool virtBonusPool = pools.get(1);
    virtBonusPool.setId("virt");
    assertEquals(new Long(10), physicalPool.getQuantity());
    assertEquals(0, physicalPool.getAttributes().size());
    // Quantity on bonus pool should be -1:
    assertEquals(new Long(-1), virtBonusPool.getQuantity());
    assertEquals("true", virtBonusPool.getAttributeValue(Product.Attributes.VIRT_ONLY));
    assertEquals("unlimited", virtBonusPool.getProduct().getAttributeValue(Product.Attributes.VIRT_LIMIT));
    Entitlement e = new Entitlement(physicalPool, consumer, owner, 1);
    Map<String, Entitlement> entitlements = new HashMap<>();
    entitlements.put(physicalPool.getId(), e);
    Map<String, PoolQuantity> poolQuantityMap = new HashMap<>();
    poolQuantityMap.put(physicalPool.getId(), new PoolQuantity(physicalPool, 1));
    enforcer.postEntitlement(poolManagerMock, consumer, owner, entitlements, null, false, poolQuantityMap);
    verify(poolManagerMock, never()).createPool(any(Pool.class));
    verify(poolManagerMock, never()).updatePoolQuantity(any(Pool.class), anyInt());
    enforcer.postUnbind(consumer, poolManagerMock, e);
    verify(poolManagerMock, never()).updatePoolQuantity(any(Pool.class), anyInt());
    verify(poolManagerMock, never()).setPoolQuantity(any(Pool.class), anyLong());
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) HashMap(java.util.HashMap) Matchers.anyLong(org.mockito.Matchers.anyLong) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Aggregations

PoolQuantity (org.candlepin.model.PoolQuantity)75 Pool (org.candlepin.model.Pool)65 Test (org.junit.Test)52 Entitlement (org.candlepin.model.Entitlement)34 LinkedList (java.util.LinkedList)30 Product (org.candlepin.model.Product)28 HashMap (java.util.HashMap)27 ArrayList (java.util.ArrayList)21 Consumer (org.candlepin.model.Consumer)12 List (java.util.List)11 ConsumerType (org.candlepin.model.ConsumerType)11 Matchers.anyString (org.mockito.Matchers.anyString)11 Date (java.util.Date)8 Set (java.util.Set)8 HashSet (java.util.HashSet)7 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)7 ValidationResult (org.candlepin.policy.ValidationResult)7 Subscription (org.candlepin.model.dto.Subscription)6 Matchers.anyLong (org.mockito.Matchers.anyLong)6 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)5