Search in sources :

Example 1 with ValidationError

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));
    }
}
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 2 with ValidationError

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());
}
Also used : Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) ConsumerCapability(org.candlepin.model.ConsumerCapability) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ValidationError

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());
}
Also used : Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Example 4 with ValidationError

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());
}
Also used : Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Example 5 with ValidationError

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;
}
Also used : Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) ConsumerType(org.candlepin.model.ConsumerType)

Aggregations

ValidationError (org.candlepin.policy.ValidationError)15 Pool (org.candlepin.model.Pool)14 ValidationResult (org.candlepin.policy.ValidationResult)13 Consumer (org.candlepin.model.Consumer)11 Product (org.candlepin.model.Product)11 Test (org.junit.Test)11 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 ConsumerCapability (org.candlepin.model.ConsumerCapability)3 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Entitlement (org.candlepin.model.Entitlement)2 PoolQuantity (org.candlepin.model.PoolQuantity)2 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)1