EntitlementCurator extends AbstractHibernateCurator<Entitlement> { public int markDependentEntitlementsDirty(Iterable<String> entitlementIds) { if (entitlementIds != null && entitlementIds.iterator().hasNext()) { Set<String> eids = new HashSet<>(); if (entitlementIds != null && entitlementIds.iterator().hasNext()) { eids.addAll(findDependentEntitlementsByProvidedProduct(entitlementIds)); Set<String> distributorEntitlements = filterDistributorEntitlementIds(entitlementIds); if (!distributorEntitlements.isEmpty()) { eids.addAll(findDependentEntitlementsByDerivedProvidedProduct(distributorEntitlements)); } } return eids.isEmpty() ? 0 : this.markEntitlementsDirty(eids); } return 0; } @Inject  EntitlementCurator(OwnerProductCurator ownerProductCurator, ProductCurator productCurator,
        ConsumerTypeCurator consumerTypeCurator, CandlepinQueryFactory cpQueryFactory); @Transactional Set<Entitlement> bulkUpdate(Set<Entitlement> entitlements); List<Entitlement> listByConsumer(Consumer consumer); List<Entitlement> listByConsumer(Consumer consumer, EntitlementFilterBuilder filters); List<Entitlement> listByConsumerAndPoolId(Consumer consumer, String poolId); Page<List<Entitlement>> listByConsumer(Consumer consumer, String productId,
        EntitlementFilterBuilder filters, PageRequest pageRequest); Page<List<Entitlement>> listByOwner(Owner owner, String productId,
        EntitlementFilterBuilder filters, PageRequest pageRequest); Page<List<Entitlement>> listAll(EntitlementFilterBuilder filters, PageRequest pageRequest); CandlepinQuery<Entitlement> listByOwner(Owner owner); CandlepinQuery<Entitlement> listByEnvironment(Environment environment); CandlepinQuery<Entitlement> listByEnvironment(String environmentId); CandlepinQuery<Entitlement> listByConsumerAndDate(Consumer consumer, Date activeOn); List<Entitlement> listDirty(Consumer consumer); Set<String> listEntitledProductIds(Consumer consumer, Pool pool); int markDependentEntitlementsDirty(Iterable<String> entitlementIds); Set<String> filterDistributorEntitlementIds(Iterable<String> entsIdsToFilter); @Transactional int markEntitlementsDirty(Iterable<String> entitlementIds); @Deprecated Map<Consumer, List<Entitlement>> getDistinctConsumers(List<Entitlement> entsToRevoke); @Transactional void delete(Entitlement entity); void batchDelete(Collection<Entitlement> entitlements); int batchDeleteByIds(Collection<String> entitlementIds); void unlinkEntitlements(Collection<Entitlement> entitlements); @Transactional Entitlement findByCertificateSerial(Long serial); @Transactional Entitlement replicate(Entitlement ent); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByStackId(Consumer consumer, String stackId); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByStackIds(Consumer consumer, Collection stackIds); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByPoolAttribute(Consumer consumer, String attributeName,
        String value); CandlepinQuery<Entitlement> findByPoolAttribute(String attributeName, String value); Entitlement findUpstreamEntitlementForStack(Consumer consumer, String stackId); Collection<String> getDependentEntitlementIdsForPools(Consumer consumer,
        Iterable<String> poolIds);  }
@Test public void testMarkDependentEntitlementsDirty() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> dependentProduct1 = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts.subList(0, 1)); List<Product> dependentProduct2 = this.createDependentProducts(owner, 1, "test_dep_prod_b", requiredProducts.subList(1, 2)); List<Product> dependentProduct3 = this.createDependentProducts(owner, 1, "test_dep_prod_c", requiredProducts.subList(2, 3)); Pool requiredPool1 = this.createPoolWithProducts(owner, "reqPool1", requiredProducts.subList(0, 2)); Pool requiredPool2 = this.createPoolWithProducts(owner, "reqPool2", requiredProducts.subList(1, 3)); Pool requiredPool3 = this.createPoolWithProducts(owner, "reqPool3", requiredProducts.subList(0, 3)); Pool dependentPool1 = this.createPoolWithProducts(owner, "depPool1", dependentProduct1); Pool dependentPool2 = this.createPoolWithProducts(owner, "depPool2", dependentProduct2); Pool dependentPool3 = this.createPoolWithProducts(owner, "depPool3", dependentProduct3); Entitlement reqPool1Ent = this.bind(consumer, requiredPool1); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(0, count); Entitlement depPool3Ent = this.bind(consumer, dependentPool3); count = this.entitlementCurator.markDependentEntitlementsDirty(Arrays.asList(reqPool1Ent.getId())); assertEquals(0, count); Entitlement depPool2Ent = this.bind(consumer, dependentPool2); count = this.entitlementCurator.markDependentEntitlementsDirty(Arrays.asList(reqPool1Ent.getId())); assertEquals(1, count); this.entitlementCurator.refresh(reqPool1Ent, depPool2Ent, depPool3Ent); assertFalse(reqPool1Ent.isDirty()); assertFalse(depPool3Ent.isDirty()); assertTrue(depPool2Ent.isDirty()); this.resetDirtyEntitlements(reqPool1Ent, depPool3Ent, depPool2Ent); Entitlement reqPool2Ent = this.bind(consumer, requiredPool2); count = this.entitlementCurator .markDependentEntitlementsDirty(Arrays.asList(reqPool1Ent.getId(), reqPool2Ent.getId())); assertEquals(2, count); this.entitlementCurator.refresh(reqPool1Ent, reqPool2Ent, depPool2Ent, depPool3Ent); assertFalse(reqPool1Ent.isDirty()); assertFalse(reqPool2Ent.isDirty()); assertTrue(depPool2Ent.isDirty()); assertTrue(depPool3Ent.isDirty()); this.resetDirtyEntitlements(reqPool1Ent, reqPool2Ent, depPool2Ent, depPool3Ent); this.entitlementCurator.delete(reqPool1Ent); this.entitlementCurator.flush(); count = this.entitlementCurator .markDependentEntitlementsDirty(Arrays.asList(reqPool1Ent.getId(), reqPool2Ent.getId())); assertEquals(2, count); this.entitlementCurator.refresh(reqPool2Ent, depPool2Ent, depPool3Ent); assertFalse(reqPool2Ent.isDirty()); assertTrue(depPool2Ent.isDirty()); assertTrue(depPool3Ent.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotUpdateUnrelatedEntitlements() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> dependentProduct1 = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts.subList(0, 1)); List<Product> dependentProduct2 = this.createDependentProducts(owner, 1, "test_dep_prod_b", requiredProducts.subList(1, 2)); List<Product> dependentProduct3 = this.createDependentProducts(owner, 1, "test_dep_prod_c", requiredProducts.subList(2, 3)); Pool requiredPool1 = this.createPoolWithProducts(owner, "reqPool1", requiredProducts.subList(0, 1)); Pool requiredPool2 = this.createPoolWithProducts(owner, "reqPool2", requiredProducts.subList(1, 2)); Pool requiredPool3 = this.createPoolWithProducts(owner, "reqPool3", requiredProducts.subList(2, 3)); Pool dependentPool1 = this.createPoolWithProducts(owner, "depPool1", dependentProduct1); Pool dependentPool2 = this.createPoolWithProducts(owner, "depPool2", dependentProduct2); Pool dependentPool3 = this.createPoolWithProducts(owner, "depPool3", dependentProduct3); Entitlement reqPool1Ent = this.bind(consumer, requiredPool1); Entitlement depPool1Ent = this.bind(consumer, dependentPool1); Entitlement depPool2Ent = this.bind(consumer, dependentPool2); Entitlement depPool3Ent = this.bind(consumer, dependentPool3); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(1, count); this.entitlementCurator.refresh(reqPool1Ent, depPool1Ent, depPool2Ent, depPool3Ent); assertFalse(reqPool1Ent.isDirty()); assertTrue(depPool1Ent.isDirty()); assertFalse(depPool2Ent.isDirty()); assertFalse(depPool3Ent.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotUpdateOtherConsumerEntitlements() { Owner owner = this.createOwner("test_owner"); Consumer consumer1 = this.createConsumer(owner); Consumer consumer2 = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> dependentProduct1 = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts.subList(0, 1)); List<Product> dependentProduct2 = this.createDependentProducts(owner, 1, "test_dep_prod_b", requiredProducts.subList(1, 2)); List<Product> dependentProduct3 = this.createDependentProducts(owner, 1, "test_dep_prod_c", requiredProducts.subList(2, 3)); Pool requiredPool1 = this.createPoolWithProducts(owner, "reqPool1", requiredProducts.subList(0, 1)); Pool requiredPool2 = this.createPoolWithProducts(owner, "reqPool2", requiredProducts.subList(1, 2)); Pool requiredPool3 = this.createPoolWithProducts(owner, "reqPool3", requiredProducts.subList(2, 3)); Pool dependentPool1 = this.createPoolWithProducts(owner, "depPool1", dependentProduct1); Pool dependentPool2 = this.createPoolWithProducts(owner, "depPool2", dependentProduct2); Pool dependentPool3 = this.createPoolWithProducts(owner, "depPool3", dependentProduct3); Entitlement reqPool1Ent = this.bind(consumer1, requiredPool1); Entitlement depPool1EntA = this.bind(consumer1, dependentPool1); Entitlement depPool2EntA = this.bind(consumer1, dependentPool2); Entitlement depPool3EntA = this.bind(consumer1, dependentPool3); Entitlement depPool1EntB = this.bind(consumer2, dependentPool1); Entitlement depPool2EntB = this.bind(consumer2, dependentPool2); Entitlement depPool3EntB = this.bind(consumer2, dependentPool3); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(1, count); this.entitlementCurator.refresh(reqPool1Ent, depPool1EntA, depPool2EntA, depPool3EntA, depPool1EntB, depPool2EntB, depPool3EntB); assertFalse(reqPool1Ent.isDirty()); assertTrue(depPool1EntA.isDirty()); assertFalse(depPool2EntA.isDirty()); assertFalse(depPool3EntA.isDirty()); assertFalse(depPool1EntB.isDirty()); assertFalse(depPool2EntB.isDirty()); assertFalse(depPool3EntB.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotUpdateOtherOwnerEntitlements() { Owner owner1 = this.createOwner("test_owner-1"); Owner owner2 = this.createOwner("test_owner-2"); Consumer consumer1 = this.createConsumer(owner1); Consumer consumer2 = this.createConsumer(owner2); List<Product> reqProducts1 = Arrays.asList( this.createProduct("req_prod_1", "req_prod_1", owner1), this.createProduct("req_prod_2", "req_prod_2", owner1)); List<Product> reqProducts2 = Arrays.asList( this.createProduct("req_prod_1", "req_prod_1", owner2), this.createProduct("req_prod_2", "req_prod_2", owner2)); List<Product> dependentProductA = this.createDependentProducts(owner1, 1, "test_dep_prod_a", reqProducts1.subList(0, 1)); List<Product> dependentProductB = this.createDependentProducts(owner2, 1, "test_dep_prod_b", reqProducts2.subList(0, 1)); Pool requiredPool1A = this.createPoolWithProducts(owner1, "reqPool1", reqProducts1.subList(0, 1)); Pool requiredPool2A = this.createPoolWithProducts(owner1, "reqPool2", reqProducts1.subList(1, 2)); Pool requiredPool1B = this.createPoolWithProducts(owner2, "reqPool1", reqProducts2.subList(0, 1)); Pool requiredPool2B = this.createPoolWithProducts(owner2, "reqPool2", reqProducts2.subList(1, 2)); Pool dependentPoolA = this.createPoolWithProducts(owner1, "depPool1", dependentProductA); Pool dependentPoolB = this.createPoolWithProducts(owner2, "depPool2", dependentProductB); Entitlement reqPool1AEnt = this.bind(consumer1, requiredPool1A); Entitlement reqPool2AEnt = this.bind(consumer1, requiredPool2A); Entitlement depPoolAEnt = this.bind(consumer1, dependentPoolA); Entitlement reqPool1BEnt = this.bind(consumer2, requiredPool1B); Entitlement reqPool2BEnt = this.bind(consumer2, requiredPool2B); Entitlement depPoolBEnt = this.bind(consumer2, dependentPoolB); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1AEnt.getId(), reqPool2AEnt.getId())); assertEquals(1, count); this.entitlementCurator.refresh(reqPool1AEnt, reqPool2AEnt, depPoolAEnt, reqPool1BEnt, reqPool2BEnt, depPoolBEnt); assertFalse(reqPool1AEnt.isDirty()); assertFalse(reqPool2AEnt.isDirty()); assertTrue(depPoolAEnt.isDirty()); assertFalse(reqPool1BEnt.isDirty()); assertFalse(reqPool2BEnt.isDirty()); assertFalse(depPoolBEnt.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyUpdatesMultipleConsumers() { Owner owner1 = this.createOwner("test_owner-1"); Owner owner2 = this.createOwner("test_owner-2"); Consumer consumer1 = this.createConsumer(owner1); Consumer consumer2 = this.createConsumer(owner2); List<Product> reqProds1 = Arrays.asList( this.createProduct("req_prod_1", "req_prod_1", owner1), this.createProduct("req_prod_2", "req_prod_2", owner1)); List<Product> reqProds2 = Arrays.asList( this.createProduct("req_prod_1", "req_prod_1", owner2), this.createProduct("req_prod_2", "req_prod_2", owner2)); List<Product> dependentProductA = this.createDependentProducts(owner1, 1, "test_dep_prod_a", reqProds1.subList(0, 1)); List<Product> dependentProductB = this.createDependentProducts(owner2, 1, "test_dep_prod_b", reqProds2.subList(0, 1)); Pool requiredPool1A = this.createPoolWithProducts(owner1, "reqPool1", reqProds1.subList(0, 1)); Pool requiredPool2A = this.createPoolWithProducts(owner1, "reqPool2", reqProds1.subList(1, 2)); Pool requiredPool1B = this.createPoolWithProducts(owner2, "reqPool1", reqProds2.subList(0, 1)); Pool requiredPool2B = this.createPoolWithProducts(owner2, "reqPool2", reqProds2.subList(1, 2)); Pool dependentPoolA = this.createPoolWithProducts(owner1, "depPool1", dependentProductA); Pool dependentPoolB = this.createPoolWithProducts(owner2, "depPool2", dependentProductB); Entitlement reqPool1AEnt = this.bind(consumer1, requiredPool1A); Entitlement reqPool2AEnt = this.bind(consumer1, requiredPool2A); Entitlement depPoolAEnt = this.bind(consumer1, dependentPoolA); Entitlement reqPool1BEnt = this.bind(consumer2, requiredPool1B); Entitlement reqPool2BEnt = this.bind(consumer2, requiredPool2B); Entitlement depPoolBEnt = this.bind(consumer2, dependentPoolB); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1AEnt.getId(), reqPool2AEnt.getId(), reqPool1BEnt.getId(), reqPool2BEnt.getId())); assertEquals(2, count); this.entitlementCurator.refresh(reqPool1AEnt, reqPool2AEnt, depPoolAEnt, reqPool1BEnt, reqPool2BEnt, depPoolBEnt); assertFalse(reqPool1AEnt.isDirty()); assertFalse(reqPool2AEnt.isDirty()); assertTrue(depPoolAEnt.isDirty()); assertFalse(reqPool1BEnt.isDirty()); assertFalse(reqPool2BEnt.isDirty()); assertTrue(depPoolBEnt.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotRequireAllRequiredProducts() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> dependentProduct = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts); Pool requiredPool1 = this.createPoolWithProducts(owner, "reqPool1", requiredProducts.subList(0, 1)); Pool requiredPool2 = this.createPoolWithProducts(owner, "reqPool2", requiredProducts.subList(1, 2)); Pool requiredPool3 = this.createPoolWithProducts(owner, "reqPool3", requiredProducts.subList(2, 3)); Pool dependentPool = this.createPoolWithProducts(owner, "depPool1", dependentProduct); Entitlement reqPool1Ent = this.bind(consumer, requiredPool2); Entitlement depPool1Ent = this.bind(consumer, dependentPool); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(1, count); this.entitlementCurator.refresh(reqPool1Ent, depPool1Ent); assertFalse(reqPool1Ent.isDirty()); assertTrue(depPool1Ent.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotExamineSkuProducts() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> providedProducts = this.createProducts(owner, 3, "test_prov_prod"); List<Product> dependentProduct = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts); Pool requiredPool = this.createPool(owner, requiredProducts.get(0), providedProducts, 1000L, TestUtil.createDate(2000, 1, 1), TestUtil.createDate(2100, 1, 1)); Pool dependentPool = this.createPoolWithProducts(owner, "depPool1", dependentProduct); Entitlement reqPool1Ent = this.bind(consumer, requiredPool); Entitlement depPool1Ent = this.bind(consumer, dependentPool); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(0, count); this.entitlementCurator.refresh(reqPool1Ent, depPool1Ent); assertFalse(reqPool1Ent.isDirty()); assertFalse(depPool1Ent.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotExamineDerivedProducts() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> providedProducts = this.createProducts(owner, 3, "test_prov_prod"); List<Product> dependentProduct = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts); Pool requiredPool = this.createPoolWithProducts(owner, "reqPool1", providedProducts); requiredPool.setDerivedProduct(requiredProducts.get(0)); this.poolCurator.merge(requiredPool); Pool dependentPool = this.createPoolWithProducts(owner, "depPool1", dependentProduct); Entitlement reqPool1Ent = this.bind(consumer, requiredPool); Entitlement depPool1Ent = this.bind(consumer, dependentPool); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(0, count); this.entitlementCurator.refresh(reqPool1Ent, depPool1Ent); assertFalse(reqPool1Ent.isDirty()); assertFalse(depPool1Ent.isDirty()); }
@Test public void testMarkDependentEntitlementsDirtyDoesNotExamineDerivedProvidedProducts() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> providedProducts = this.createProducts(owner, 3, "test_prov_prod"); List<Product> dependentProduct = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts); Pool requiredPool = this.createPoolWithProducts(owner, "reqPool1", providedProducts); requiredPool.setDerivedProvidedProducts(requiredProducts); this.poolCurator.merge(requiredPool); Pool dependentPool = this.createPoolWithProducts(owner, "depPool1", dependentProduct); Entitlement reqPool1Ent = this.bind(consumer, requiredPool); Entitlement depPool1Ent = this.bind(consumer, dependentPool); int count = this.entitlementCurator.markDependentEntitlementsDirty( Arrays.asList(reqPool1Ent.getId())); assertEquals(0, count); this.entitlementCurator.refresh(reqPool1Ent, depPool1Ent); assertFalse(reqPool1Ent.isDirty()); assertFalse(depPool1Ent.isDirty()); }