Search in sources :

Example 41 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class PoolManagerTest method testDeleteExcessEntitlementsBatch.

@Test
public void testDeleteExcessEntitlementsBatch() 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"));
    final 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");
    Entitlement derivedEnt2 = new Entitlement(derivedPool, consumer, owner, 1);
    derivedEnt2.setId("2");
    final Pool derivedPool2 = TestUtil.createPool(owner, product, 1);
    derivedPool2.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    derivedPool2.setSourceSubscription(new SourceSubscription(sub.getId(), "der"));
    derivedPool2.setConsumed(2L);
    derivedPool2.setId("derivedPool2");
    Entitlement derivedEnt3 = new Entitlement(derivedPool2, consumer, owner, 1);
    derivedEnt3.setId("3");
    Set<Entitlement> ents = new HashSet<>();
    ents.add(derivedEnt);
    ents.add(derivedEnt2);
    derivedPool.setEntitlements(ents);
    Set<Entitlement> ents2 = new HashSet<>();
    ents2.add(derivedEnt3);
    derivedPool2.setEntitlements(ents2);
    Pool derivedPool3 = TestUtil.createPool(owner, product, 1);
    derivedPool3.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    derivedPool3.setSourceSubscription(new SourceSubscription(sub.getId(), "der"));
    derivedPool3.setConsumed(2L);
    derivedPool3.setId("derivedPool3");
    // before
    assertEquals(3, derivedPool.getConsumed().intValue());
    assertEquals(2, derivedPool2.getConsumed().intValue());
    assertEquals(2, derivedPool3.getConsumed().intValue());
    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(Arrays.asList(derivedPool, derivedPool2, derivedPool3));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(eq(Arrays.asList(derivedPool)))).thenReturn(Arrays.asList(derivedEnt, derivedEnt2));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(eq(Arrays.asList(derivedPool2)))).thenReturn(Arrays.asList(derivedEnt3));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(eq(Arrays.asList(derivedPool3)))).thenReturn(new ArrayList<>());
    Collection<Pool> overPools = new ArrayList<Pool>() {

        {
            add(derivedPool);
            add(derivedPool2);
        }
    };
    when(mockPoolCurator.lockAndLoad(any(Collection.class))).thenReturn(overPools);
    when(mockPoolCurator.lockAndLoad(eq(derivedPool))).thenReturn(derivedPool);
    when(mockPoolCurator.lockAndLoad(eq(derivedPool2))).thenReturn(derivedPool2);
    when(mockPoolCurator.lockAndLoad(eq(derivedPool3))).thenReturn(derivedPool3);
    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, hasItems(derivedEnt, derivedEnt2, derivedEnt3));
    assertEquals(1, derivedPool.getConsumed().intValue());
    assertEquals(1, derivedPool2.getConsumed().intValue());
    assertEquals(2, derivedPool3.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 42 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class PoolManagerTest method createPoolWithEntitlements.

private Pool createPoolWithEntitlements() {
    Pool newPool = TestUtil.createPool(owner, product);
    Entitlement e1 = new Entitlement(newPool, TestUtil.createConsumer(owner), owner, 1);
    e1.setId("1");
    Entitlement e2 = new Entitlement(newPool, TestUtil.createConsumer(owner), owner, 1);
    e2.setId("2");
    newPool.getEntitlements().add(e1);
    newPool.getEntitlements().add(e2);
    return newPool;
}
Also used : Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement)

Example 43 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class PoolManagerTest method expiredEntitlementEvent.

@Test
public void expiredEntitlementEvent() {
    Date now = new Date();
    Product p = TestUtil.createProduct();
    p.setAttribute(Product.Attributes.HOST_LIMITED, "true");
    p.setAttribute(Product.Attributes.VIRT_LIMIT, "unlimited");
    Consumer guest = TestUtil.createConsumer(owner);
    guest.setFact("virt.is_guest", "true");
    guest.addInstalledProduct(new ConsumerInstalledProduct(p));
    Pool pool = TestUtil.createPool(owner, p);
    pool.setAttribute(Pool.Attributes.UNMAPPED_GUESTS_ONLY, "true");
    pool.setAttribute(Pool.Attributes.VIRT_ONLY, "true");
    pool.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    pool.setAttribute(Pool.Attributes.PHYSICAL_ONLY, "false");
    pool.setAttribute(Product.Attributes.VIRT_LIMIT, "0");
    pool.setStartDate(new Date(now.getTime() - (1000 * 60 * 60 * 24 * 2)));
    Entitlement ent = TestUtil.createEntitlement(owner, guest, pool, null);
    ent.setEndDateOverride(new Date(now.getTime() - (1000 * 60 * 60 * 24 * 1)));
    ent.setId("test-ent-id");
    ent.setQuantity(1);
    Set<Entitlement> entitlements = new HashSet<>();
    entitlements.add(ent);
    pool.setEntitlements(entitlements);
    Event event = new Event();
    event.setConsumerUuid(guest.getUuid());
    event.setOwnerId(owner.getId());
    event.setTarget(Target.ENTITLEMENT);
    event.setType(Type.EXPIRED);
    when(eventFactory.entitlementExpired(eq(ent))).thenReturn(event);
    when(mockPoolCurator.lockAndLoad(eq(pool))).thenReturn(pool);
    manager.revokeEntitlement(ent);
    String message = event.getMessageText();
    assertNotNull(message);
    message = message.split(": ")[1];
    assertEquals(message, i18n.tr("Unmapped guest entitlement expired without establishing a host/guest mapping."));
}
Also used : Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Event(org.candlepin.audit.Event) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Entitlement(org.candlepin.model.Entitlement) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with Entitlement

use of org.candlepin.model.Entitlement 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));
    }
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Page(org.candlepin.common.paging.Page) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ValidationResult(org.candlepin.policy.ValidationResult) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) AutobindData(org.candlepin.resource.dto.AutobindData) ValidationError(org.candlepin.policy.ValidationError) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) Date(java.util.Date) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 45 with Entitlement

use of org.candlepin.model.Entitlement 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);
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Page(org.candlepin.common.paging.Page) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ValidationResult(org.candlepin.policy.ValidationResult) Date(java.util.Date) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) AutobindData(org.candlepin.resource.dto.AutobindData) ConsumerType(org.candlepin.model.ConsumerType) 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