use of org.candlepin.policy.ValidationResult 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.ValidationResult 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);
}
use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class PoolManagerTest method testCleanupExpiredPoolsReadOnlySubscriptions.
@Test
public void testCleanupExpiredPoolsReadOnlySubscriptions() {
Pool p = createPoolWithEntitlements();
p.setSubscriptionId("subid");
List<Pool> pools = Arrays.asList(p);
when(mockPoolCurator.lockAndLoadByIds(anyCollection())).thenReturn(pools);
when(mockPoolCurator.listExpiredPools(anyInt())).thenReturn(pools);
when(mockPoolCurator.entitlementsIn(p)).thenReturn(new ArrayList<>(p.getEntitlements()));
Subscription sub = new Subscription();
sub.setId(p.getSubscriptionId());
when(mockSubAdapter.getSubscription(any(String.class))).thenReturn(sub);
when(mockSubAdapter.isReadOnly()).thenReturn(true);
PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
ValidationResult result = new ValidationResult();
when(preHelper.getResult()).thenReturn(result);
manager.cleanupExpiredPools();
// And the pool should be deleted:
verify(mockPoolCurator).batchDelete(eq(pools), anySetOf(String.class));
verify(mockSubAdapter, never()).getSubscription(any(String.class));
verify(mockSubAdapter, never()).deleteSubscription(any(Subscription.class));
}
use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class PoolManagerTest method testDeleteExcessEntitlements.
@Test
public void testDeleteExcessEntitlements() throws EntitlementRefusedException {
ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
Consumer consumer = TestUtil.createConsumer(ctype, owner);
Subscription sub = TestUtil.createSubscription(owner, product);
sub.setId("testing-subid");
pool.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
Pool derivedPool = TestUtil.createPool(owner, product, 1);
derivedPool.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
derivedPool.setSourceSubscription(new SourceSubscription(sub.getId(), "der"));
derivedPool.setConsumed(3L);
derivedPool.setId("derivedPool");
Entitlement masterEnt = new Entitlement(pool, consumer, owner, 5);
Entitlement derivedEnt = new Entitlement(derivedPool, consumer, owner, 1);
derivedEnt.setId("1");
Set<Entitlement> ents = new HashSet<>();
ents.add(derivedEnt);
derivedPool.setEntitlements(ents);
// before
assertEquals(3, derivedPool.getConsumed().intValue());
assertEquals(1, derivedPool.getEntitlements().size());
Collection<Pool> overPools = Collections.singletonList(derivedPool);
when(mockPoolCurator.lockAndLoad(any(Collection.class))).thenReturn(overPools);
when(mockPoolCurator.lockAndLoad(pool)).thenReturn(pool);
when(enforcerMock.update(any(Consumer.class), any(Entitlement.class), any(Integer.class))).thenReturn(new ValidationResult());
when(mockPoolCurator.lookupOversubscribedBySubscriptionIds(any(String.class), anyMap())).thenReturn(Collections.singletonList(derivedPool));
when(mockPoolCurator.retrieveOrderedEntitlementsOf(anyListOf(Pool.class))).thenReturn(Collections.singletonList(derivedEnt));
when(mockPoolCurator.lockAndLoad(eq(derivedPool))).thenReturn(derivedPool);
pool.setId("masterpool");
manager.adjustEntitlementQuantity(consumer, masterEnt, 3);
Class<List<Entitlement>> listClass = (Class<List<Entitlement>>) (Class) ArrayList.class;
ArgumentCaptor<List<Entitlement>> arg = ArgumentCaptor.forClass(listClass);
verify(entitlementCurator).batchDelete(arg.capture());
List<Entitlement> entsDeleted = arg.getValue();
assertThat(entsDeleted, hasItem(derivedEnt));
assertEquals(2, derivedPool.getConsumed().intValue());
}
use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class ActivationKeyRulesTest method testActivationKeyRulesSufficientQuantity.
/*
* the number of available subscriptions shouldn't matter, only
* the total number
*/
@Test
public void testActivationKeyRulesSufficientQuantity() {
ActivationKey key = new ActivationKey();
Pool pool = genPool();
pool.setQuantity(5L);
pool.setConsumed(4L);
// Attempting to overconsume the pool
ValidationResult result = actKeyRules.runPreActKey(key, pool, new Long(2));
assertTrue(result.getWarnings().isEmpty());
assertTrue(result.getErrors().isEmpty());
}
Aggregations