Search in sources :

Example 26 with Subscription

use of org.candlepin.model.dto.Subscription 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 27 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class PoolManagerTest method testCreatePoolForSubscription.

@Test
public void testCreatePoolForSubscription() {
    Product product = TestUtil.createProduct();
    Subscription s = TestUtil.createSubscription(getOwner(), product);
    List<Pool> newPools = new LinkedList<>();
    Pool p = TestUtil.createPool(product);
    p.setSourceSubscription(new SourceSubscription(s.getId(), "master"));
    newPools.add(p);
    when(poolRulesMock.createAndEnrichPools(eq(s), any(List.class))).thenReturn(newPools);
    this.manager.createAndEnrichPools(s);
    verify(this.mockPoolCurator, times(1)).create(any(Pool.class));
}
Also used : SourceSubscription(org.candlepin.model.SourceSubscription) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 28 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class PoolManagerTest method mockSubsList.

private void mockSubsList(List<Subscription> subs) {
    List<String> subIds = new LinkedList<>();
    for (Subscription sub : subs) {
        subIds.add(sub.getId());
        when(mockSubAdapter.getSubscription(eq(sub.getId()))).thenReturn(sub);
    }
    when(mockSubAdapter.getSubscriptionIds(any(Owner.class))).thenReturn(subIds);
    when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subs);
}
Also used : Owner(org.candlepin.model.Owner) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) LinkedList(java.util.LinkedList)

Example 29 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class PoolManagerTest method derivedProvidedProductsCopiedOntoMasterPoolWhenCreatingNewPool.

@Test
public void derivedProvidedProductsCopiedOntoMasterPoolWhenCreatingNewPool() {
    Product product = TestUtil.createProduct();
    Product subProduct = TestUtil.createProduct();
    Product subProvidedProduct = TestUtil.createProduct();
    Subscription sub = TestUtil.createSubscription(owner, product);
    sub.setDerivedProduct(subProduct.toDTO());
    Set<ProductData> subProvided = new HashSet<>();
    subProvided.add(subProvidedProduct.toDTO());
    sub.setDerivedProvidedProducts(subProvided);
    this.mockProducts(owner, product, subProduct, subProvidedProduct);
    PoolRules pRules = new PoolRules(manager, mockConfig, entitlementCurator, mockOwnerProductCurator, mockProductCurator);
    List<Pool> pools = pRules.createAndEnrichPools(sub);
    assertEquals(1, pools.size());
    Pool resultPool = pools.get(0);
    assertEquals(1, resultPool.getDerivedProvidedProducts().size());
}
Also used : ProductData(org.candlepin.model.dto.ProductData) PoolRules(org.candlepin.policy.js.pool.PoolRules) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class PoolManagerTest method testCleanupExpiredPoolsReadOnlySubscriptions.

@Test
public void testCleanupExpiredPoolsReadOnlySubscriptions() {
    Pool p = createPoolWithEntitlements();
    p.setSubscriptionId("subid");
    List<Pool> pools = Arrays.asList(p);
    when(mockPoolCurator.lockAndLoadByIds(anyCollection())).thenReturn(pools);
    when(mockPoolCurator.listExpiredPools(anyInt())).thenReturn(pools);
    when(mockPoolCurator.entitlementsIn(p)).thenReturn(new ArrayList<>(p.getEntitlements()));
    Subscription sub = new Subscription();
    sub.setId(p.getSubscriptionId());
    when(mockSubAdapter.getSubscription(any(String.class))).thenReturn(sub);
    when(mockSubAdapter.isReadOnly()).thenReturn(true);
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
    ValidationResult result = new ValidationResult();
    when(preHelper.getResult()).thenReturn(result);
    manager.cleanupExpiredPools();
    // And the pool should be deleted:
    verify(mockPoolCurator).batchDelete(eq(pools), anySetOf(String.class));
    verify(mockSubAdapter, never()).getSubscription(any(String.class));
    verify(mockSubAdapter, never()).deleteSubscription(any(Subscription.class));
}
Also used : PreUnbindHelper(org.candlepin.policy.js.entitlement.PreUnbindHelper) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Aggregations

Subscription (org.candlepin.model.dto.Subscription)101 Test (org.junit.Test)77 Pool (org.candlepin.model.Pool)60 Product (org.candlepin.model.Product)48 SourceSubscription (org.candlepin.model.SourceSubscription)36 LinkedList (java.util.LinkedList)35 ArrayList (java.util.ArrayList)34 Owner (org.candlepin.model.Owner)33 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)27 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)18 Matchers.anyString (org.mockito.Matchers.anyString)18 Entitlement (org.candlepin.model.Entitlement)16 Date (java.util.Date)15 PoolType (org.candlepin.model.Pool.PoolType)13 ImportSubscriptionServiceAdapter (org.candlepin.service.impl.ImportSubscriptionServiceAdapter)13 List (java.util.List)11 OwnerServiceAdapter (org.candlepin.service.OwnerServiceAdapter)11 ConsumerType (org.candlepin.model.ConsumerType)10 ProductData (org.candlepin.model.dto.ProductData)9