use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class PoolManagerTest method testRefreshPoolsRemovesExpiredSubscriptionsAlongWithItsPoolsAndEnts.
@Test
public void testRefreshPoolsRemovesExpiredSubscriptionsAlongWithItsPoolsAndEnts() {
PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
Date expiredStart = TestUtil.createDate(2004, 5, 5);
Date expiredDate = TestUtil.createDate(2005, 5, 5);
List<Subscription> subscriptions = new ArrayList<>();
Owner owner = this.getOwner();
Product product = TestUtil.createProduct();
Subscription sub = TestUtil.createSubscription(owner, product);
sub.setStartDate(expiredStart);
sub.setEndDate(expiredDate);
sub.setId("123");
subscriptions.add(sub);
mockSubsList(subscriptions);
List<Pool> pools = new ArrayList<>();
Pool p = TestUtil.createPool(owner, product);
p.setId("test-pool");
p.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
p.setStartDate(expiredStart);
p.setEndDate(expiredDate);
p.setConsumed(1L);
pools.add(p);
when(mockPoolCurator.lockAndLoadByIds(anyCollection())).thenReturn(pools);
mockPoolsList(pools);
List<Entitlement> poolEntitlements = new ArrayList<>();
Entitlement ent = TestUtil.createEntitlement();
ent.setId("test-ent");
ent.setPool(p);
ent.setQuantity(1);
poolEntitlements.add(ent);
p.getEntitlements().addAll(poolEntitlements);
when(mockPoolCurator.getEntitlementIdsForPools(anyCollection())).thenReturn(Arrays.asList(ent.getId()));
CandlepinQuery<Entitlement> cqmockEnt = mock(CandlepinQuery.class);
when(cqmockEnt.list()).thenReturn(poolEntitlements);
when(cqmockEnt.iterator()).thenReturn(poolEntitlements.iterator());
when(entitlementCurator.listAllByIds(anyCollection())).thenReturn(cqmockEnt);
ValidationResult result = new ValidationResult();
when(preHelper.getResult()).thenReturn(result);
when(mockOwnerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
this.mockProducts(owner, product);
this.mockProductImport(owner, product);
this.mockContentImport(owner, new Content[] {});
CandlepinQuery<Pool> cqmock = mock(CandlepinQuery.class);
when(cqmock.list()).thenReturn(pools);
when(cqmock.iterator()).thenReturn(pools.iterator());
when(mockPoolCurator.listByOwnerAndType(eq(owner), any(PoolType.class))).thenReturn(cqmock);
cqmock = mock(CandlepinQuery.class);
when(cqmock.list()).thenReturn(Collections.<Pool>emptyList());
when(mockPoolCurator.getPoolsBySubscriptionIds(anyList())).thenReturn(cqmock);
this.manager.getRefresher(mockSubAdapter, mockOwnerAdapter).add(owner).run();
verify(mockPoolCurator).batchDelete(eq(pools), anyCollectionOf(String.class));
verify(entitlementCurator).batchDelete(eq(poolEntitlements));
}
use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class PoolManagerTest method testEntitleByProductsEmptyArray.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testEntitleByProductsEmptyArray() throws Exception {
Product product = TestUtil.createProduct();
List<Pool> pools = new ArrayList<>();
Pool pool1 = TestUtil.createPool(product);
pools.add(pool1);
Date now = new Date();
ValidationResult result = mock(ValidationResult.class);
// Setup an installed product for the consumer, we'll make the bind request
// with no products specified, so this should get used instead:
String[] installedPids = new String[] { product.getUuid() };
ComplianceStatus mockCompliance = new ComplianceStatus(now);
mockCompliance.addNonCompliantProduct(installedPids[0]);
when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class))).thenReturn(mockCompliance);
Page page = mock(Page.class);
when(page.getPageData()).thenReturn(pools);
when(mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), any(String.class), anyString(), anyString(), 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);
// Make the call but provide a null array of product IDs (simulates healing):
ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
Consumer consumer = TestUtil.createConsumer(ctype, owner);
AutobindData data = AutobindData.create(consumer, owner).on(now);
manager.entitleByProducts(data);
verify(autobindRules).selectBestPools(any(Consumer.class), eq(installedPids), any(List.class), eq(mockCompliance), any(String.class), any(Set.class), eq(false));
}
use of org.candlepin.policy.ValidationResult 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");
}
}
use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class EntitlerTest method fakeOutResult.
private ValidationResult fakeOutResult(String msg) {
ValidationResult result = new ValidationResult();
ValidationError err = new ValidationError(msg);
result.addError(err);
return result;
}
use of org.candlepin.policy.ValidationResult in project candlepin by candlepin.
the class EntitlerTest method bindByProductErrorTest.
private void bindByProductErrorTest(String msg) throws Exception {
try {
String[] pids = { "prod1", "prod2", "prod3" };
Map<String, ValidationResult> fakeResult = new HashMap<>();
fakeResult.put("blah", fakeOutResult(msg));
EntitlementRefusedException ere = new EntitlementRefusedException(fakeResult);
AutobindData data = AutobindData.create(consumer, owner).forProducts(pids);
when(pm.entitleByProducts(data)).thenThrow(ere);
entitler.bindByProducts(data);
} catch (EntitlementRefusedException e) {
fail(msg + ": threw unexpected error");
}
}
Aggregations