use of org.candlepin.policy.ValidationError 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));
}
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method preEntitlementNoRamCapableBindError.
@Test
public void preEntitlementNoRamCapableBindError() {
// Test with sockets to make sure that they are skipped.
Consumer c = this.createMockConsumer(true);
c.setFact("memory.memtotal", "2000000");
Set<ConsumerCapability> caps = new HashSet<>();
c.setCapabilities(caps);
Product prod = TestUtil.createProduct();
prod.setAttribute(Product.Attributes.RAM, "2");
Pool p = TestUtil.createPool(prod);
p.setId("poolId");
ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.BIND);
assertNotNull(results);
assertEquals(0, results.getWarnings().size());
ValidationError error = results.getErrors().get(0);
assertEquals("rulefailed.ram.unsupported.by.consumer", error.getResourceKey());
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method preEntitlementShouldNotAllowConsumptionFromDerivedPools.
@Test
public void preEntitlementShouldNotAllowConsumptionFromDerivedPools() {
Consumer c = this.createMockConsumer(true);
Product prod = TestUtil.createProduct();
Pool p = TestUtil.createPool(prod);
p.setAttribute(Product.Attributes.VIRT_ONLY, "true");
p.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
p.setId("poolId");
ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.BIND);
assertNotNull(results);
assertEquals(1, results.getErrors().size());
ValidationError error = results.getErrors().get(0);
assertEquals("pool.not.available.to.manifest.consumers", error.getResourceKey());
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method preEntitlementShouldNotAllowListOfRequiresHostPools.
@Test
public void preEntitlementShouldNotAllowListOfRequiresHostPools() {
Consumer c = this.createMockConsumer(true);
Product prod = TestUtil.createProduct();
Pool p = TestUtil.createPool(prod);
p.setAttribute(Product.Attributes.VIRT_ONLY, "true");
p.setAttribute(Pool.Attributes.REQUIRES_HOST, "true");
p.setId("poolId");
ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.LIST_POOLS);
assertNotNull(results);
assertEquals(1, results.getErrors().size());
ValidationError error = results.getErrors().get(0);
assertEquals("pool.not.available.to.manifest.consumers", error.getResourceKey());
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class EntitlementRules method update.
@Override
public ValidationResult update(Consumer consumer, Entitlement entitlement, Integer change) {
ValidationResult result = new ValidationResult();
ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
if (!ctype.isManifest() && !ctype.isType(ConsumerTypeEnum.SHARE)) {
Pool pool = entitlement.getPool();
// multi ent check
if (!"yes".equalsIgnoreCase(pool.getProductAttributeValue(Pool.Attributes.MULTI_ENTITLEMENT)) && entitlement.getQuantity() + change > 1) {
result.addError(new ValidationError(EntitlementRulesTranslator.PoolErrorKeys.MULTI_ENTITLEMENT_UNSUPPORTED));
}
if (!consumer.isGuest()) {
String multiplier = pool.getProductAttributeValue(Product.Attributes.INSTANCE_MULTIPLIER);
if (multiplier != null) {
int instanceMultiplier = Integer.parseInt(multiplier);
// quantity should be divisible by multiplier
if ((entitlement.getQuantity() + change) % instanceMultiplier != 0) {
result.addError(new ValidationError(EntitlementRulesTranslator.PoolErrorKeys.QUANTITY_MISMATCH));
}
}
}
}
finishValidation(result, entitlement.getPool(), change);
return result;
}
Aggregations