EntitlementCurator extends AbstractHibernateCurator<Entitlement> { public Entitlement findUpstreamEntitlementForStack(Consumer consumer, String stackId) { Date currentDate = new Date(); Criteria activeNowQuery = currentSession().createCriteria(Entitlement.class) .add(Restrictions.eq("consumer", consumer)) .createAlias("pool", "ent_pool") .createAlias("ent_pool.product", "product") .createAlias("product.attributes", "attrs") .add(Restrictions.le("ent_pool.startDate", currentDate)) .add(Restrictions.ge("ent_pool.endDate", currentDate)) .add(Restrictions.eq("attrs.indices", Product.Attributes.STACKING_ID)) .add(Restrictions.eq("attrs.elements", stackId).ignoreCase()) .add(Restrictions.isNull("ent_pool.sourceEntitlement")) .createAlias("ent_pool.sourceSubscription", "sourceSub") .add(Restrictions.isNotNull("sourceSub.id")) .addOrder(Order.asc("created")) .setMaxResults(1); return (Entitlement) activeNowQuery.uniqueResult(); } @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 findUpstreamEntitlementForStack() { String stackingId = "test_stack_id"; Product product = TestUtil.createProduct(); product.setAttribute(Product.Attributes.STACKING_ID, stackingId); productCurator.create(product); Pool futurePool = createPool(owner, product, 1L, createFutureDate(1), createFutureDate(2)); poolCurator.create(futurePool); bind(consumer, futurePool); Pool currentPool = createPool(owner, product, 1L, dateSource.currentDate(), createFutureDate(1)); poolCurator.create(currentPool); bind(consumer, currentPool); Pool anotherCurrentPool = createPool(owner, product, 1L, dateSource.currentDate(), createFutureDate(1)); poolCurator.create(anotherCurrentPool); bind(consumer, anotherCurrentPool); Entitlement result = entitlementCurator.findUpstreamEntitlementForStack(consumer, stackingId); assertNotNull(result); assertEquals(currentPool, result.getPool()); }
@Test public void findUpstreamEntitlementForStackNothingActive() { String stackingId = "test_stack_id"; Product product = TestUtil.createProduct(); product.setAttribute(Product.Attributes.STACKING_ID, stackingId); productCurator.create(product); Pool futurePool = createPool(owner, product, 1L, createFutureDate(1), createDate(2021, 1, 1)); poolCurator.create(futurePool); bind(consumer, futurePool); Entitlement result = entitlementCurator.findUpstreamEntitlementForStack(consumer, stackingId); assertNull(result); }
@Test public void findUpstreamEntitlementForStackNoResults() { String stackingId = "test_stack_id"; Product product = TestUtil.createProduct(); product.setAttribute(Product.Attributes.STACKING_ID, stackingId); productCurator.create(product); Entitlement result = entitlementCurator.findUpstreamEntitlementForStack(consumer, stackingId); assertNull(result); }