Search in sources :

Example 1 with PreUnbindHelper

use of org.candlepin.policy.js.entitlement.PreUnbindHelper in project candlepin by candlepin.

the class PoolManagerTest method testCleanup.

@Test
public void testCleanup() throws Exception {
    Pool p = createPoolWithEntitlements();
    when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(p);
    when(mockPoolCurator.entitlementsIn(p)).thenReturn(new ArrayList<>(p.getEntitlements()));
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
    ValidationResult result = new ValidationResult();
    when(preHelper.getResult()).thenReturn(result);
    manager.deletePool(p);
    // And the pool should be deleted:
    verify(mockPoolCurator).delete(p);
    // Check that appropriate events were sent out:
    verify(eventFactory).poolDeleted(p);
    verify(mockEventSink, times(1)).queueEvent((Event) any());
}
Also used : PreUnbindHelper(org.candlepin.policy.js.entitlement.PreUnbindHelper) Pool(org.candlepin.model.Pool) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Example 2 with PreUnbindHelper

use of org.candlepin.policy.js.entitlement.PreUnbindHelper in project candlepin by candlepin.

the class PoolManagerTest method testRevokeAllEntitlements.

@Test
public void testRevokeAllEntitlements() {
    Consumer c = TestUtil.createConsumer(owner);
    Entitlement e1 = new Entitlement(pool, c, owner, 1);
    Entitlement e2 = new Entitlement(pool, c, owner, 1);
    List<Entitlement> entitlementList = new ArrayList<>();
    entitlementList.add(e1);
    entitlementList.add(e2);
    when(entitlementCurator.listByConsumer(eq(c))).thenReturn(entitlementList);
    when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool);
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
    ValidationResult result = new ValidationResult();
    when(preHelper.getResult()).thenReturn(result);
    int total = manager.revokeAllEntitlements(c);
    assertEquals(2, total);
    verify(entitlementCurator, times(1)).markDependentEntitlementsDirty(any(List.class));
// TODO assert batch revokes have been called
}
Also used : PreUnbindHelper(org.candlepin.policy.js.entitlement.PreUnbindHelper) Consumer(org.candlepin.model.Consumer) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) Entitlement(org.candlepin.model.Entitlement) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Example 3 with PreUnbindHelper

use of org.candlepin.policy.js.entitlement.PreUnbindHelper in project candlepin by candlepin.

the class PoolManagerTest method testRefreshPoolsRemovesOtherOwnerPoolsForSameSub.

@Test
public void testRefreshPoolsRemovesOtherOwnerPoolsForSameSub() {
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
    Owner other = new Owner("otherkey", "othername");
    List<Subscription> subscriptions = new ArrayList<>();
    Owner owner = this.getOwner();
    Product product = TestUtil.createProduct();
    product.setLocked(true);
    Subscription sub = TestUtil.createSubscription(owner, product);
    sub.setId("123");
    subscriptions.add(sub);
    mockSubsList(subscriptions);
    List<Pool> pools = new ArrayList<>();
    Pool p = TestUtil.copyFromSub(sub);
    p.setOwner(other);
    p.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
    p.setConsumed(1L);
    pools.add(p);
    when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(p);
    mockPoolsList(pools);
    List<Entitlement> poolEntitlements = new ArrayList<>();
    Entitlement ent = TestUtil.createEntitlement();
    ent.setPool(p);
    ent.setQuantity(1);
    poolEntitlements.add(ent);
    when(mockPoolCurator.entitlementsIn(eq(p))).thenReturn(poolEntitlements);
    ValidationResult result = new ValidationResult();
    when(preHelper.getResult()).thenReturn(result);
    when(mockOwnerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
    this.mockProducts(owner, product);
    this.mockProductImport(owner, product);
    this.mockContentImport(owner, new Content[] {});
    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);
    this.manager.getRefresher(mockSubAdapter, mockOwnerAdapter).add(owner).run();
    // The pool left over from the pre-migrated subscription should be deleted
    // and granted entitlements should be revoked
    List<Entitlement> entsToDelete = Arrays.asList(ent);
    verify(mockPoolCurator).delete(eq(p));
    verify(entitlementCurator).batchDelete(eq(entsToDelete));
    // Make sure pools that don't match the owner were removed from the list
    // They shouldn't cause us to attempt to update existing pools when we
    // haven't created them in the first place
    ArgumentCaptor<Pool> argPool = ArgumentCaptor.forClass(Pool.class);
    verify(poolRulesMock).createAndEnrichPools(argPool.capture(), any(List.class));
    TestUtil.assertPoolsAreEqual(TestUtil.copyFromSub(sub), argPool.getValue());
}
Also used : PreUnbindHelper(org.candlepin.policy.js.entitlement.PreUnbindHelper) Owner(org.candlepin.model.Owner) PoolType(org.candlepin.model.Pool.PoolType) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) ValidationResult(org.candlepin.policy.ValidationResult) SourceSubscription(org.candlepin.model.SourceSubscription) 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) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 4 with PreUnbindHelper

use of org.candlepin.policy.js.entitlement.PreUnbindHelper 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)

Example 5 with PreUnbindHelper

use of org.candlepin.policy.js.entitlement.PreUnbindHelper in project candlepin by candlepin.

the class PoolManagerTest method testBatchRevokeCleansUpCorrectPoolsWithSourceEnt.

@Test
public void testBatchRevokeCleansUpCorrectPoolsWithSourceEnt() throws Exception {
    Consumer c = TestUtil.createConsumer(owner);
    Pool pool2 = TestUtil.createPool(owner, product);
    Entitlement e = new Entitlement(pool, c, owner, 1);
    Entitlement e2 = new Entitlement(pool2, c, owner, 1);
    Entitlement e3 = new Entitlement(pool2, c, owner, 1);
    List<Entitlement> entsToDelete = new ArrayList<>();
    entsToDelete.add(e);
    entsToDelete.add(e2);
    List<Pool> poolsWithSource = createPoolsWithSourceEntitlement(e, product);
    poolsWithSource.get(0).getEntitlements().add(e3);
    Set<Pool> poolsWithSourceAsSet = new HashSet<>(poolsWithSource);
    when(mockPoolCurator.listBySourceEntitlements(entsToDelete)).thenReturn(poolsWithSourceAsSet);
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
    ValidationResult result = new ValidationResult();
    when(preHelper.getResult()).thenReturn(result);
    when(mockConfig.getBoolean(ConfigProperties.STANDALONE)).thenReturn(true);
    when(mockPoolCurator.lockAndLoad(eq(pool))).thenReturn(pool);
    when(mockPoolCurator.lockAndLoad(eq(pool2))).thenReturn(pool2);
    manager.revokeEntitlements(entsToDelete);
    entsToDelete.add(e3);
    verify(entitlementCurator).batchDelete(eq(entsToDelete));
    verify(mockPoolCurator).batchDelete(eq(poolsWithSourceAsSet), anySetOf(String.class));
}
Also used : PreUnbindHelper(org.candlepin.policy.js.entitlement.PreUnbindHelper) Consumer(org.candlepin.model.Consumer) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Entitlement(org.candlepin.model.Entitlement) ValidationResult(org.candlepin.policy.ValidationResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Pool (org.candlepin.model.Pool)8 ValidationResult (org.candlepin.policy.ValidationResult)8 PreUnbindHelper (org.candlepin.policy.js.entitlement.PreUnbindHelper)8 Test (org.junit.Test)8 Entitlement (org.candlepin.model.Entitlement)5 ArrayList (java.util.ArrayList)4 SourceSubscription (org.candlepin.model.SourceSubscription)4 Subscription (org.candlepin.model.dto.Subscription)4 Matchers.anyString (org.mockito.Matchers.anyString)4 LinkedList (java.util.LinkedList)3 List (java.util.List)2 Consumer (org.candlepin.model.Consumer)2 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)2 Owner (org.candlepin.model.Owner)2 PoolType (org.candlepin.model.Pool.PoolType)2 Product (org.candlepin.model.Product)2 Matchers.anyList (org.mockito.Matchers.anyList)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 CandlepinQuery (org.candlepin.model.CandlepinQuery)1