@Test
  public void refreshPoolsCleanupPoolThatLostVirtLimit() {
    List<Subscription> subscriptions = Util.newList();
    List<Pool> pools = Util.newList();
    Subscription s = TestUtil.createSubscription(getOwner(), TestUtil.createProduct());
    s.setId("01923");
    subscriptions.add(s);
    Pool p = TestUtil.createPool(s.getProduct());
    p.setSubscriptionId(s.getId());
    p.setAttribute(PoolManager.DELETE_FLAG, "true");
    pools.add(p);

    when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions);
    when(mockPoolCurator.listAvailableEntitlementPools(
            any(Consumer.class),
            any(Owner.class),
            anyString(),
            any(Date.class),
            anyBoolean(),
            anyBoolean()))
        .thenReturn(pools);

    List<PoolUpdate> updates = new LinkedList();
    updates.add(new PoolUpdate(p, false, true, false));
    when(poolRulesMock.updatePools(s, pools)).thenReturn(updates);

    this.manager.getRefresher().add(getOwner()).run();
    verify(this.mockPoolCurator, times(1)).delete(any(Pool.class));
  }
  @Test
  public void refreshPoolsCreatingPoolsForExistingSubscriptions() {
    List<Subscription> subscriptions = Util.newList();
    List<Pool> pools = Util.newList();
    Subscription s = TestUtil.createSubscription(getOwner(), TestUtil.createProduct());
    subscriptions.add(s);
    when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions);
    when(mockPoolCurator.listAvailableEntitlementPools(
            any(Consumer.class),
            any(Owner.class),
            anyString(),
            any(Date.class),
            anyBoolean(),
            anyBoolean()))
        .thenReturn(pools);

    List<Pool> newPools = new LinkedList<Pool>();
    Pool p = TestUtil.createPool(s.getProduct());
    p.setSubscriptionId(s.getId());
    newPools.add(p);
    when(poolRulesMock.createPools(s)).thenReturn(newPools);

    this.manager.getRefresher().add(getOwner()).run();
    verify(this.mockPoolCurator, times(1)).create(any(Pool.class));
  }
  private void verifyAndAssertForAllChanges(Subscription s, Pool p, int expectedEventCount) {
    verify(mockPoolCurator).retrieveFreeEntitlementsOfPool(any(Pool.class), eq(true));
    verify(mockEventSink, times(expectedEventCount)).sendEvent(any(Event.class));

    assertEquals(s.getQuantity(), p.getQuantity());
    assertEquals(s.getEndDate(), p.getEndDate());
    assertEquals(s.getStartDate(), p.getStartDate());
  }
 private Set<Product> getDerivedProductsForDistributor(Subscription sub, Entitlement ent) {
   Set<Product> derivedProducts = new HashSet<Product>();
   boolean derived = ent.getPool().hasAttribute("pool_derived");
   if (!derived && ent.getConsumer().getType().isManifest() && sub.getDerivedProduct() != null) {
     derivedProducts.add(sub.getDerivedProduct());
     derivedProducts.addAll(sub.getDerivedProvidedProducts());
   }
   return derivedProducts;
 }
  @Test
  public void testCreatePoolForSubscription() {
    final Subscription s = TestUtil.createSubscription(getOwner(), TestUtil.createProduct());

    List<Pool> newPools = new LinkedList<Pool>();
    Pool p = TestUtil.createPool(s.getProduct());
    p.setSubscriptionId(s.getId());
    newPools.add(p);
    when(poolRulesMock.createPools(s)).thenReturn(newPools);

    this.manager.createPoolsForSubscription(s);
    verify(this.mockPoolCurator, times(1)).create(any(Pool.class));
  }
  @Test
  public void testRefreshPoolsRemovesExpiredSubscriptionsAlongWithItsPoolsAndEnts() {
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);

    Date expiredStart = TestUtil.createDate(2004, 5, 5);
    Date expiredDate = TestUtil.createDate(2005, 5, 5);

    List<Subscription> subscriptions = Util.newList();

    Subscription sub = TestUtil.createSubscription(getOwner(), TestUtil.createProduct());
    sub.setStartDate(expiredStart);
    sub.setEndDate(expiredDate);
    sub.setId("123");
    subscriptions.add(sub);

    when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions);

    List<Pool> pools = Util.newList();
    Pool p = TestUtil.createPool(sub.getOwner(), sub.getProduct());
    p.setSubscriptionId(sub.getId());
    p.setStartDate(expiredStart);
    p.setEndDate(expiredDate);
    p.setConsumed(1L);
    pools.add(p);

    when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(p);

    when(mockPoolCurator.listAvailableEntitlementPools(
            any(Consumer.class),
            any(Owner.class),
            anyString(),
            any(Date.class),
            anyBoolean(),
            anyBoolean()))
        .thenReturn(pools);

    List<Entitlement> poolEntitlements = Util.newList();
    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);

    this.manager.getRefresher().add(sub.getOwner()).run();

    verify(mockSubAdapter).deleteSubscription(eq(sub));
    verify(mockPoolCurator).delete(eq(p));

    // Verify the entitlement was removed.
    verify(entCertAdapterMock).revokeEntitlementCertificates(eq(ent));
    verify(entitlementCurator).delete(eq(ent));
  }
  @Test
  public void testNonLazyRegenerate() throws Exception {
    Subscription s = TestUtil.createSubscription(getOwner(), product);
    s.setId("testSubId");
    pool.setSubscriptionId(s.getId());
    Entitlement e =
        new Entitlement(
            pool, TestUtil.createConsumer(o), pool.getStartDate(), pool.getEndDate(), 1);
    e.setDirty(true);

    when(mockSubAdapter.getSubscription(pool.getSubscriptionId())).thenReturn(s);

    manager.regenerateCertificatesOf(e, false, false);
    assertFalse(e.getDirty());

    verify(entCertAdapterMock).revokeEntitlementCertificates(e);
    verify(entCertAdapterMock).generateEntitlementCert(eq(e), eq(s), eq(product));
    verify(mockEventSink, times(1)).sendEvent(any(Event.class));
  }
 private Set<Product> getProvidedProducts(Pool pool, Subscription sub) {
   Set<Product> providedProducts = new HashSet<Product>();
   // TODO: eliminate the use of subscription here by looking up products in a batch
   // somehow, and we can eliminate all use of subscriptions during bind.
   if (sub != null) {
     // need to use the sub provided products if creating an
     // entitlement for derived pool who's sub specifies a
     // sub product.
     boolean derived = pool.hasAttribute("pool_derived");
     providedProducts =
         derived && sub.getDerivedProduct() != null
             ? sub.getDerivedProvidedProducts()
             : sub.getProvidedProducts();
   } else {
     // If this pool doesn't have a subscription associated with it, we need to
     // lookup all the Product objects manually:
     for (ProvidedProduct providedProduct : pool.getProvidedProducts()) {
       providedProducts.add(productAdapter.getProductById(providedProduct.getProductId()));
     }
   }
   return providedProducts;
 }