use of org.candlepin.model.CandlepinQuery 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.model.CandlepinQuery 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.model.CandlepinQuery in project candlepin by candlepin.
the class PoolManagerTest method mockPoolsList.
private void mockPoolsList(List<Pool> pools) {
List<Pool> floating = new LinkedList<>();
subToPools = new HashMap<>();
for (Pool pool : pools) {
String subid = pool.getSubscriptionId();
if (subid != null) {
if (!subToPools.containsKey(subid)) {
subToPools.put(subid, new LinkedList<>());
}
subToPools.get(subid).add(pool);
} else {
floating.add(pool);
}
}
for (String subid : subToPools.keySet()) {
CandlepinQuery cqmock = mock(CandlepinQuery.class);
when(cqmock.list()).thenReturn(subToPools.get(subid));
when(mockPoolCurator.getPoolsBySubscriptionId(eq(subid))).thenReturn(cqmock);
}
when(mockPoolCurator.getOwnersFloatingPools(any(Owner.class))).thenReturn(floating);
when(mockPoolCurator.getPoolsFromBadSubs(any(Owner.class), any(Collection.class))).thenAnswer(new Answer<List<Pool>>() {
@SuppressWarnings("unchecked")
@Override
public List<Pool> answer(InvocationOnMock iom) throws Throwable {
Collection<String> subIds = (Collection<String>) iom.getArguments()[1];
List<Pool> results = new LinkedList<>();
for (Entry<String, List<Pool>> entry : PoolManagerTest.subToPools.entrySet()) {
for (Pool pool : entry.getValue()) {
if (!subIds.contains(pool.getSubscriptionId())) {
results.add(pool);
}
}
}
return results;
}
});
}
use of org.candlepin.model.CandlepinQuery in project candlepin by candlepin.
the class PoolManagerTest method testRefreshPoolsDeletesOrphanedPools.
@Test
public void testRefreshPoolsDeletesOrphanedPools() {
List<Subscription> subscriptions = new ArrayList<>();
List<Pool> pools = new ArrayList<>();
Product product = TestUtil.createProduct();
Pool p = TestUtil.createPool(product);
p.setSourceSubscription(new SourceSubscription("112", "master"));
pools.add(p);
mockSubsList(subscriptions);
mockPoolsList(pools);
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.mockProductImport(owner, product);
this.mockContentImport(owner, new Content[] {});
Owner owner = getOwner();
when(mockOwnerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
this.manager.getRefresher(mockSubAdapter, mockOwnerAdapter).add(owner).run();
List<Pool> poolsToDelete = Arrays.asList(p);
verify(this.manager).deletePools(eq(poolsToDelete));
}
use of org.candlepin.model.CandlepinQuery in project candlepin by candlepin.
the class EntitlerTest method testRevokesLapsedUnmappedGuestEntitlementsOnAutoHeal.
@Test
public void testRevokesLapsedUnmappedGuestEntitlementsOnAutoHeal() throws Exception {
Owner owner1 = new Owner("o1");
owner1.setId(TestUtil.randomString());
when(ownerCurator.findOwnerById(eq(owner1.getId()))).thenReturn(owner1);
Product product = TestUtil.createProduct();
Pool p1 = TestUtil.createPool(owner1, product);
p1.setAttribute(Pool.Attributes.UNMAPPED_GUESTS_ONLY, "true");
Date thirtySixHoursAgo = new Date(new Date().getTime() - 36L * 60L * 60L * 1000L);
Date twelveHoursAgo = new Date(new Date().getTime() - 12L * 60L * 60L * 1000L);
Consumer c;
c = TestUtil.createConsumer(owner1);
c.setCreated(thirtySixHoursAgo);
c.setFact("virt.uuid", "1");
Entitlement e1 = TestUtil.createEntitlement(owner1, c, p1, null);
e1.setEndDateOverride(twelveHoursAgo);
Set<Entitlement> entitlementSet1 = new HashSet<>();
entitlementSet1.add(e1);
p1.setEntitlements(entitlementSet1);
CandlepinQuery cqmock = mock(CandlepinQuery.class);
when(cqmock.iterator()).thenReturn(Arrays.asList(e1).iterator());
when(entitlementCurator.findByPoolAttribute(eq(c), eq("unmapped_guests_only"), eq("true"))).thenReturn(cqmock);
String[] pids = { product.getId(), "prod2" };
when(cc.findByUuid(eq("abcd1234"))).thenReturn(c);
entitler.bindByProducts(pids, "abcd1234", null, null);
AutobindData data = AutobindData.create(c, owner1).forProducts(pids);
verify(pm).entitleByProducts(eq(data));
verify(pm).revokeEntitlements(Arrays.asList(e1));
}
Aggregations