use of org.candlepin.model.SourceSubscription in project candlepin by candlepin.
the class PoolManagerTest method testRefreshPoolsRemovesExpiredSubscriptionsAlongWithItsPoolsAndEnts.
@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 = new ArrayList<>();
Owner owner = this.getOwner();
Product product = TestUtil.createProduct();
Subscription sub = TestUtil.createSubscription(owner, product);
sub.setStartDate(expiredStart);
sub.setEndDate(expiredDate);
sub.setId("123");
subscriptions.add(sub);
mockSubsList(subscriptions);
List<Pool> pools = new ArrayList<>();
Pool p = TestUtil.createPool(owner, product);
p.setId("test-pool");
p.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
p.setStartDate(expiredStart);
p.setEndDate(expiredDate);
p.setConsumed(1L);
pools.add(p);
when(mockPoolCurator.lockAndLoadByIds(anyCollection())).thenReturn(pools);
mockPoolsList(pools);
List<Entitlement> poolEntitlements = new ArrayList<>();
Entitlement ent = TestUtil.createEntitlement();
ent.setId("test-ent");
ent.setPool(p);
ent.setQuantity(1);
poolEntitlements.add(ent);
p.getEntitlements().addAll(poolEntitlements);
when(mockPoolCurator.getEntitlementIdsForPools(anyCollection())).thenReturn(Arrays.asList(ent.getId()));
CandlepinQuery<Entitlement> cqmockEnt = mock(CandlepinQuery.class);
when(cqmockEnt.list()).thenReturn(poolEntitlements);
when(cqmockEnt.iterator()).thenReturn(poolEntitlements.iterator());
when(entitlementCurator.listAllByIds(anyCollection())).thenReturn(cqmockEnt);
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);
cqmock = mock(CandlepinQuery.class);
when(cqmock.list()).thenReturn(Collections.<Pool>emptyList());
when(mockPoolCurator.getPoolsBySubscriptionIds(anyList())).thenReturn(cqmock);
this.manager.getRefresher(mockSubAdapter, mockOwnerAdapter).add(owner).run();
verify(mockPoolCurator).batchDelete(eq(pools), anyCollectionOf(String.class));
verify(entitlementCurator).batchDelete(eq(poolEntitlements));
}
use of org.candlepin.model.SourceSubscription in project candlepin by candlepin.
the class PoolManagerTest method testRefreshPoolsSkipsOrphanedEntitlementDerivedPools.
@Test
public void testRefreshPoolsSkipsOrphanedEntitlementDerivedPools() {
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"));
// Mock a pool with a source entitlement:
p.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
p.setSourceStack(null);
p.setSourceEntitlement(new Entitlement());
pools.add(p);
mockSubsList(subscriptions);
mockPoolsList(pools);
Owner owner = getOwner();
when(mockOwnerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
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();
verify(this.manager, never()).deletePool(same(p));
}
use of org.candlepin.model.SourceSubscription in project candlepin by candlepin.
the class RefresherTest method testPoolOnlyExaminedOnceTwoProducts.
@Test
public void testPoolOnlyExaminedOnceTwoProducts() {
Product product = mock(Product.class);
Product product2 = mock(Product.class);
ProductData productData = mock(ProductData.class);
ProductData productData2 = mock(ProductData.class);
when(product.getUuid()).thenReturn("product id");
when(product2.getUuid()).thenReturn("product id 2");
when(product.toDTO()).thenReturn(productData);
when(product2.toDTO()).thenReturn(productData2);
Pool pool = new Pool();
pool.setSourceSubscription(new SourceSubscription("subId", "master"));
Subscription subscription = new Subscription();
subscription.setId("subId");
Owner owner = TestUtil.createOwner();
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(eq(productData2))).thenReturn(subscriptions);
when(subAdapter.getSubscription("subId")).thenReturn(subscription);
when(poolManager.lookupBySubscriptionId(owner, "subId")).thenReturn(pools);
Pool mainPool = TestUtil.copyFromSub(subscription);
when(poolManager.convertToMasterPool(subscription)).thenReturn(mainPool);
refresher.add(product);
refresher.add(product2);
refresher.run();
verify(poolManager, times(1)).refreshPoolsForMasterPool(eq(mainPool), eq(true), eq(false), any(Map.class));
}
use of org.candlepin.model.SourceSubscription in project candlepin by candlepin.
the class OwnerResource method getSubscriptions.
/**
* Retrieves a list of Subscriptions for an Owner
*
* @return a list of Subscription objects
* @httpcode 404
* @httpcode 200
*/
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/subscriptions")
@ApiOperation(notes = "Retrieves a list of Subscriptions for an Owner", value = "List Subscriptions")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public List<Subscription> getSubscriptions(@PathParam("owner_key") String ownerKey) {
Owner owner = this.findOwnerByKey(ownerKey);
List<Subscription> subscriptions = new LinkedList<>();
for (Pool pool : this.poolManager.listPoolsByOwner(owner).list()) {
SourceSubscription srcsub = pool.getSourceSubscription();
if (srcsub != null && "master".equalsIgnoreCase(srcsub.getSubscriptionSubKey())) {
subscriptions.add(this.poolManager.fabricateSubscriptionFromPool(pool));
}
}
return subscriptions;
}
use of org.candlepin.model.SourceSubscription in project candlepin by candlepin.
the class EntitlementRules method postBindShareCreate.
private void postBindShareCreate(PoolManager poolManager, Consumer c, Owner sharingOwner, Map<String, Entitlement> entitlementMap) {
log.debug("Running post-bind share create");
Owner recipient = ownerCurator.lookupByKey(c.getRecipientOwnerKey());
List<Pool> sharedPoolsToCreate = new ArrayList<>();
for (Entitlement entitlement : entitlementMap.values()) {
Pool sourcePool = entitlement.getPool();
// resolve and copy all products
// Handle any product creation/manipulation incurred by the share action
Set<Product> allProducts = new HashSet<>();
allProducts.add(sourcePool.getProduct());
if (sourcePool.getProvidedProducts() != null) {
allProducts.addAll(sourcePool.getProvidedProducts());
}
if (sourcePool.getDerivedProduct() != null) {
allProducts.add(sourcePool.getDerivedProduct());
}
if (sourcePool.getDerivedProvidedProducts() != null) {
allProducts.addAll(sourcePool.getDerivedProvidedProducts());
}
Map<String, Product> resolvedProducts = resolveProductShares(sharingOwner, recipient, allProducts);
Product product = resolvedProducts.get(sourcePool.getProduct().getId());
Set<Product> providedProducts = copySetFromResolved(sourcePool.getProvidedProducts(), resolvedProducts);
Long q = Long.valueOf(entitlement.getQuantity());
// endDateOverride doesnt really apply here , this is just for posterity.
Date endDate = (entitlement.getEndDateOverride() == null) ? sourcePool.getEndDate() : entitlement.getEndDateOverride();
Pool sharedPool = new Pool(recipient, product, providedProducts, q, sourcePool.getStartDate(), endDate, sourcePool.getContractNumber(), sourcePool.getAccountNumber(), sourcePool.getOrderNumber());
if (sourcePool.getDerivedProduct() != null) {
Product derivedProduct = resolvedProducts.get(sourcePool.getDerivedProduct().getId());
sharedPool.setDerivedProduct(derivedProduct);
}
Set<Product> derivedProvidedProducts = copySetFromResolved(sourcePool.getDerivedProvidedProducts(), resolvedProducts);
sharedPool.setDerivedProvidedProducts(derivedProvidedProducts);
if (entitlement != null && entitlement.getPool() != null) {
sharedPool.setSourceEntitlement(entitlement);
}
/* Since we set the source entitlement id as the subscription sub key for
entitlement derived pools, it makes sense to do the same for share pools,
as share pools are also entitlement derived, sort of.
*/
String subscriptionId = sourcePool.getSubscriptionId();
if (subscriptionId != null && !subscriptionId.isEmpty()) {
sharedPool.setSourceSubscription(new SourceSubscription(subscriptionId, entitlement.getId()));
}
// Copy the pool's attributes
for (Entry<String, String> entry : sourcePool.getAttributes().entrySet()) {
sharedPool.setAttribute(entry.getKey(), entry.getValue());
}
sharedPool.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
sharedPool.setCreatedByShare(Boolean.TRUE);
sharedPool.setHasSharedAncestor(Boolean.TRUE);
for (Branding b : sourcePool.getBranding()) {
sharedPool.getBranding().add(new Branding(b.getProductId(), b.getType(), b.getName()));
}
sharedPoolsToCreate.add(sharedPool);
}
if (CollectionUtils.isNotEmpty(sharedPoolsToCreate)) {
poolManager.createPools(sharedPoolsToCreate);
}
}
Aggregations