OwnerInfoCurator { public OwnerInfo getByOwner(Owner owner) { OwnerInfo info = new OwnerInfo(); Date now = new Date(); List<ConsumerType> types = consumerTypeCurator.listAll().list(); HashMap<String, ConsumerType> typeHash = new HashMap<>(); for (ConsumerType type : types) { typeHash.put(type.getLabel(), type); int consumers = consumerCurator.getConsumerCount(owner, type); int entCount = consumerCurator.getConsumerEntitlementCount(owner, type); info.addTypeTotal(type, consumers, entCount); int count = getRequiresConsumerTypeCount(type, owner, now); info.addToConsumerTypeCountByPool(type, count); count = getEnabledConsumerTypeCount(type, owner, now); if (count > 0) { info.addToEnabledConsumerTypeCountByPool(type, count); } } int activePools = getActivePoolCount(owner, now); info.addDefaultEnabledConsumerTypeCount(activePools); Collection<String> families = getProductFamilies(owner, now); for (String family : families) { int virtualCount = getProductFamilyCount(owner, now, family, true); int totalCount = getProductFamilyCount(owner, now, family, false); info.addToEntitlementsConsumedByFamily(family, totalCount - virtualCount, virtualCount); } int virtTotalEntitlements = getProductFamilyCount(owner, now, null, true); int totalEntitlements = getProductFamilyCount(owner, now, null, false); info.addDefaultEntitlementsConsumedByFamily( totalEntitlements - virtTotalEntitlements, virtTotalEntitlements); setConsumerGuestCounts(owner, info); setConsumerCountsByComplianceStatus(owner, info); return info; } @Inject  OwnerInfoCurator(Provider<EntityManager> entityManager,
        ConsumerCurator consumerCurator, ConsumerTypeCurator consumerTypeCurator,
        PoolCurator poolCurator); OwnerInfo getByOwner(Owner owner);  }
@Test public void testOwnerInfoNoConsumers() { OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 0); put("domain", 0); put("uebercert", 0); } }; Map<String, Integer> expectedEntitlementsConsumed = new HashMap<String, Integer>() { { put("system", 0); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); assertEquals(expectedEntitlementsConsumed, info.getEntitlementsConsumedByType()); }
@Test public void testOwnerInfoOneSystemNoEntitlements() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; Map<String, Integer> expectedEntitlementsConsumed = new HashMap<String, Integer>() { { put("system", 0); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); assertEquals(expectedEntitlementsConsumed, info.getEntitlementsConsumedByType()); }
@Test public void testOwnerInfoOneSystemEntitlement() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; Map<String, Integer> expectedEntitlementsConsumed = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); assertEquals(expectedEntitlementsConsumed, info.getEntitlementsConsumedByType()); }
@Test public void testOwnerInfoOneSystemEntitlementWithQuantityOfTwo() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(2); entitlementCurator.create(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; Map<String, Integer> expectedEntitlementsConsumed = new HashMap<String, Integer>() { { put("system", 2); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); assertEquals(expectedEntitlementsConsumed, info.getEntitlementsConsumedByType()); }
@Test public void testOwnerInfoOneOfEachEntitlement() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); type = consumerTypeCurator.getByLabel("domain"); consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); cert = createEntitlementCertificate("fake", "fake"); entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 1); put("domain", 1); put("uebercert", 0); } }; Map<String, Integer> expectedEntitlementsConsumed = new HashMap<String, Integer>() { { put("system", 1); put("domain", 1); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); assertEquals(expectedEntitlementsConsumed, info.getEntitlementsConsumedByType()); }
@Test public void testOwnerPoolEntitlementCountPoolOnly() { ConsumerType type = consumerTypeCurator.getByLabel("domain"); pool1.setAttribute(Pool.Attributes.REQUIRES_CONSUMER_TYPE, type.getLabel()); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 0); put("domain", 1); put("uebercert", 0); } }; assertEquals(expectedPoolCount, info.getConsumerTypeCountByPool()); }
@Test public void testOwnerPoolEntitlementCountProductOnly() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Product prod = new Product("sysProd", "sysProd"); prod.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type.getLabel()); createProduct(prod, owner); pool1.setProduct(prod); owner.addEntitlementPool(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedPoolCount, info.getConsumerTypeCountByPool()); }
@Test public void testOwnerPoolEntitlementPoolOverridesProduct() { ConsumerType type = consumerTypeCurator.getByLabel("domain"); ConsumerType type2 = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.REQUIRES_CONSUMER_TYPE, type.getLabel()); Product prod = new Product("sysProd", "sysProd"); prod.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type2.getLabel()); createProduct(prod, owner); pool1.setProduct(prod); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 0); put("domain", 1); put("uebercert", 0); } }; assertEquals(expectedPoolCount, info.getConsumerTypeCountByPool()); }
@Test public void testConsumerTypeCountByPoolExcludesFuturePools() { ConsumerType type = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.REQUIRES_CONSUMER_TYPE, type.getLabel()); pool1.setStartDate(Util.tomorrow()); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 0); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedPoolCount, info.getConsumerTypeCountByPool()); }
@Test public void testConsumerTypeCountByPoolExcludesExpiredPools() { ConsumerType type = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.REQUIRES_CONSUMER_TYPE, type.getLabel()); pool1.setEndDate(Util.yesterday()); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 0); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedPoolCount, info.getConsumerTypeCountByPool()); }
@Test public void testConsumerTypeCountByPoolPutsDefaultsIntoSystem() { owner.addEntitlementPool(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedPoolCount, info.getConsumerTypeCountByPool()); }
@Test public void testOwnerPoolEnabledCountPoolOnly() { ConsumerType type = consumerTypeCurator.getByLabel("domain"); pool1.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type.getLabel()); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("domain", 1); } }; assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testOwnerPoolEnabledCountProductOnly() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Product prod = new Product("sysProd", "sysProd"); prod.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type.getLabel()); createProduct(prod, owner); pool1.setProduct(prod); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("system", 1); } }; assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testOwnerPoolEnabledPoolOverridesProduct() { ConsumerType type = consumerTypeCurator.getByLabel("domain"); ConsumerType type2 = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type.getLabel()); Product prod = new Product("sysProd", "sysProd"); prod.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type2.getLabel()); createProduct(prod, owner); pool1.setProduct(prod); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("domain", 1); } }; assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testEnabledConsumerTypeCountByPoolExcludesFuturePools() { ConsumerType type = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type.getLabel()); pool1.setStartDate(Util.tomorrow()); owner.addEntitlementPool(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<>(); assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testEnabledConsumerTypeCountByPoolExcludesExpiredPools() { ConsumerType type = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type.getLabel()); pool1.setEndDate(Util.yesterday()); owner.addEntitlementPool(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<>(); assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testOwnerPoolMultiEnabledCount() { ConsumerType type1 = consumerTypeCurator.getByLabel("domain"); ConsumerType type2 = consumerTypeCurator.getByLabel("system"); pool1.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, type1.getLabel() + "," + type2.getLabel()); owner.addEntitlementPool(pool1); this.poolCurator.merge(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<String, Integer>() { { put("domain", 1); put("system", 1); } }; assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testOwnerPoolEnabledZeroCount() { pool1.setAttribute(Pool.Attributes.ENABLED_CONSUMER_TYPES, "non-type"); owner.addEntitlementPool(pool1); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedPoolCount = new HashMap<>(); assertEquals(expectedPoolCount, info.getEnabledConsumerTypeCountByPool()); }
@Test public void testOwnerInfoEntitlementsConsumedByFamilyPutsFamilylessInNone() { owner.addEntitlementPool(pool1); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); pool1.getEntitlements().add(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, OwnerInfo.ConsumptionTypeCounts> expected = new HashMap<String, OwnerInfo.ConsumptionTypeCounts>() { { put("none", new OwnerInfo.ConsumptionTypeCounts(1, 0)); } }; assertEquals(expected, info.getEntitlementsConsumedByFamily()); }
@Test public void testOwnerInfoEntitlementsConsumedByFamilyPoolOverridesProduct() { owner.addEntitlementPool(pool1); pool1.setAttribute(Pool.Attributes.PRODUCT_FAMILY, "test family"); Product product = new Product("testProd", "testProd"); product.setAttribute(Pool.Attributes.PRODUCT_FAMILY, "bad test family"); createProduct(product, owner); pool1.setProduct(product); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); pool1.getEntitlements().add(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, OwnerInfo.ConsumptionTypeCounts> expected = new HashMap<String, OwnerInfo.ConsumptionTypeCounts>() { { put("test family", new OwnerInfo.ConsumptionTypeCounts(1, 0)); } }; assertEquals(expected, info.getEntitlementsConsumedByFamily()); }
@Test public void testOwnerInfoEntitlementsConsumedByFamilySortsByFamily() { owner.addEntitlementPool(pool1); Product product = TestUtil.createProduct(); product.setAttribute(Pool.Attributes.PRODUCT_FAMILY, "test family"); createProduct(product, owner); pool1.setProduct(product); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); pool1.getEntitlements().add(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, OwnerInfo.ConsumptionTypeCounts> expected = new HashMap<String, OwnerInfo.ConsumptionTypeCounts>() { { put("test family", new OwnerInfo.ConsumptionTypeCounts(1, 0)); } }; assertEquals(expected, info.getEntitlementsConsumedByFamily()); }
@Test public void testOwnerInfoEntitlementsConsumedByFamilySeperatesVirtAndPhysical() { owner.addEntitlementPool(pool1); Product product = TestUtil.createProduct(); product.setAttribute(Pool.Attributes.VIRT_ONLY, "true"); createProduct(product, owner); pool1.setProduct(product); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); pool1.getEntitlements().add(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, OwnerInfo.ConsumptionTypeCounts> expected = new HashMap<String, OwnerInfo.ConsumptionTypeCounts>() { { put("none", new OwnerInfo.ConsumptionTypeCounts(0, 1)); } }; assertEquals(expected, info.getEntitlementsConsumedByFamily()); }
@Test public void testOwnerInfoEntitlementsConsumedByFamilySeperatesVirtExplicitFamily() { owner.addEntitlementPool(pool1); Product product = TestUtil.createProduct(); product.setAttribute(Pool.Attributes.PRODUCT_FAMILY, "test family"); product.setAttribute(Pool.Attributes.VIRT_ONLY, "true"); createProduct(product, owner); pool1.setProduct(product); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("test-consumer", "test-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate cert = createEntitlementCertificate("fake", "fake"); Entitlement entitlement = createEntitlement(owner, consumer, pool1, cert); entitlement.setQuantity(1); entitlementCurator.create(entitlement); pool1.getEntitlements().add(entitlement); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, OwnerInfo.ConsumptionTypeCounts> expected = new HashMap<String, OwnerInfo.ConsumptionTypeCounts>() { { put("test family", new OwnerInfo.ConsumptionTypeCounts(0, 1)); } }; assertEquals(expected, info.getEntitlementsConsumedByFamily()); }
@Test public void testConsumerGuestCount() { ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer guest1 = new Consumer("test-consumer", "test-user", owner, type); guest1.setFact("virt.is_guest", "true"); consumerCurator.create(guest1); Consumer guest2 = new Consumer("test-consumer", "test-user", owner, type); guest2.setFact("virt.is_guest", "true"); consumerCurator.create(guest2); Consumer physical1 = new Consumer("test-consumer", "test-user", owner, type); physical1.setFact("virt.is_guest", "false"); consumerCurator.create(physical1); Consumer physical2 = new Consumer("test-consumer2", "test-user", owner, type); consumerCurator.create(physical2); OwnerInfo info = ownerInfoCurator.getByOwner(owner); assertEquals((Integer) 2, info.getConsumerGuestCounts().get(OwnerInfo.GUEST)); assertEquals((Integer) 2, info.getConsumerGuestCounts().get(OwnerInfo.PHYSICAL)); Owner anotherOwner = createOwner(); ownerCurator.create(anotherOwner); info = ownerInfoCurator.getByOwner(anotherOwner); assertEquals((Integer) 0, info.getConsumerGuestCounts().get(OwnerInfo.GUEST)); assertEquals((Integer) 0, info.getConsumerGuestCounts().get(OwnerInfo.PHYSICAL)); }
@Test public void testConsumerCountsByEntitlementStatus() { setupConsumerCountTest("test-user"); OwnerInfo info = ownerInfoCurator.getByOwner(owner); assertConsumerCountsByEntitlementStatus(info); }
@Test public void testPermissionsAppliedWhenDeterminingConsumerCountsByEntStatus() { User mySystemsUser = setupOnlyMyConsumersPrincipal(); setupConsumerCountTest("test-user"); setupConsumerCountTest(mySystemsUser.getUsername()); OwnerInfo info = ownerInfoCurator.getByOwner(owner); assertConsumerCountsByEntitlementStatus(info); }
@Test public void testPermissionsAppliedForConsumerCounts() { User mySystemsUser = setupOnlyMyConsumersPrincipal(); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("my-system-1", mySystemsUser.getUsername(), owner, type); consumerCurator.create(consumer); consumer = new Consumer("not-my-system", "another-user", owner, type); consumerCurator.create(consumer); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); }
@Test public void testPermissionsAppliedForConsumerTypeEntitlementEntitlementsConsumedByType() { User mySystemsUser = setupOnlyMyConsumersPrincipal(); ConsumerType type = consumerTypeCurator.getByLabel("system"); Consumer consumer = new Consumer("my-system-1", mySystemsUser.getUsername(), owner, type); consumerCurator.create(consumer); EntitlementCertificate myConsumersCert = createEntitlementCertificate("mine", "mine"); Entitlement myConsumerEntitlement = createEntitlement(owner, consumer, pool1, myConsumersCert); myConsumerEntitlement.setQuantity(2); entitlementCurator.create(myConsumerEntitlement); consumerCurator.merge(consumer); consumer = new Consumer("not-my-system", "another-user", owner, type); consumerCurator.create(consumer); EntitlementCertificate otherCert = createEntitlementCertificate("other", "other"); Entitlement otherEntitlement = createEntitlement(owner, consumer, pool1, otherCert); otherEntitlement.setQuantity(1); entitlementCurator.create(otherEntitlement); consumerCurator.merge(consumer); OwnerInfo info = ownerInfoCurator.getByOwner(owner); Map<String, Integer> expectedConsumers = new HashMap<String, Integer>() { { put("system", 1); put("domain", 0); put("uebercert", 0); } }; Map<String, Integer> expectedEntitlementsConsumed = new HashMap<String, Integer>() { { put("system", 2); put("domain", 0); put("uebercert", 0); } }; assertEquals(expectedConsumers, info.getConsumerCounts()); assertEquals(expectedEntitlementsConsumed, info.getEntitlementsConsumedByType()); }