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;
}
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;
}
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;
}
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());
}
}
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");
}
}
Aggregations