Search in sources :

Example 46 with Entitlement

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());
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) ValidationResult(org.candlepin.policy.ValidationResult) SourceSubscription(org.candlepin.model.SourceSubscription) Consumer(org.candlepin.model.Consumer) Matchers.anyCollection(org.mockito.Matchers.anyCollection) Collection(java.util.Collection) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) ConsumerType(org.candlepin.model.ConsumerType) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with Entitlement

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));
}
Also used : Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) CandlepinQuery(org.candlepin.model.CandlepinQuery) AutobindData(org.candlepin.resource.dto.AutobindData) Entitlement(org.candlepin.model.Entitlement) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with Entitlement

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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 49 with Entitlement

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());
}
Also used : Pool(org.candlepin.model.Pool) AutobindData(org.candlepin.resource.dto.AutobindData) Entitlement(org.candlepin.model.Entitlement) Date(java.util.Date) Test(org.junit.Test)

Example 50 with Entitlement

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());
}
Also used : Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Aggregations

Entitlement (org.candlepin.model.Entitlement)301 Test (org.junit.Test)201 Consumer (org.candlepin.model.Consumer)164 Pool (org.candlepin.model.Pool)125 LinkedList (java.util.LinkedList)84 Product (org.candlepin.model.Product)68 Date (java.util.Date)62 ArrayList (java.util.ArrayList)61 HashSet (java.util.HashSet)59 HashMap (java.util.HashMap)55 Owner (org.candlepin.model.Owner)44 PoolQuantity (org.candlepin.model.PoolQuantity)35 ConsumerType (org.candlepin.model.ConsumerType)34 List (java.util.List)30 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)24 Matchers.anyString (org.mockito.Matchers.anyString)17 Set (java.util.Set)16 Subscription (org.candlepin.model.dto.Subscription)16 ApiOperation (io.swagger.annotations.ApiOperation)15 ApiResponses (io.swagger.annotations.ApiResponses)15