Search in sources :

Example 11 with SourceSubscription

use of org.candlepin.model.SourceSubscription 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 12 with SourceSubscription

use of org.candlepin.model.SourceSubscription 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 13 with SourceSubscription

use of org.candlepin.model.SourceSubscription 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 14 with SourceSubscription

use of org.candlepin.model.SourceSubscription 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));
}
Also used : Owner(org.candlepin.model.Owner) SourceSubscription(org.candlepin.model.SourceSubscription) PoolType(org.candlepin.model.Pool.PoolType) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) CandlepinQuery(org.candlepin.model.CandlepinQuery) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Test(org.junit.Test)

Example 15 with SourceSubscription

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

the class RefresherTest method testPoolOnlyExaminedOnceProductAndOwner.

@Test
public void testPoolOnlyExaminedOnceProductAndOwner() {
    Owner owner = TestUtil.createOwner();
    Product product = mock(Product.class);
    ProductData productData = mock(ProductData.class);
    when(product.getUuid()).thenReturn("product id");
    when(product.toDTO()).thenReturn(productData);
    Pool pool = new Pool();
    pool.setSourceSubscription(new SourceSubscription("subId", "master"));
    pool.setOwner(owner);
    Subscription subscription = new Subscription();
    subscription.setId("subId");
    subscription.setOwner(owner);
    List<Pool> pools = new ArrayList<>();
    pools.add(pool);
    List<Subscription> subscriptions = new ArrayList<>();
    subscriptions.add(subscription);
    when(subAdapter.getSubscriptions(eq(productData))).thenReturn(subscriptions);
    when(subAdapter.getSubscriptions(owner)).thenReturn(subscriptions);
    when(subAdapter.getSubscription("subId")).thenReturn(subscription);
    when(poolManager.lookupBySubscriptionId(owner, "subId")).thenReturn(pools);
    refresher.add(owner);
    refresher.add(product);
    refresher.run();
    verify(poolManager, times(1)).refreshPoolsWithRegeneration(eq(subAdapter), eq(owner), eq(false));
    verify(poolManager, times(0)).updatePoolsForMasterPool(any(List.class), any(Pool.class), eq(pool.getQuantity()), eq(false), any(Map.class));
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) SourceSubscription(org.candlepin.model.SourceSubscription) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Pool (org.candlepin.model.Pool)31 SourceSubscription (org.candlepin.model.SourceSubscription)31 Product (org.candlepin.model.Product)23 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)19 Owner (org.candlepin.model.Owner)19 Subscription (org.candlepin.model.dto.Subscription)18 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)15 LinkedList (java.util.LinkedList)14 PoolType (org.candlepin.model.Pool.PoolType)10 Entitlement (org.candlepin.model.Entitlement)9 HashSet (java.util.HashSet)8 List (java.util.List)7 Branding (org.candlepin.model.Branding)7 HashMap (java.util.HashMap)6 Consumer (org.candlepin.model.Consumer)6 Matchers.anyList (org.mockito.Matchers.anyList)6 Map (java.util.Map)5 Date (java.util.Date)4 CandlepinQuery (org.candlepin.model.CandlepinQuery)4