Search in sources :

Example 11 with EntitlementRefusedException

use of org.candlepin.policy.EntitlementRefusedException in project candlepin by candlepin.

the class PreEntitlementRulesCheckOp method preProcess.

/**
 * The java script portion of the rules can be run before the locks have been placed.
 * if there is a failure, we stop the chain here.
 * @param context
 */
@Override
public boolean preProcess(BindContext context) {
    /* Whether or not we run pre ent rules check, this first call
         * is used to fetch the pools and enrich some context fields.
         */
    Map<String, PoolQuantity> poolQuantityMap = context.getPoolQuantities();
    if (context.isQuantityRequested()) {
        log.debug("Running pre-entitlement rules.");
        // XXX preEntitlement is run twice for new entitlement creation
        results = enforcer.preEntitlement(context.getConsumer(), poolQuantityMap.values(), callerType);
        EntitlementRefusedException exception = checkResults();
        if (exception != null) {
            context.setException(exception, Thread.currentThread().getStackTrace());
            return false;
        }
    }
    return true;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException)

Example 12 with EntitlementRefusedException

use of org.candlepin.policy.EntitlementRefusedException in project candlepin by candlepin.

the class PreEntitlementRulesCheckOp method execute.

/**
 * The pool's quantity might have changed since we last fetched it,
 * so ensure that the pool still has enough quantity left.
 * @param context
 */
@Override
public boolean execute(BindContext context) {
    if (context.isQuantityRequested()) {
        for (PoolQuantity poolQuantity : context.getPoolQuantities().values()) {
            Pool pool = poolQuantity.getPool();
            enforcer.finishValidation(results.get(pool.getId()), pool, context.getPoolQuantities().get(pool.getId()).getQuantity());
        }
        EntitlementRefusedException exception = checkResults();
        if (exception != null) {
            context.setException(exception, Thread.currentThread().getStackTrace());
            return false;
        }
    }
    return true;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) Pool(org.candlepin.model.Pool)

Example 13 with EntitlementRefusedException

use of org.candlepin.policy.EntitlementRefusedException in project candlepin by candlepin.

the class PreEntitlementRulesCheckOp method checkResults.

private EntitlementRefusedException checkResults() {
    boolean success = true;
    for (Map.Entry<String, ValidationResult> entry : results.entrySet()) {
        ValidationResult result = entry.getValue();
        if (!result.isSuccessful()) {
            log.warn("Entitlement not granted: {} for pool: {}", result.getErrors().toString(), entry.getKey());
            success = false;
        }
    }
    if (!success) {
        return new EntitlementRefusedException(results);
    }
    return null;
}
Also used : EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) ValidationResult(org.candlepin.policy.ValidationResult) Map(java.util.Map)

Example 14 with EntitlementRefusedException

use of org.candlepin.policy.EntitlementRefusedException in project candlepin by candlepin.

the class PoolManagerFunctionalTest method testBatchBindError.

@Test
public void testBatchBindError() throws EntitlementRefusedException {
    Owner owner = createOwner();
    Product p = TestUtil.createProduct("test-product", "Test Product");
    p.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes");
    productCurator.create(p);
    Consumer devSystem = new Consumer("dev", "user", owner, systemType);
    devSystem.setFact("dev_sku", p.getId());
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(p));
    consumerCurator.create(devSystem);
    Pool pool1 = createPool(owner, p, 1L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    pool1.setAttribute(Pool.Attributes.DEVELOPMENT_POOL, "true");
    pool1.setAttribute(Pool.Attributes.REQUIRES_CONSUMER, devSystem.getUuid());
    poolCurator.create(pool1);
    Entitlement ent = createEntitlement(owner, devSystem, pool1, createEntitlementCertificate("keycert", "cert"));
    ent.setQuantity(1);
    entitlementCurator.create(ent);
    pool1.setConsumed(pool1.getConsumed() + 1);
    poolCurator.merge(pool1);
    poolCurator.flush();
    assertEquals(1, poolCurator.find(pool1.getId()).getConsumed().intValue());
    Pool pool2 = createPool(owner, p, 1L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    poolCurator.create(pool2);
    Entitlement ent2 = createEntitlement(owner, devSystem, pool2, createEntitlementCertificate("keycert", "cert"));
    ent2.setQuantity(1);
    entitlementCurator.create(ent2);
    pool2.setConsumed(pool2.getConsumed() + 1);
    poolCurator.merge(pool2);
    poolCurator.flush();
    Map<String, Integer> poolQuantities = new HashMap<>();
    poolQuantities.put(pool1.getId(), 1);
    poolQuantities.put(pool2.getId(), 1);
    try {
        List<Entitlement> results = poolManager.entitleByPools(devSystem, poolQuantities);
        fail();
    } catch (EntitlementRefusedException e) {
        assertNotNull(e.getResults());
        assertEquals(2, e.getResults().entrySet().size());
        assertEquals("rulefailed.no.entitlements.available", e.getResults().get(pool1.getId()).getErrors().get(0).getResourceKey());
        assertEquals("rulefailed.no.entitlements.available", e.getResults().get(pool2.getId()).getErrors().get(0).getResourceKey());
    }
}
Also used : Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 15 with EntitlementRefusedException

use of org.candlepin.policy.EntitlementRefusedException in project candlepin by candlepin.

the class EntitlerTest method bindByPoolErrorTest.

private void bindByPoolErrorTest(String msg) {
    try {
        String poolid = "pool10";
        Pool pool = mock(Pool.class);
        Map<String, ValidationResult> fakeResult = new HashMap<>();
        fakeResult.put(poolid, fakeOutResult(msg));
        EntitlementRefusedException ere = new EntitlementRefusedException(fakeResult);
        when(pool.getId()).thenReturn(poolid);
        when(poolCurator.find(eq(poolid))).thenReturn(pool);
        Map<String, Integer> pQs = new HashMap<>();
        pQs.put(poolid, 1);
        when(pm.entitleByPools(eq(consumer), eq(pQs))).thenThrow(ere);
        entitler.bindByPoolQuantity(consumer, poolid, 1);
    } catch (EntitlementRefusedException e) {
        fail(msg + ": threw unexpected error");
    }
}
Also used : HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) Pool(org.candlepin.model.Pool) ValidationResult(org.candlepin.policy.ValidationResult)

Aggregations

EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)16 Pool (org.candlepin.model.Pool)13 HashMap (java.util.HashMap)10 ValidationResult (org.candlepin.policy.ValidationResult)8 Entitlement (org.candlepin.model.Entitlement)7 PoolQuantity (org.candlepin.model.PoolQuantity)7 ArrayList (java.util.ArrayList)5 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)4 Transactional (com.google.inject.persist.Transactional)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Set (java.util.Set)3 Consumer (org.candlepin.model.Consumer)3 Owner (org.candlepin.model.Owner)3 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)3 Product (org.candlepin.model.Product)3 ValidationError (org.candlepin.policy.ValidationError)3 ComplianceStatus (org.candlepin.policy.js.compliance.ComplianceStatus)3