ConsumerCurator extends AbstractHibernateCurator<Consumer> { public int countConsumers(String ownerKey, Collection<String> typeLabels, Collection<String> skus, Collection<String> subscriptionIds, Collection<String> contracts) { if (ownerKey == null || ownerKey.isEmpty()) { throw new IllegalArgumentException("Owner key can't be null or empty"); } Criteria crit = super.createSecureCriteria("c"); DetachedCriteria ownerCriteria = DetachedCriteria.forClass(Owner.class, "o") .setProjection(Property.forName("o.id")) .add(Restrictions.eq("o.key", ownerKey)); crit.add(Property.forName("c.ownerId").in(ownerCriteria)); if (!CollectionUtils.isEmpty(typeLabels)) { DetachedCriteria typeQuery = DetachedCriteria.forClass(ConsumerType.class, "ctype") .add(CPRestrictions.in("ctype.label", typeLabels)) .setProjection(Projections.id()); crit.add(Subqueries.propertyIn("c.typeId", typeQuery)); } boolean hasSkus = !CollectionUtils.isEmpty(skus); boolean hasSubscriptionIds = !CollectionUtils.isEmpty(subscriptionIds); boolean hasContracts = !CollectionUtils.isEmpty(contracts); if (hasSkus || hasSubscriptionIds || hasContracts) { crit.createAlias("c.entitlements", "e").createAlias("e.pool", "po"); } if (hasSkus) { crit.createAlias("po.product", "pr") .createAlias("pr.attributes", "pa") .add(CPRestrictions.in("pr.id", skus)) .add(Restrictions.eq("pa.indices", "type")) .add(Restrictions.eq("pa.elements", "MKT")); } if (hasSubscriptionIds) { crit.createAlias("po.sourceSubscription", "ss") .add(CPRestrictions.in("ss.subscriptionId", subscriptionIds)); } if (hasContracts) { crit.add(CPRestrictions.in("po.contractNumber", contracts)); } crit.setProjection(Projections.countDistinct("c.id")); return ((Long) crit.uniqueResult()).intValue(); } ConsumerCurator(); @Transactional @Override Consumer create(Consumer entity, boolean flush); @Override @Transactional void delete(Consumer entity); @Transactional Consumer replicate(Consumer consumer); @Transactional Consumer findByVirtUuid(String uuid, String ownerId); @Transactional VirtConsumerMap getGuestConsumersMap(String ownerId, Set<String> guestIds); @Transactional Consumer findByUser(User user); @Transactional Consumer findByUsername(String username); @Transactional Consumer findByUuid(String uuid); @Transactional Collection<Consumer> findByUuids(Collection<String> uuids); @Transactional Collection<Consumer> findByUuidsAndOwner(Collection<String> uuids, String ownerId); Consumer getConsumer(String uuid); Collection<Consumer> getConsumers(Collection<String> consumerIds); @SuppressWarnings("unchecked") @Transactional List<String> getDistinctSyspurposeRolesByOwner(Owner owner); @SuppressWarnings("unchecked") @Transactional List<String> getDistinctSyspurposeUsageByOwner(Owner owner); @SuppressWarnings("unchecked") @Transactional List<String> getDistinctSyspurposeServicelevelByOwner(Owner owner); @SuppressWarnings("unchecked") @Transactional List<String> getDistinctSyspurposeAddonsByOwner(Owner owner); @SuppressWarnings("unchecked") @Transactional CandlepinQuery<Consumer> listByOwner(Owner owner); @SuppressWarnings("unchecked") @Transactional CandlepinQuery<Consumer> listByUsernameAndType(String userName, List<ConsumerType> types,
        Owner owner); @Transactional Consumer update(Consumer updatedConsumer); @Transactional Consumer update(Consumer updatedConsumer, boolean flush); void updateLastCheckin(Consumer consumer); @Transactional void updateLastCheckin(Consumer consumer, Date checkinDate); @Transactional void heartbeatUpdate(final String reporterId, final Date checkIn, final String ownerKey); @Transactional Set<Consumer> bulkUpdate(Set<Consumer> consumers); @Transactional Set<Consumer> bulkUpdate(Set<Consumer> consumers, boolean flush); @Transactional Consumer getHost(String guestId, String ownerId); Map<String, GuestId> getGuestIdMap(Iterable<String> guestIds, Owner owner); @Transactional List<Consumer> getGuests(Consumer consumer); @Transactional Consumer getHypervisor(String hypervisorId, Owner owner); @Transactional VirtConsumerMap getHostConsumersMap(Owner owner, Iterable<String> hypervisorIds); @Transactional VirtConsumerMap getHostConsumersMap(Owner owner, List<Consumer> hypervisors); @Transactional Consumer getExistingConsumerByHypervisorIdOrUuid(String ownerId, String hypervisorId,
        String systemUuid); @SuppressWarnings("unchecked") @Transactional CandlepinQuery<Consumer> getHypervisorsBulk(Iterable<String> hypervisorIds, String ownerId); @SuppressWarnings("unchecked") @Transactional CandlepinQuery<Consumer> getHypervisorsForOwner(String ownerId); boolean doesConsumerExist(String uuid); Set<String> getExistingConsumerUuids(Iterable<String> consumerUuids); Consumer verifyAndLookupConsumer(String consumerUuid); Consumer verifyAndLookupConsumerWithEntitlements(String consumerUuid); @SuppressWarnings("checkstyle:indentation") CandlepinQuery<Consumer> searchOwnerConsumers(Owner owner, String userName,
        Collection<ConsumerType> types, List<String> uuids, List<String> hypervisorIds,
        List<KeyValueParameter> factFilters, List<String> skus,
        List<String> subscriptionIds, List<String> contracts); int countConsumers(String ownerKey,
        Collection<String> typeLabels, Collection<String> skus,
        Collection<String> subscriptionIds, Collection<String> contracts); int getConsumerCount(Owner owner, ConsumerType type); int getConsumerEntitlementCount(Owner owner, ConsumerType type); @SuppressWarnings("unchecked") List<String> getConsumerIdsWithStartedEnts(); int cullInvalidConsumerContentAccess(Owner owner, String... existingModes);  }
@Test public void nullCollectionsShouldCountAllExistingConsumers() throws Exception { createConsumer(owner); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void emptyCollectionsShouldCountAllExistingConsumers() throws Exception { createConsumer(owner); typeLabels = new HashSet<>(); skus = new LinkedList<>(); subscriptionIds = new LinkedList<>(); contracts = new LinkedList<>(); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void countShouldThrowExceptionIfOwnerKeyIsNull() throws Exception { String ownerKey = null; IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> consumerCurator.countConsumers(ownerKey, typeLabels, skus, subscriptionIds, contracts) ); assertEquals("Owner key can't be null or empty", ex.getMessage()); }
@Test public void countShouldThrowExceptionIfOwnerKeyIsEmpty() throws Exception { String ownerKey = ""; IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> consumerCurator.countConsumers(ownerKey, typeLabels, skus, subscriptionIds, contracts) ); assertEquals("Owner key can't be null or empty", ex.getMessage()); }
@Test public void countShouldReturnZeroIfOwnerHasNotAnyConsumers() throws Exception { createConsumer(owner); Owner ownerWithoutConsumers = createOwner(); int count = consumerCurator.countConsumers(ownerWithoutConsumers.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(0, count); }
@Test public void shouldCountConsumersOnlyOfGivenOwner() { createConsumer(owner); Owner otherOwner = createOwner(); createConsumer(otherOwner); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void countShouldReturnZeroIfUnkownOwner() throws Exception { createConsumer(owner); int count = consumerCurator.countConsumers("unknown-key", typeLabels, skus, subscriptionIds, contracts); assertEquals(0, count); }
@Test public void shouldCountOwnerConsumersOnlyWithTypeLabels() { Consumer c1 = createConsumer(owner); Consumer c2 = createConsumer(owner); ConsumerType c1t = consumerTypeCurator.getConsumerType(c1); ConsumerType c2t = consumerTypeCurator.getConsumerType(c2); String l1 = c1t.getLabel(); String l2 = c2t.getLabel(); assertNotEquals(l1, l2); HashSet<String> typeLabels = new HashSet<>(1); typeLabels.add(l1); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void testDisjunctionInCountingWithTypeLabels() { Consumer c1 = createConsumer(owner); Consumer c2 = createConsumer(owner); ConsumerType c1t = consumerTypeCurator.getConsumerType(c1); ConsumerType c2t = consumerTypeCurator.getConsumerType(c2); String l1 = c1t.getLabel(); String l2 = c2t.getLabel(); assertNotEquals(l1, l2); HashSet<String> typeLabels = new HashSet<>(1); typeLabels.add(l1); typeLabels.add(l2); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(2, count); }
@Test public void countShouldReturnZeroWhenUnknownLabel() throws Exception { createConsumer(owner); Set<String> labels = new HashSet<>(); labels.add("unknown-label"); int count = consumerCurator.countConsumers(owner.getKey(), labels, skus, subscriptionIds, contracts); assertEquals(0, count); }
@Test public void countWithSkuShouldWorkOnlyWithMarketingProducts() { Consumer c = createConsumer(owner); Product mktProduct = createMktProductAndBindItToConsumer(owner, c); Product notMktproduct = createProductAndBindItToConsumer(owner, c); List<String> skus = new ArrayList<>(); skus.add(notMktproduct.getId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(0, count); skus.clear(); skus.add(mktProduct.getId()); count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void shouldCountConsumersOnlyWithSkus() { Product p1 = createConsumerWithBindingToMKTProduct(owner); Product p2 = createConsumerWithBindingToMKTProduct(owner); assertNotEquals(p1.getId(), p2.getId()); List<String> skus = new ArrayList<>(); skus.add(p1.getId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void testDisjunctionInCountingWithSkus() { Product p1 = createConsumerWithBindingToMKTProduct(owner); Product p2 = createConsumerWithBindingToMKTProduct(owner); List<String> skus = new ArrayList<>(); skus.add(p1.getId()); skus.add(p2.getId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(2, count); }
@Test public void countConsumerOnlyOnceIfMoreSkusMatchToSameConsumer() { Consumer c = createConsumer(owner); Product p1 = createMktProductAndBindItToConsumer(owner, c); Product p2 = createMktProductAndBindItToConsumer(owner, c); assertNotEquals(p1.getId(), p2.getId()); List<String> skus = new ArrayList<>(); skus.add(p1.getId()); skus.add(p2.getId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void countShouldReturnZeroIfUnknownSku() throws Exception { createConsumer(owner); List<String> skus = new ArrayList<>(); skus.add("unknown-sku"); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(0, count); }
@Test public void shouldCountConsumersOnlyWithSubscriptionIds() throws Exception { Product product = createProduct(owner); Pool p1 = createConsumerWithBindingToProduct(owner, product); Pool p2 = createConsumerWithBindingToProduct(owner, product); assertNotEquals(p1.getSubscriptionId(), p2.getSubscriptionId()); List<String> ids = new ArrayList<>(); ids.add(p1.getSubscriptionId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, ids, contracts); assertEquals(1, count); }
@Test public void testDisjunctionInCountingWithSubscriptionIds() throws Exception { Product product = createProduct(owner); Pool p1 = createConsumerWithBindingToProduct(owner, product); Pool p2 = createConsumerWithBindingToProduct(owner, product); assertNotEquals(p1.getSubscriptionId(), p2.getSubscriptionId()); List<String> ids = new ArrayList<>(); ids.add(p1.getSubscriptionId()); ids.add(p2.getSubscriptionId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, ids, contracts); assertEquals(2, count); }
@Test public void countConsumerOnlyOnceIfMoreSubIdsMatchToSameConsumer() throws Exception { Consumer c = createConsumer(owner); Pool p1 = createPoolAndBindItToConsumer(owner, c); Pool p2 = createPoolAndBindItToConsumer(owner, c); assertNotEquals(p1.getSubscriptionId(), p2.getSubscriptionId()); List<String> ids = new ArrayList<>(); ids.add(p1.getSubscriptionId()); ids.add(p2.getSubscriptionId()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, ids, contracts); assertEquals(1, count); }
@Test public void countShouldReturnZeroIfUnknownSubscriptionId() throws Exception { createConsumer(owner); List<String> ids = new ArrayList<>(); ids.add("unknown-subId"); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, ids, contracts); assertEquals(0, count); }
@Test public void shouldCountConsumersOnlyWithContractNums() throws Exception { Product product = createProduct(owner); Pool p1 = createConsumerWithBindingToProduct(owner, product, "contract-1"); createConsumerAndEntitlement(owner, p1); createConsumerWithBindingToProduct(owner, product, "contract-2"); List<String> c = new ArrayList<>(); c.add(p1.getContractNumber()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, c); assertEquals(2, count); }
@Test public void testDisjunctionInCountingWithContractNums() throws Exception { Product product = createProduct(owner); Pool p1 = createConsumerWithBindingToProduct(owner, product, "contract-1"); Pool p2 = createConsumerWithBindingToProduct(owner, product, "contract-2"); List<String> c = new ArrayList<>(); c.add(p1.getContractNumber()); c.add(p2.getContractNumber()); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, c); assertEquals(2, count); }
@Test public void countConsumerOnlyOnceIfMoreContractsMatchToSameConsumer() throws Exception { Consumer c = createConsumer(owner); String contr1 = "contract-1"; String contr2 = "contract-2"; createProductAndBindItToConsumer(owner, c, contr1); createProductAndBindItToConsumer(owner, c, contr2); List<String> contracts = new ArrayList<>(); contracts.add(contr1); contracts.add(contr2); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, contracts); assertEquals(1, count); }
@Test public void countShouldReturnZeroIfUnknownContractNumber() throws Exception { createConsumer(owner); List<String> c = new ArrayList<>(); c.add("unknown-contract"); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subscriptionIds, c); assertEquals(0, count); }
@Test public void countShouldUseConjuctionBetweenAllParameters() throws Exception { String sku = "sku"; String subscriptionId = "subscriptionId"; String poolContract = "contract"; Product skuProduct = createTransientMarketingProduct(sku); skuProduct = createProduct(skuProduct, owner); Consumer c1 = createConsumer(owner); Consumer c2 = createConsumerAndBindItToProduct(owner, skuProduct); Consumer c3 = createConsumerAndBindItToProduct(owner, skuProduct, subscriptionId); Consumer c4 = createConsumerAndBindItToProduct(owner, skuProduct, subscriptionId, poolContract); ConsumerType c1t = consumerTypeCurator.getConsumerType(c1); ConsumerType c2t = consumerTypeCurator.getConsumerType(c2); ConsumerType c3t = consumerTypeCurator.getConsumerType(c3); ConsumerType c4t = consumerTypeCurator.getConsumerType(c4); Set<String> labels = new HashSet<>(); List<String> skus = new ArrayList<>(); List<String> subIds = new ArrayList<>(); List<String> contracts = new ArrayList<>(); labels.add(c1t.getLabel()); labels.add(c2t.getLabel()); labels.add(c3t.getLabel()); labels.add(c4t.getLabel()); skus.add(sku); subIds.add(subscriptionId); contracts.add(poolContract); int count = consumerCurator.countConsumers(owner.getKey(), typeLabels, skus, subIds, contracts); assertEquals(1, count); }