use of org.candlepin.model.Entitlement 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.model.Entitlement 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));
}
use of org.candlepin.model.Entitlement in project candlepin by candlepin.
the class EntitlerTest method bindByPool.
@Test
public void bindByPool() throws EntitlementRefusedException {
String poolid = "pool10";
Pool pool = mock(Pool.class);
Entitlement ent = mock(Entitlement.class);
List<Entitlement> eList = new ArrayList<>();
eList.add(ent);
when(pm.find(eq(poolid))).thenReturn(pool);
Map<String, Integer> pQs = new HashMap<>();
pQs.put(poolid, 1);
when(pm.entitleByPools(eq(consumer), eq(pQs))).thenReturn(eList);
List<Entitlement> ents = entitler.bindByPoolQuantity(consumer, poolid, 1);
assertNotNull(ents);
assertEquals(ent, ents.get(0));
}
use of org.candlepin.model.Entitlement in project candlepin by candlepin.
the class PoolManagerFunctionalTest method testQuantityCheck.
@Test
public void testQuantityCheck() throws Exception {
Pool monitoringPool = poolCurator.listByOwnerAndProduct(o, monitoring.getId()).get(0);
assertEquals(Long.valueOf(5), monitoringPool.getQuantity());
AutobindData data = AutobindData.create(parentSystem, o).on(new Date()).forProducts(new String[] { monitoring.getId() });
for (int i = 0; i < 5; i++) {
List<Entitlement> entitlements = poolManager.entitleByProducts(data);
assertEquals(1, entitlements.size());
}
// The cert should specify 5 monitoring entitlements, taking a 6th should fail:
assertEquals(0, poolManager.entitleByProducts(data).size());
assertEquals(Long.valueOf(5), monitoringPool.getConsumed());
}
use of org.candlepin.model.Entitlement in project candlepin by candlepin.
the class PoolManagerFunctionalTest method testDevPoolBatchBind.
@Test
public void testDevPoolBatchBind() throws EntitlementRefusedException {
Owner owner = createOwner();
Product p = TestUtil.createProduct("test-product", "Test Product");
productCurator.create(p);
Consumer devSystem = new Consumer("dev", "user", owner, systemType);
devSystem.setFact("dev_sku", p.getId());
devSystem.addInstalledProduct(new ConsumerInstalledProduct(p));
consumerCurator.create(devSystem);
Pool pool1 = createPool(owner, p, 10L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
pool1.setAttribute(Pool.Attributes.DEVELOPMENT_POOL, "true");
pool1.setAttribute(Pool.Attributes.REQUIRES_CONSUMER, devSystem.getUuid());
poolCurator.create(pool1);
Pool pool2 = createPool(owner, p, 10L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
poolCurator.create(pool2);
Map<String, Integer> poolQuantities = new HashMap<>();
poolQuantities.put(pool1.getId(), 1);
poolQuantities.put(pool2.getId(), 1);
List<Entitlement> results = poolManager.entitleByPools(devSystem, poolQuantities);
assertEquals(2, results.size());
assertTrue(results.get(0).getPool() == pool1 || results.get(0).getPool() == pool2);
assertTrue(results.get(1).getPool() == pool1 || results.get(1).getPool() == pool2);
pool1 = poolCurator.find(pool1.getId());
pool2 = poolCurator.find(pool2.getId());
assertEquals(1, pool1.getConsumed().intValue());
assertEquals(1, pool2.getConsumed().intValue());
}
Aggregations