QuantityRules { @SuppressWarnings("checkstyle:indentation") public SuggestedQuantityDTO getSuggestedQuantity(Pool p, Consumer c, Date date) { JsonJsContext args = new JsonJsContext(mapper); Stream<EntitlementDTO> entStream = c.getEntitlements() == null ? Stream.empty() : c.getEntitlements().stream() .filter(ent -> ent.isValidOnDate(date)) .map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class)); Stream<GuestIdDTO> guestIdStream = c.getGuestIds() == null ? Stream.empty() : c.getGuestIds().stream() .map(this.translator.getStreamMapper(GuestId.class, GuestIdDTO.class)); args.put("consumer", this.translator.translate(c, ConsumerDTO.class)); args.put("pool", this.translator.translate(p, PoolDTO.class)); args.put("validEntitlements", entStream); args.put("log", log, false); args.put("guestIds", guestIdStream); String json = jsRules.runJsFunction(String.class, "get_suggested_quantity", args); return mapper.toObject(json, SuggestedQuantityDTO.class); } @Inject  QuantityRules(JsRunner jsRules, RulesObjectMapper mapper, ModelTranslator translator); @SuppressWarnings("checkstyle:indentation") SuggestedQuantityDTO getSuggestedQuantity(Pool p, Consumer c, Date date); @SuppressWarnings("checkstyle:indentation") Map<String, SuggestedQuantityDTO> getSuggestedQuantities(List<Pool> pools, Consumer c, Date date);  }
@Test public void testNonMultiEntitlementPool() { pool.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "no"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, TestUtil.createConsumer(), new Date()); assertEquals(new Long(1), suggested.getSuggested()); }
@Test public void testPhysicalDefaultToNumSocketsBySocketCount() { consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testPhysicalRoundsUp() { consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "3"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testPhysicalAccountsForCurrentlyConsumed() { consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "1"); Entitlement e = createValidEntitlement(pool); e.setQuantity(2); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testVirtDefaultToNumCpusByVcpuCount() { consumer.setFact(IS_VIRT, "true"); consumer.setFact(CORES_FACT, "8"); pool.getProduct().setAttribute(VCPU_ATTRIBUTE, "4"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void shouldUseCoresIfNoVcpu() { consumer.setFact(IS_VIRT, "true"); consumer.setFact(SOCKET_FACT, "1"); consumer.setFact(CORES_FACT, "8"); pool.getProduct().setAttribute(CORES_ATTRIBUTE, "2"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "1"); final SuggestedQuantityDTO suggested = quantityRules .getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); }
@Test public void testVirtIgnoresSockets() { consumer.getEntitlements().clear(); consumer.setFact(IS_VIRT, "true"); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(1), suggested.getSuggested()); }
@Test public void testVirtUses1IfNoVcpu() { consumer.getEntitlements().clear(); consumer.setFact(IS_VIRT, "true"); consumer.setFact(SOCKET_FACT, "4"); consumer.setFact(CORES_FACT, "8"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(1), suggested.getSuggested()); }
@Test public void testVirtRoundsUp() { consumer.setFact(IS_VIRT, "true"); consumer.setFact(CORES_FACT, "8"); pool.getProduct().setAttribute(VCPU_ATTRIBUTE, "6"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testVirtAccountsForCurrentlyConsumed() { consumer.setFact(IS_VIRT, "true"); consumer.setFact(CORES_FACT, "4"); pool.getProduct().setAttribute(VCPU_ATTRIBUTE, "1"); Entitlement e = createValidEntitlement(pool); e.setQuantity(2); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testUnlimitedQuantity() { consumer.setFact(SOCKET_FACT, "8"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.setQuantity(new Long(-1)); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); }
@Test public void testIsNotVirtWhenFactIsFalse() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); assertEquals(new Long(1), suggested.getIncrement()); }
@Test public void testInstanceBasedOnPhysical() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); assertEquals(new Long(2), suggested.getIncrement()); }
@Test public void testInstanceBasedOnSingleSocketPhysical() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "1"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); assertEquals(new Long(2), suggested.getIncrement()); }
@Test public void testSingleSocketInstanceBasedOnPhysical() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "1"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "1"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); assertEquals(new Long(2), suggested.getIncrement()); }
@Test public void testInstanceBasedOnPhysicalNotEnoughAvailable() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "40"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); pool.setQuantity(4L); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); assertEquals(new Long(2), suggested.getIncrement()); }
@Test public void testInstanceBasedOnPhysicalNotEnoughAvailableUneven() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "40"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); pool.setQuantity(3L); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); assertEquals(new Long(2), suggested.getIncrement()); }
@Test public void testInstanceBasedOnGuest() { consumer.getEntitlements().clear(); consumer.setFact(IS_VIRT, "true"); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(1), suggested.getSuggested()); assertEquals(new Long(1), suggested.getIncrement()); }
@Test public void testIsNotVirtWhenFactIsEmpty() { consumer.setFact(IS_VIRT, ""); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testTotalConsumedIsZeroWhenNoMatches() { consumer.setFact(IS_VIRT, ""); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); Product product2 = TestUtil.createProduct(); Pool pool2 = TestUtil.createPool(owner, product2); Entitlement e = createValidEntitlement(pool2); e.setQuantity(2); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testCalculatedValueIsZeroWhenNegativeIsCalculated() { consumer.setFact(IS_VIRT, ""); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); Entitlement e = createValidEntitlement(pool); e.setQuantity(1000); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(0), suggested.getSuggested()); }
@Test public void testTotalConsumedDoesNotIncludeFutureEntitlements() { consumer.setFact(IS_VIRT, ""); consumer.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); Entitlement e = TestUtil.createEntitlement(owner, consumer, pool, null); pool.setStartDate(TestUtil.createDate(9000, 1, 1)); pool.setEndDate(TestUtil.createDate(9001, 1, 1)); e.setQuantity(2); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity( pool, consumer, TestUtil.createDate(2010, 1, 1)); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testFutureSuggested() { consumer.setFact(SOCKET_FACT, "4"); pool.setStartDate(TestUtil.createDate(9000, 1, 1)); pool.setEndDate(TestUtil.createDate(9001, 1, 1)); Pool currentPool = TestUtil.createPool(owner, product); currentPool.setStartDate(TestUtil.createDate(2000, 1, 1)); currentPool.setEndDate(TestUtil.createDate(5000, 1, 1)); currentPool.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); currentPool.getProduct().setAttribute(Product.Attributes.STACKING_ID, "1"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); currentPool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); Entitlement currentEntitlement = TestUtil.createEntitlement(owner, consumer, currentPool, null); currentEntitlement.setQuantity(2); Set<Entitlement> ents = new HashSet<>(); ents.add(currentEntitlement); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity( currentPool, consumer, TestUtil.createDate(2010, 6, 1)); assertEquals(new Long(0), suggested.getSuggested()); suggested = quantityRules.getSuggestedQuantity(pool, consumer, TestUtil.createDate(9000, 6, 1)); assertEquals(new Long(2), suggested.getSuggested()); }
@Test public void testPhysicalIgnoresFutureConsumed() { Pool futurePool = TestUtil.createPool(owner, product); futurePool.setStartDate(TestUtil.createDate(2050, 1, 1)); futurePool.setEndDate(TestUtil.createDate(2060, 1, 1)); pool.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); pool.getProduct().setAttribute(Product.Attributes.STACKING_ID, "1"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "1"); futurePool.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); futurePool.getProduct().setAttribute(Product.Attributes.STACKING_ID, "1"); futurePool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "1"); consumer.setFact(SOCKET_FACT, "4"); Entitlement e = createValidEntitlement(futurePool); e.setQuantity(4); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); }
@Test public void testPhysicalIgnoresPastConsumed() { pool.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); pool.getProduct().setAttribute(Product.Attributes.STACKING_ID, "1"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "1"); consumer.setFact(SOCKET_FACT, "4"); Entitlement e = createValidEntitlement(pool); e.setQuantity(4); Set<Entitlement> ents = new HashSet<>(); ents.add(e); consumer.setEntitlements(ents); Calendar c = Calendar.getInstance(); c.setTime(pool.getEndDate()); Date futureDate = TestUtil.createDate(c.get(Calendar.YEAR) + 1, 1, 1); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, futureDate); assertEquals(new Long(4), suggested.getSuggested()); }
@Test public void testStackOnlyStacksWithSameStackingId() { consumer.setFact(IS_VIRT, "false"); consumer.setFact(SOCKET_FACT, "8"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.setQuantity(10L); Product product1 = TestUtil.createProduct(); Pool pool1 = TestUtil.createPool(owner, product1); Entitlement e = TestUtil.createEntitlement(owner, consumer, pool1, new EntitlementCertificate()); Set<Entitlement> entSet = new HashSet<>(); entSet.add(e); pool1.setEntitlements(entSet); pool1.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); pool1.getProduct().setAttribute(Product.Attributes.STACKING_ID, "2"); pool1.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool1.setQuantity(10L); Entitlement toAdd = pool1.getEntitlements().iterator().next(); toAdd.setQuantity(2); consumer.addEntitlement(toAdd); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); }
@Test public void testInsufficientGuestLimit() { consumer.setFact(SOCKET_FACT, "8"); Map<String, String> guestAttrs = new HashMap<>(); guestAttrs.put("virtWhoType", "libvirt"); guestAttrs.put("active", "1"); for (int i = 0; i < 5; i++) { consumer.addGuestId(new GuestId("" + i, consumer, guestAttrs)); } pool.getProduct().setAttribute(GUEST_LIMIT_ATTRIBUTE, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.setQuantity(new Long(-1)); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date()); assertEquals(new Long(4), suggested.getSuggested()); }
@Test public void testInstanceBasedOnDistributor() { Consumer dist = TestUtil.createConsumer(owner); dist.setFact(IS_VIRT, "false"); dist.setFact(SOCKET_FACT, "4"); pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2"); pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2"); ctype.setManifest(true); when(consumerTypeCurator.getConsumerType(eq(dist))).thenReturn(ctype); SuggestedQuantityDTO suggested = quantityRules.getSuggestedQuantity(pool, dist, new Date()); assertEquals(new Long(1), suggested.getSuggested()); assertEquals(new Long(1), suggested.getIncrement()); }