PoolCurator extends AbstractHibernateCurator<Pool> { @SuppressWarnings("unchecked") public List<Pool> getOversubscribedBySubscriptionIds(String ownerId, Map<String, Entitlement> subIdMap) { List<Criterion> subIdMapCriteria = new ArrayList<>(); Criterion[] exampleCriteria = new Criterion[0]; for (Entry<String, Entitlement> entry : subIdMap.entrySet()) { SimpleExpression subscriptionExpr = Restrictions.eq("sourceSub.subscriptionId", entry.getKey()); Junction subscriptionJunction = Restrictions.and(subscriptionExpr).add( Restrictions.or(Restrictions.isNull("sourceEntitlement"), Restrictions.eqOrIsNull("sourceEntitlement", entry.getValue()))); subIdMapCriteria.add(subscriptionJunction); } return currentSession() .createCriteria(Pool.class) .createAlias("sourceSubscription", "sourceSub") .add(Restrictions.eq("owner.id", ownerId)) .add(Restrictions.ge("quantity", 0L)) .add(Restrictions.gtProperty("consumed", "quantity")) .add(Restrictions.or(subIdMapCriteria.toArray(exampleCriteria))).list(); } @Inject  PoolCurator(ConsumerCurator consumerCurator, ConsumerTypeCurator consumerTypeCurator); @Transactional CandlepinQuery<Pool> listByOwner(Owner owner); @Transactional CandlepinQuery<Pool> listByOwner(Owner owner, Date activeOn); @Transactional CandlepinQuery<Pool> listByOwnerAndType(Owner owner, PoolType type); CandlepinQuery<Pool> listBySourceEntitlement(Entitlement e); Set<Pool> listBySourceEntitlements(Iterable<Entitlement> ents); @Transactional List<Pool> listByConsumer(Consumer c); @Transactional List<Pool> listByOwnerAndProduct(Owner owner, String productId); List<Pool> listExpiredPools(); @Transactional @SuppressWarnings("checkstyle:indentation") List<Pool> listExpiredPools(int blockSize); @SuppressWarnings("unchecked") @Transactional List<Pool> listAvailableEntitlementPools(Consumer c, Owner o, String productId, Date activeOn); @SuppressWarnings("unchecked") @Transactional List<Pool> listAvailableEntitlementPools(Consumer c, Owner o, Collection<String> productIds,
        Date activeOn); @SuppressWarnings("unchecked") @Transactional List<Pool> listAvailableEntitlementPools(Consumer c, String ownerId, Collection<String> productIds,
        Date activeOn); @Transactional List<Pool> listByFilter(PoolFilterBuilder filters); @Transactional Page<List<Pool>> listAvailableEntitlementPools(Consumer c, String ownerId, String productId,
        String subscriptionId, Date activeOn, PoolFilterBuilder filters,
        PageRequest pageRequest, boolean postFilter, boolean addFuture, boolean onlyFuture, Date after); @Transactional Page<List<Pool>> listAvailableEntitlementPools(Consumer c, Owner o, String productId,
        String subscriptionId, Date activeOn, PoolFilterBuilder filters,
        PageRequest pageRequest, boolean postFilter, boolean addFuture, boolean onlyFuture, Date after); @Transactional List<String> listEntitledConsumerUuids(String poolId); @Transactional @SuppressWarnings({"unchecked", "checkstyle:indentation", "checkstyle:methodlength"}) // TODO: Remove the methodlength suppression once this method is cleaned up Page<List<Pool>> listAvailableEntitlementPools(Consumer consumer, String ownerId,
        Collection<String> productIds, String subscriptionId, Date activeOn, PoolFilterBuilder filters,
        PageRequest pageRequest, boolean postFilter, boolean addFuture, boolean onlyFuture, Date after); @SuppressWarnings("unchecked") @Transactional boolean hasActiveEntitlementPools(String ownerId, Date date); @Transactional List<Pool> listPoolsRestrictedToUser(String username); @SuppressWarnings("unchecked") List<Entitlement> retrieveOrderedEntitlementsOf(Collection<Pool> existingPools); @SuppressWarnings("unchecked") List<String> retrieveOrderedEntitlementIdsOf(Collection<Pool> pools); void deactivatePool(Pool pool); @Transactional @SuppressWarnings("unchecked") List<Entitlement> entitlementsIn(Pool entitlementPool); @SuppressWarnings("unchecked") List<Pool> getBySubscriptionId(Owner owner, String subId); @SuppressWarnings("unchecked") List<Pool> getBySubscriptionIds(Owner owner, Collection<String> subIds); @SuppressWarnings("unchecked") List<Pool> getBySubscriptionIds(String ownerId, Collection<String> subIds); @SuppressWarnings("unchecked") List<Pool> getOversubscribedBySubscriptionIds(String ownerId, Map<String, Entitlement> subIdMap); @Transactional Pool replicate(Pool pool); @SuppressWarnings("checkstyle:indentation") int getNoOfDependentEntitlements(String entitlementId); @SuppressWarnings("unchecked") List<EntitlementCertificate> retrieveEntCertsOfPoolsWithSourceEntitlement(String entId); Filter disableConsumerFilter(); List<ActivationKey> getActivationKeysForPool(Pool p); Set<String> retrieveServiceLevelsForOwner(Owner owner, boolean exempt); Set<String> retrieveServiceLevelsForOwner(String ownerId, boolean exempt); @Transactional void delete(Pool entity); void batchDelete(Collection<Pool> pools, Collection<String> alreadyDeletedPools); @SuppressWarnings("checkstyle:indentation") List<Pool> getSubPoolForStackIds(Consumer consumer, Collection stackIds); @SuppressWarnings("unchecked") List<Pool> getOwnerSubPoolsForStackId(Owner owner, String stackId); @SuppressWarnings("unchecked") List<Pool> getPoolsFromBadSubs(Owner owner, Collection<String> expectedSubIds); @SuppressWarnings("unchecked") Set<String> getDerivedPoolIdsForPools(Iterable<String> poolIds); @SuppressWarnings("unchecked") Collection<String> getEntitlementIdsForPools(Collection<String> poolIds); @SuppressWarnings("unchecked") Collection<String> getPoolIdsForSourceEntitlements(Collection<String> entIds); @SuppressWarnings("unchecked") Collection<String> getPoolIdsForEntitlements(Collection<String> entIds); @SuppressWarnings("unchecked") CandlepinQuery<Pool> getPoolsBySubscriptionId(String subId); @SuppressWarnings("unchecked") CandlepinQuery<Pool> getPoolsBySubscriptionIds(Collection<String> subIds); @SuppressWarnings("unchecked") List<Pool> getOwnersFloatingPools(Owner owner); CandlepinQuery<Pool> getMasterPools(); CandlepinQuery<Pool> getMasterPoolsForOwner(Owner owner); CandlepinQuery<Pool> getMasterPoolsForOwnerExcludingSubs(Owner owner,
        Collection<String> excludedSubIds); @SuppressWarnings("unchecked") Set<String> getAllKnownProductIds(); @SuppressWarnings("unchecked") Set<String> getAllKnownProductIdsForOwner(Owner owner); @SuppressWarnings("checkstyle:indentation") Pool findDevPool(Consumer consumer); boolean exists(Pool pool); void calculateConsumedForOwnersPools(Owner owner); void calculateExportedForOwnersPools(Owner owner); void markCertificatesDirtyForPoolsWithProducts(Owner owner, Collection<String> productIds); Boolean provides(Pool pool, String providedProductId); Boolean providesDerived(Pool pool, String derivedProvidedProductId); Map<String, Set<String>> getProvidedProductIds(Collection<Pool> pools); Map<String, Set<String>> getProvidedProductIdsByPoolIds(Collection<String> poolIds); Map<String, Set<String>> getDerivedProvidedProductIds(Collection<Pool> pools); Map<String, Set<String>> getDerivedProvidedProductIdsByPoolIds(Collection<String> poolIds); @Transactional void removeCdn(Cdn cdn); void clearPoolSourceEntitlementRefs(Iterable<String> poolIds); Set<String> getExistingPoolIdsByIds(Iterable<String> poolIds); Map<String, Set<String>> getConsumerStackDerivedPoolIdMap(Iterable<String> stackIds); Set<String> getUnentitledStackDerivedPoolIds(Iterable<String> entitlementIds); static final int EXPIRED_POOL_BLOCK_SIZE; }
@Test public void testBatchLookupOverconsumedBySubscriptionId() { Map<String, Entitlement> subIdMap = new HashMap<>(); List<Pool> expectedPools = new ArrayList<>(); for (Integer i = 0; i < 5; i++) { Pool pool = createPool(owner, product, 1L, TestUtil.createDate(2050, 3, 2), TestUtil.createDate(2055, 3, 2)); poolCurator.create(pool); expectedPools.add(pool); String subid = pool.getSubscriptionId(); Entitlement e = new Entitlement(pool, consumer, owner, 2); e.setId(Util.generateDbUUID()); entitlementCurator.create(e); pool.setConsumed(pool.getConsumed() + 2); poolCurator.merge(pool); subIdMap.put(subid, e); } Pool unconsumedPool = createPool(owner, product, 1L, TestUtil.createDate(2050, 3, 2), TestUtil.createDate(2055, 3, 2)); poolCurator.create(unconsumedPool); Pool notOverConsumedPool = createPool(owner, product, 1L, TestUtil.createDate(2050, 3, 2), TestUtil.createDate(2055, 3, 2)); poolCurator.create(notOverConsumedPool); Entitlement ent = new Entitlement(notOverConsumedPool, consumer, owner, 1); ent.setId(Util.generateDbUUID()); entitlementCurator.create(ent); notOverConsumedPool.setConsumed(notOverConsumedPool.getConsumed() + 1); poolCurator.merge(notOverConsumedPool); Pool overConsumedPool = createPool(owner, product, 1L, TestUtil.createDate(2050, 3, 2), TestUtil.createDate(2055, 3, 2)); poolCurator.create(overConsumedPool); Entitlement ent12 = new Entitlement(overConsumedPool, consumer, owner, 2); ent12.setId(Util.generateDbUUID()); entitlementCurator.create(ent12); overConsumedPool.setConsumed(overConsumedPool.getConsumed() + 2); poolCurator.merge(overConsumedPool); List<Pool> gotPools = poolCurator.getOversubscribedBySubscriptionIds(owner.getId(), subIdMap); assertEquals(5, gotPools.size()); assertThat(expectedPools, hasItems(gotPools.toArray(new Pool[0]))); assertThat(gotPools, not(hasItem(unconsumedPool))); assertThat(gotPools, not(hasItem(notOverConsumedPool))); assertThat(gotPools, not(hasItem(overConsumedPool))); }
@Test public void testLookupOverconsumedIgnoresOtherSourceEntitlementPools() { Pool pool = createPool(owner, product, 1L, TestUtil.createDate(2011, 3, 2), TestUtil.createDate(2055, 3, 2)); poolCurator.create(pool); String subid = pool.getSubscriptionId(); Entitlement sourceEnt = new Entitlement(pool, consumer, owner, 1); sourceEnt.setId(Util.generateDbUUID()); entitlementCurator.create(sourceEnt); pool.setConsumed(pool.getConsumed() + 1); poolCurator.merge(pool); Pool derivedPool = new Pool( owner, product, new HashSet<>(), 1L, TestUtil.createDate(2011, 3, 2), TestUtil.createDate(2055, 3, 2), "", "", "" ); derivedPool.setSourceEntitlement(sourceEnt); derivedPool.setSourceSubscription(new SourceSubscription(subid, "derived")); poolCurator.create(derivedPool); Map<String, Entitlement> subMap = new HashMap<>(); subMap.put(subid, sourceEnt); assertEquals(0, poolCurator.getOversubscribedBySubscriptionIds(owner.getId(), subMap).size()); Entitlement derivedEnt = new Entitlement(derivedPool, consumer, owner, 3); derivedEnt.setId(Util.generateDbUUID()); entitlementCurator.create(derivedEnt); derivedPool.setConsumed(derivedPool.getConsumed() + 3); poolCurator.merge(derivedPool); assertEquals(1, poolCurator.getOversubscribedBySubscriptionIds(owner.getId(), subMap).size()); subMap.clear(); subMap.put(subid, derivedEnt); assertEquals(0, poolCurator.getOversubscribedBySubscriptionIds(owner.getId(), subMap).size()); }