ConsumerBindUtil { public void handleActivationKeys(Consumer consumer, List<ActivationKey> keys, boolean autoattachDisabledForOwner) throws AutobindDisabledForOwnerException, AutobindHypervisorDisabledException { boolean listSuccess = false; boolean scaEnabledForAny = false; boolean isAutoheal = BooleanUtils.isTrue(consumer.isAutoheal()); for (ActivationKey key : keys) { boolean keySuccess = true; boolean scaEnabled = key.getOwner().isContentAccessEnabled(); scaEnabledForAny |= scaEnabled; keySuccess &= handleActivationKeyServiceLevel(consumer, key.getServiceLevel(), key.getOwner()); handleActivationKeyOverrides(consumer, key.getContentOverrides()); handleActivationKeyRelease(consumer, key.getReleaseVer()); handleActivationKeyUsage(consumer, key.getUsage()); handleActivationKeyRole(consumer, key.getRole()); handleActivationKeyAddons(consumer, key.getAddOns()); if (Boolean.TRUE.equals(key.isAutoAttach())) { if (autoattachDisabledForOwner) { log.warn("Auto-attach disabled for owner; skipping auto-attach for consumer with " + "activation key: {}, {}", consumer.getUuid(), key.getName()); } else if (scaEnabled) { log.warn("Owner is using simple content access; skipping auto-attach for consumer with " + "activation key: {}, {}", consumer.getUuid(), key.getName()); } else if (!isAutoheal) { log.warn("Auto-heal disabled for consumer; skipping auto-attach for consumer with " + "activation key: {}, {}", consumer.getUuid(), key.getName()); } else { this.handleActivationKeyAutoBind(consumer, key); } } else { keySuccess &= handleActivationKeyPools(consumer, key); } listSuccess |= keySuccess; } if (!listSuccess && !scaEnabledForAny) { throw new BadRequestException( i18n.tr("None of the subscriptions on the activation key were available for attaching.")); } } @Inject  ConsumerBindUtil(Entitler entitler, I18n i18n,
        ConsumerContentOverrideCurator consumerContentOverrideCurator,
        OwnerCurator ownerCurator, QuantityRules quantityRules, ServiceLevelValidator serviceLevelValidator); void handleActivationKeys(Consumer consumer, List<ActivationKey> keys,
        boolean autoattachDisabledForOwner); int getQuantityToBind(Pool pool, Consumer consumer); void validateServiceLevel(String ownerId, String serviceLevel);  }
@Test public void registerWithKeyWithPoolAndInstalledProductsAutoAttach() throws Exception { Product prod = TestUtil.createProduct(); String[] prodIds = new String[]{prod.getId()}; Pool pool = TestUtil.createPool(owner, prod); pool.setId("id-string"); List<String> poolIds = new ArrayList<>(); poolIds.add(pool.getId()); List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); key1.addPool(pool, 0L); key1.setAutoAttach(true); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); Set<ConsumerInstalledProduct> cips = new HashSet<>(); ConsumerInstalledProduct cip = new ConsumerInstalledProduct(consumer, prod); cips.add(cip); consumer.setInstalledProducts(cips); AutobindData ad = new AutobindData(consumer, owner).withPools(poolIds).forProducts(prodIds); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); consumerBindUtil.handleActivationKeys(consumer, keys, false); verify(entitler).bindByProducts(eq(ad)); }
@Test public void registerWithKeyWithInstalledProductsAutoAttach() throws Exception { Product prod = TestUtil.createProduct(); String[] prodIds = new String[]{prod.getId()}; List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); key1.setAutoAttach(true); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); Set<ConsumerInstalledProduct> cips = new HashSet<>(); ConsumerInstalledProduct cip = new ConsumerInstalledProduct(consumer, prod); cips.add(cip); consumer.setInstalledProducts(cips); AutobindData ad = new AutobindData(consumer, owner).forProducts(prodIds); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); consumerBindUtil.handleActivationKeys(consumer, keys, false); verify(entitler).bindByProducts(eq(ad)); }
@Test public void registerWithKeyWithInstalledProductsPlusAutoAttach() throws Exception { Product prod1 = TestUtil.createProduct(); Product prod2 = TestUtil.createProduct(); String[] prodIds = new String[]{prod1.getId(), prod2.getId()}; List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); key1.addProduct(prod2); key1.setAutoAttach(true); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); Set<ConsumerInstalledProduct> cips = new HashSet<>(); ConsumerInstalledProduct cip = new ConsumerInstalledProduct(consumer, prod1); cips.add(cip); consumer.setInstalledProducts(cips); AutobindData ad = new AutobindData(consumer, owner).forProducts(prodIds); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); consumerBindUtil.handleActivationKeys(consumer, keys, false); verify(entitler).bindByProducts(eq(ad)); }
@Test public void registerFailWithKeyServiceLevelNotExist() throws Exception { List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); key1.setServiceLevel("I don't exist"); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); doThrow(new BadRequestException("exception")).when(serviceLevelValidator) .validate(eq(owner.getId()), eq(key1.getServiceLevel())); assertThrows(BadRequestException.class, () -> consumerBindUtil.handleActivationKeys(consumer, keys, false)); }
@Test public void registerPassWithKeyServiceLevelNotExistOtherKeysSucceed() throws Exception { List<ActivationKey> keys = mockActivationKeys(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); key1.setServiceLevel("I don't exist"); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); doThrow(new BadRequestException("exception")).when(serviceLevelValidator) .validate(eq(owner.getId()), eq(key1.getServiceLevel())); consumerBindUtil.handleActivationKeys(consumer, keys, false); }
@Test public void registerFailWithNoGoodKeyPool() throws Exception { List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); Product prod1 = TestUtil.createProduct(); Pool ghost = TestUtil.createPool(owner, prod1, 5); ghost.setId("ghost-pool"); key1.addPool(ghost, 10L); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); when(entitler.bindByPoolQuantity(eq(consumer), eq(ghost.getId()), eq(10))) .thenThrow(new ForbiddenException("fail")); assertThrows(BadRequestException.class, () -> consumerBindUtil.handleActivationKeys(consumer, keys, false)); }
@Test public void registerPassWithOneGoodKeyPool() throws Exception { List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); keys.add(key1); Product prod1 = TestUtil.createProduct(); Pool pool1 = TestUtil.createPool(owner, prod1, 5); pool1.setId("pool1"); key1.addPool(pool1, 10L); Product prod2 = TestUtil.createProduct(); Pool pool2 = TestUtil.createPool(owner, prod2, 5); pool2.setId("pool2"); key1.addPool(pool2, 10L); Product prod3 = TestUtil.createProduct(); Pool pool3 = TestUtil.createPool(owner, prod3, 5); pool3.setId("pool3"); key1.addPool(pool3, 5L); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); when(entitler.bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(10))) .thenThrow(new ForbiddenException("fail")); when(entitler.bindByPoolQuantity(eq(consumer), eq(pool2.getId()), eq(10))) .thenThrow(new ForbiddenException("fail")); consumerBindUtil.handleActivationKeys(consumer, keys, false); }
@Test public void registerPassWithOneGoodKey() throws Exception { List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); ActivationKey key2 = new ActivationKey("key2", owner); keys.add(key1); keys.add(key2); Product prod1 = TestUtil.createProduct(); Pool pool1 = TestUtil.createPool(owner, prod1, 5); pool1.setId("pool1"); key1.addPool(pool1, 10L); Product prod2 = TestUtil.createProduct(); Pool pool2 = TestUtil.createPool(owner, prod2, 5); pool2.setId("pool2"); key1.addPool(pool2, 10L); Product prod3 = TestUtil.createProduct(); Pool pool3 = TestUtil.createPool(owner, prod3, 5); pool3.setId("pool3"); key2.addPool(pool3, 5L); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); when(entitler.bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(10))) .thenThrow(new ForbiddenException("fail")); when(entitler.bindByPoolQuantity(eq(consumer), eq(pool2.getId()), eq(10))) .thenThrow(new ForbiddenException("fail")); consumerBindUtil.handleActivationKeys(consumer, keys, false); }
@Test public void registerPassWhenAutobindDisabledForOwnerAndKeyHasAutobindEnabled() throws Exception { List<ActivationKey> keys = new ArrayList<>(); ActivationKey key1 = new ActivationKey("key1", owner); key1.setAutoAttach(true); keys.add(key1); Product prod1 = TestUtil.createProduct(); Pool pool1 = TestUtil.createPool(owner, prod1, 5); pool1.setId("pool1"); key1.addPool(pool1, 10L); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); when(entitler.bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(10))) .thenThrow(new ForbiddenException("fail")); consumerBindUtil.handleActivationKeys(consumer, keys, true); }
@Test public void handleActivationKeysWithoutAutoAttachUsingSCA() throws Exception { this.owner.setContentAccessModeList(ContentAccessMode.ORG_ENVIRONMENT.toDatabaseValue()); this.owner.setContentAccessMode(ContentAccessMode.ORG_ENVIRONMENT.toDatabaseValue()); Pool pool1 = this.createTestPool(this.owner, 1); ActivationKey key1 = new ActivationKey("test_key-1", this.owner); key1.setAutoAttach(false); key1.addPool(pool1, 1L); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); consumerBindUtil.handleActivationKeys(consumer, Arrays.asList(key1), false); verify(this.entitler, times(1)).bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(1)); }
@Test public void handleActivationKeysWithoutAutoAttachUsingSCAWhenBindFails() throws Exception { this.owner.setContentAccessModeList(ContentAccessMode.ORG_ENVIRONMENT.toDatabaseValue()); this.owner.setContentAccessMode(ContentAccessMode.ORG_ENVIRONMENT.toDatabaseValue()); Pool pool1 = this.createTestPool(this.owner, 1); ActivationKey key1 = new ActivationKey("test_key-1", this.owner); key1.setAutoAttach(false); key1.addPool(pool1, 5L); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); doThrow(new ForbiddenException("exception")).when(this.entitler) .bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(5)); consumerBindUtil.handleActivationKeys(consumer, Arrays.asList(key1), false); verify(this.entitler, times(1)).bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(5)); }
@Test public void handleActivationKeysWithAutoAttachUsingSCA() throws Exception { this.owner.setContentAccessModeList(ContentAccessMode.ORG_ENVIRONMENT.toDatabaseValue()); this.owner.setContentAccessMode(ContentAccessMode.ORG_ENVIRONMENT.toDatabaseValue()); ActivationKey key1 = new ActivationKey("test_key-1", this.owner); key1.setAutoAttach(true); Consumer consumer = new Consumer("sys.example.com", null, null, this.systemConsumerType); ConsumerBindUtil consumerBindUtil = this.buildConsumerBindUtil(); consumerBindUtil.handleActivationKeys(consumer, Arrays.asList(key1), false); verify(this.entitler, never()).bindByProducts(any(AutobindData.class)); }