Entitler { public List<Entitlement> bindByProducts(String[] productIds, String consumeruuid, Date entitleDate, Collection<String> fromPools) throws AutobindDisabledForOwnerException, AutobindHypervisorDisabledException { Consumer c = consumerCurator.findByUuid(consumeruuid); Owner o = ownerCurator.findOwnerById(c.getOwnerId()); AutobindData data = AutobindData.create(c, o).on(entitleDate) .forProducts(productIds).withPools(fromPools); return bindByProducts(data); } @Inject  Entitler(PoolManager pm, ConsumerCurator cc, I18n i18n, EventFactory evtFactory,
        EventSink sink, EntitlementRulesTranslator messageTranslator,
        EntitlementCurator entitlementCurator, Configuration config,
        OwnerCurator ownerCurator, PoolCurator poolCurator,
        ProductManager productManager, ProductServiceAdapter productAdapter, ContentManager contentManager,
        ConsumerTypeCurator ctc); List<Entitlement> bindByPoolQuantity(Consumer consumer, String poolId, Integer quantity); List<Entitlement> bindByPoolQuantities(String consumeruuid,
        Map<String, Integer> poolIdAndQuantities); List<Entitlement> bindByPoolQuantities(Consumer consumer,
        Map<String, Integer> poolIdAndQuantities); void adjustEntitlementQuantity(Consumer consumer, Entitlement ent,
        Integer quantity); List<Entitlement> bindByProducts(String[] productIds,
        String consumeruuid, Date entitleDate, Collection<String> fromPools); List<Entitlement> bindByProducts(AutobindData data); @Transactional List<Entitlement> bindByProducts(AutobindData data, boolean force); List<PoolQuantity> getDryRun(Consumer consumer, Owner owner,
        String serviceLevelOverride); int revokeUnmappedGuestEntitlements(Consumer consumer); int revokeUnmappedGuestEntitlements(); void sendEvents(List<Entitlement> entitlements);  }
@Test public void bindByProductsString() throws Exception { Owner owner = new Owner("o1"); owner.setId(TestUtil.randomString()); String[] pids = {"prod1", "prod2", "prod3"}; when(cc.findByUuid(eq("abcd1234"))).thenReturn(consumer); when(consumer.getOwnerId()).thenReturn(owner.getOwnerId()); when(ownerCurator.findOwnerById(eq(owner.getId()))).thenReturn(owner); entitler.bindByProducts(pids, "abcd1234", null, null); AutobindData data = AutobindData.create(consumer, this.owner).forProducts(pids); verify(pm).entitleByProducts(eq(data)); }
@Test public void bindByProducts() throws Exception { String[] pids = {"prod1", "prod2", "prod3"}; AutobindData data = AutobindData.create(consumer, owner).forProducts(pids); entitler.bindByProducts(data); verify(pm).entitleByProducts(data); }
@Test public void testRevokesLapsedUnmappedGuestEntitlementsOnAutoHeal() throws Exception { Owner owner1 = new Owner("o1"); owner1.setId(TestUtil.randomString()); when(ownerCurator.findOwnerById(eq(owner1.getId()))).thenReturn(owner1); Product product = TestUtil.createProduct(); Pool p1 = TestUtil.createPool(owner1, product); p1.setAttribute(Pool.Attributes.UNMAPPED_GUESTS_ONLY, "true"); Date thirtySixHoursAgo = new Date(new Date().getTime() - 36L * 60L * 60L * 1000L); Date twelveHoursAgo = new Date(new Date().getTime() - 12L * 60L * 60L * 1000L); Consumer c; c = TestUtil.createConsumer(owner1); c.setCreated(thirtySixHoursAgo); c.setFact("virt.uuid", "1"); Entitlement e1 = TestUtil.createEntitlement(owner1, c, p1, null); e1.setEndDateOverride(twelveHoursAgo); Set<Entitlement> entitlementSet1 = new HashSet<>(); entitlementSet1.add(e1); p1.setEntitlements(entitlementSet1); CandlepinQuery cqmock = mock(CandlepinQuery.class); when(cqmock.iterator()).thenReturn(Collections.singletonList(e1).iterator()); when(entitlementCurator.findByPoolAttribute(eq(c), eq("unmapped_guests_only"), eq("true"))) .thenReturn(cqmock); when(config.getInt(ConfigProperties.ENTITLER_BULK_SIZE)).thenReturn(1000); String[] pids = {product.getId(), "prod2"}; when(cc.findByUuid(eq("abcd1234"))).thenReturn(c); entitler.bindByProducts(pids, "abcd1234", null, null); AutobindData data = AutobindData.create(c, owner1).forProducts(pids); verify(pm).entitleByProducts(eq(data)); verify(pm).revokeEntitlements(Collections.singletonList(e1)); }
@Test public void testDevPoolCreationAtBind() throws Exception { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<>(); Product p = TestUtil.createProduct("test-product", "Test Product"); p.setAttribute(Product.Attributes.SUPPORT_LEVEL, "Premium"); devProdDTOs.add(p.toDTO()); Pool devPool = mock(Pool.class); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p.getId()); when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false); when(poolCurator.hasActiveEntitlementPools(eq(owner.getId()), nullable(Date.class))).thenReturn(true); doReturn(devProdDTOs).when(productAdapter).getProductsByIds(eq(owner.getKey()), anyList()); this.mockProductImport(p); this.mockContentImport(Collections.emptyMap()); when(pm.createPool(any(Pool.class))).thenReturn(devPool); when(devPool.getId()).thenReturn("test_pool_id"); AutobindData ad = new AutobindData(devSystem, owner); entitler.bindByProducts(ad); verify(pm).createPool(any(Pool.class)); }
@Test public void testDevPoolCreationAtBindFailStandalone() { Owner owner = TestUtil.createOwner("o"); Product p = TestUtil.createProduct("test-product", "Test Product"); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p.getId()); devSystem.addInstalledProduct(new ConsumerInstalledProduct(p)); when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(true); AutobindData ad = new AutobindData(devSystem, owner); assertThrows(ForbiddenException.class, () -> entitler.bindByProducts(ad)); }
@Test public void testDevPoolCreationAtBindFailNotActive() { Owner owner = TestUtil.createOwner("o"); Product p = TestUtil.createProduct("test-product", "Test Product"); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p.getId()); devSystem.addInstalledProduct(new ConsumerInstalledProduct(p)); when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false); AutobindData ad = new AutobindData(devSystem, owner); assertThrows(ForbiddenException.class, () -> entitler.bindByProducts(ad)); }
@Test public void testDevPoolCreationAtBindFailNoSkuProduct() throws Exception { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<>(); Product p = TestUtil.createProduct("test-product", "Test Product"); Product ip = TestUtil.createProduct("test-product-installed", "Installed Test Product"); devProdDTOs.add(ip.toDTO()); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p.getId()); devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip)); when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false); when(poolCurator.hasActiveEntitlementPools(eq(owner.getId()), nullable(Date.class))).thenReturn(true); doReturn(devProdDTOs).when(productAdapter).getProductsByIds(eq(owner.getKey()), anyList()); mockProductImport(p, ip); mockContentImport(); AutobindData ad = new AutobindData(devSystem, owner); try { entitler.bindByProducts(ad); } catch (ForbiddenException fe) { assertEquals(i18n.tr("SKU product not available to this development unit: \"{0}\"", p.getId()), fe.getMessage()); } }