@Test public void testEntitleByProductsEmptyArray() throws Exception { Product product = TestUtil.createProduct(); List<Pool> pools = Util.newList(); Pool pool1 = TestUtil.createPool(product); pools.add(pool1); Date now = new Date(); ValidationResult result = mock(ValidationResult.class); // Setup an installed product for the consumer, we'll make the bind request // with no products specified, so this should get used instead: String[] installedPids = new String[] {product.getId()}; ComplianceStatus mockCompliance = new ComplianceStatus(now); mockCompliance.addNonCompliantProduct(installedPids[0]); when(complianceRules.getStatus(any(Consumer.class), any(Date.class))) .thenReturn(mockCompliance); when(mockPoolCurator.listAvailableEntitlementPools( any(Consumer.class), any(Owner.class), anyString(), eq(now), anyBoolean(), anyBoolean())) .thenReturn(pools); when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool1); when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt())) .thenReturn(result); when(result.isSuccessful()).thenReturn(true); List<PoolQuantity> bestPools = new ArrayList<PoolQuantity>(); bestPools.add(new PoolQuantity(pool1, 1)); when(autobindRules.selectBestPools( any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class))) .thenReturn(bestPools); // Make the call but provide a null array of product IDs (simulates healing): manager.entitleByProducts(TestUtil.createConsumer(o), null, now); verify(autobindRules) .selectBestPools( any(Consumer.class), eq(installedPids), any(List.class), eq(mockCompliance), any(String.class), any(Set.class)); }
@Test public void testRefreshPoolsRemovesExpiredSubscriptionsAlongWithItsPoolsAndEnts() { PreUnbindHelper preHelper = mock(PreUnbindHelper.class); Date expiredStart = TestUtil.createDate(2004, 5, 5); Date expiredDate = TestUtil.createDate(2005, 5, 5); List<Subscription> subscriptions = Util.newList(); Subscription sub = TestUtil.createSubscription(getOwner(), TestUtil.createProduct()); sub.setStartDate(expiredStart); sub.setEndDate(expiredDate); sub.setId("123"); subscriptions.add(sub); when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions); List<Pool> pools = Util.newList(); Pool p = TestUtil.createPool(sub.getOwner(), sub.getProduct()); p.setSubscriptionId(sub.getId()); p.setStartDate(expiredStart); p.setEndDate(expiredDate); p.setConsumed(1L); pools.add(p); when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(p); when(mockPoolCurator.listAvailableEntitlementPools( any(Consumer.class), any(Owner.class), anyString(), any(Date.class), anyBoolean(), anyBoolean())) .thenReturn(pools); List<Entitlement> poolEntitlements = Util.newList(); Entitlement ent = TestUtil.createEntitlement(); ent.setPool(p); ent.setQuantity(1); poolEntitlements.add(ent); when(mockPoolCurator.entitlementsIn(eq(p))).thenReturn(poolEntitlements); ValidationResult result = new ValidationResult(); when(preHelper.getResult()).thenReturn(result); this.manager.getRefresher().add(sub.getOwner()).run(); verify(mockSubAdapter).deleteSubscription(eq(sub)); verify(mockPoolCurator).delete(eq(p)); // Verify the entitlement was removed. verify(entCertAdapterMock).revokeEntitlementCertificates(eq(ent)); verify(entitlementCurator).delete(eq(ent)); }
@Test public void refreshPoolsCreatingPoolsForExistingSubscriptions() { List<Subscription> subscriptions = Util.newList(); List<Pool> pools = Util.newList(); Subscription s = TestUtil.createSubscription(getOwner(), TestUtil.createProduct()); subscriptions.add(s); when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions); when(mockPoolCurator.listAvailableEntitlementPools( any(Consumer.class), any(Owner.class), anyString(), any(Date.class), anyBoolean(), anyBoolean())) .thenReturn(pools); List<Pool> newPools = new LinkedList<Pool>(); Pool p = TestUtil.createPool(s.getProduct()); p.setSubscriptionId(s.getId()); newPools.add(p); when(poolRulesMock.createPools(s)).thenReturn(newPools); this.manager.getRefresher().add(getOwner()).run(); verify(this.mockPoolCurator, times(1)).create(any(Pool.class)); }
@Test public void testDevPoolCreationAtBindNoFailMissingInstalledProduct() throws Exception { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<ProductData>(); Product p = TestUtil.createProduct("test-product", "Test Product"); Product ip1 = TestUtil.createProduct("test-product-installed-1", "Installed Test Product 1"); Product ip2 = TestUtil.createProduct("test-product-installed-2", "Installed Test Product 2"); devProdDTOs.add(p.toDTO()); devProdDTOs.add(ip1.toDTO()); Pool activePool = TestUtil.createPool(owner, p); List<Pool> activeList = new ArrayList<Pool>(); activeList.add(activePool); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p.getId()); devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip1)); devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip2)); when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false); when(poolCurator.hasActiveEntitlementPools(eq(owner), any(Date.class))).thenReturn(true); when(productAdapter.getProductsByIds(any(Owner.class), any(List.class))) .thenReturn(devProdDTOs); this.mockProducts(owner, p, ip1, ip2); this.mockProductImport(owner, p, ip1, ip2); this.mockContentImport(owner, Collections.<String, Content>emptyMap()); Pool expectedPool = entitler.assembleDevPool(devSystem, p.getId()); when(pm.createPool(any(Pool.class))).thenReturn(expectedPool); AutobindData ad = new AutobindData(devSystem); entitler.bindByProducts(ad); }
@Test public void refreshPoolsCleanupPoolThatLostVirtLimit() { List<Subscription> subscriptions = Util.newList(); List<Pool> pools = Util.newList(); Subscription s = TestUtil.createSubscription(getOwner(), TestUtil.createProduct()); s.setId("01923"); subscriptions.add(s); Pool p = TestUtil.createPool(s.getProduct()); p.setSubscriptionId(s.getId()); p.setAttribute(PoolManager.DELETE_FLAG, "true"); pools.add(p); when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions); when(mockPoolCurator.listAvailableEntitlementPools( any(Consumer.class), any(Owner.class), anyString(), any(Date.class), anyBoolean(), anyBoolean())) .thenReturn(pools); List<PoolUpdate> updates = new LinkedList(); updates.add(new PoolUpdate(p, false, true, false)); when(poolRulesMock.updatePools(s, pools)).thenReturn(updates); this.manager.getRefresher().add(getOwner()).run(); verify(this.mockPoolCurator, times(1)).delete(any(Pool.class)); }
@Test public void testDevPoolCreationAtBind() throws Exception { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<ProductData>(); Product p = TestUtil.createProduct("test-product", "Test Product"); p.setAttribute("support_level", "Premium"); devProdDTOs.add(p.toDTO()); Pool activePool = TestUtil.createPool(owner, p); List<Pool> activeList = new ArrayList<Pool>(); activeList.add(activePool); 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), any(Date.class))).thenReturn(true); when(productAdapter.getProductsByIds(eq(owner), any(List.class))).thenReturn(devProdDTOs); this.mockProducts(owner, p); this.mockProductImport(owner, p); this.mockContentImport(owner, Collections.<String, Content>emptyMap()); when(pm.createPool(any(Pool.class))).thenReturn(devPool); when(devPool.getId()).thenReturn("test_pool_id"); AutobindData ad = new AutobindData(devSystem); entitler.bindByProducts(ad); verify(pm).createPool(any(Pool.class)); }
private Pool findPool(String poolId) { Pool pool = poolCurator.find(poolId); if (pool == null) { throw new BadRequestException(i18n.tr("Pool with id {0} could not be found.", poolId)); } return pool; }
@SuppressWarnings("unchecked") @Test public void testEntitleWithADate() throws Exception { Product product = TestUtil.createProduct(); List<Pool> pools = Util.newList(); Pool pool1 = TestUtil.createPool(product); pools.add(pool1); Pool pool2 = TestUtil.createPool(product); pools.add(pool2); Date now = new Date(); ValidationResult result = mock(ValidationResult.class); when(mockPoolCurator.listAvailableEntitlementPools( any(Consumer.class), any(Owner.class), any(String.class), eq(now), anyBoolean(), anyBoolean())) .thenReturn(pools); when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool1); when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt())) .thenReturn(result); when(result.isSuccessful()).thenReturn(true); List<PoolQuantity> bestPools = new ArrayList<PoolQuantity>(); bestPools.add(new PoolQuantity(pool1, 1)); when(autobindRules.selectBestPools( any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class))) .thenReturn(bestPools); List<Entitlement> e = manager.entitleByProducts(TestUtil.createConsumer(o), new String[] {product.getId()}, now); assertNotNull(e); assertEquals(e.size(), 1); }
@Test public void testRevokeCleansUpPoolsWithSourceEnt() throws Exception { Entitlement e = new Entitlement( pool, TestUtil.createConsumer(o), pool.getStartDate(), pool.getEndDate(), 1); List<Pool> poolsWithSource = createPoolsWithSourceEntitlement(e, product); when(mockPoolCurator.listBySourceEntitlement(e)).thenReturn(poolsWithSource); PreUnbindHelper preHelper = mock(PreUnbindHelper.class); ValidationResult result = new ValidationResult(); when(preHelper.getResult()).thenReturn(result); when(mockConfig.standalone()).thenReturn(true); when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool); manager.revokeEntitlement(e); verify(entCertAdapterMock).revokeEntitlementCertificates(e); verify(entitlementCurator).delete(e); }
private Pool entitlementPoolWithMembersAndExpiration( Owner theOwner, Product product, final int currentMembers, final int maxMembers, Date expiry) { Pool p = createPoolAndSub(theOwner, product, Long.valueOf(maxMembers), new Date(), expiry); for (int i = 0; i < currentMembers; i++) { Consumer c = createConsumer(theOwner); Entitlement e = createEntitlement(theOwner, c, p, null); e.setQuantity(1); entitlementCurator.create(e); p.getEntitlements().add(e); poolCurator.merge(p); } poolCurator.refresh(p); return p; }
@Test public void testCleanup() throws Exception { Pool p = createPoolWithEntitlements(); when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(p); when(mockPoolCurator.entitlementsIn(p)) .thenReturn(new ArrayList<Entitlement>(p.getEntitlements())); PreUnbindHelper preHelper = mock(PreUnbindHelper.class); ValidationResult result = new ValidationResult(); when(preHelper.getResult()).thenReturn(result); manager.deletePool(p); // And the pool should be deleted: verify(mockPoolCurator).delete(p); // Check that appropriate events were sent out: verify(eventFactory).poolDeleted(p); verify(mockEventSink, times(3)).sendEvent((Event) any()); }
private void bindByPoolErrorTest(String msg) { try { String poolid = "pool10"; Pool pool = mock(Pool.class); Map<String, ValidationResult> fakeResult = new HashMap<String, ValidationResult>(); fakeResult.put(poolid, fakeOutResult(msg)); EntitlementRefusedException ere = new EntitlementRefusedException(fakeResult); when(pool.getId()).thenReturn(poolid); when(poolCurator.find(eq(poolid))).thenReturn(pool); Map<String, Integer> pQs = new HashMap<String, Integer>(); pQs.put(poolid, 1); when(pm.entitleByPools(eq(consumer), eq(pQs))).thenThrow(ere); entitler.bindByPoolQuantity(consumer, poolid, 1); } catch (EntitlementRefusedException e) { fail(msg + ": threw unexpected error"); } }
@Test public void testRefreshPoolsForDeactivatingPools() { List<Subscription> subscriptions = Util.newList(); List<Pool> pools = Util.newList(); Pool p = TestUtil.createPool(TestUtil.createProduct()); p.setSubscriptionId("112"); pools.add(p); when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions); when(mockPoolCurator.listAvailableEntitlementPools( any(Consumer.class), any(Owner.class), anyString(), any(Date.class), anyBoolean(), anyBoolean())) .thenReturn(pools); this.manager.getRefresher().add(getOwner()).run(); verify(this.manager).deletePool(same(p)); }
@Test(expected = ForbiddenException.class) public void testDevPoolCreationAtBindFailNotActive() throws Exception { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<ProductData>(); Product p = TestUtil.createProduct("test-product", "Test Product"); devProdDTOs.add(p.toDTO()); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p.getId()); devSystem.addInstalledProduct(new ConsumerInstalledProduct(p)); when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false); when(poolCurator.hasActiveEntitlementPools(eq(owner), any(Date.class))).thenReturn(false); when(productAdapter.getProductsByIds(any(Owner.class), any(List.class))) .thenReturn(devProdDTOs); when(ownerProductCurator.getProductById(eq(owner), eq(p.getId()))).thenReturn(p); AutobindData ad = new AutobindData(devSystem); entitler.bindByProducts(ad); }
@Test public void testRevokeAllEntitlements() { Consumer c = TestUtil.createConsumer(o); Entitlement e1 = new Entitlement(pool, c, pool.getStartDate(), pool.getEndDate(), 1); Entitlement e2 = new Entitlement(pool, c, pool.getStartDate(), pool.getEndDate(), 1); List<Entitlement> entitlementList = new ArrayList<Entitlement>(); entitlementList.add(e1); entitlementList.add(e2); when(entitlementCurator.listByConsumer(eq(c))).thenReturn(entitlementList); when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool); PreUnbindHelper preHelper = mock(PreUnbindHelper.class); ValidationResult result = new ValidationResult(); when(preHelper.getResult()).thenReturn(result); int total = manager.revokeAllEntitlements(c); assertEquals(2, total); verify(entitlementCurator, never()).listModifying(any(Entitlement.class)); }
@Test public void testDevPoolCreationAtBindFailNoSkuProduct() throws Exception { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<ProductData>(); Product p = TestUtil.createProduct("test-product", "Test Product"); Product ip = TestUtil.createProduct("test-product-installed", "Installed Test Product"); devProdDTOs.add(ip.toDTO()); Pool activePool = TestUtil.createPool(owner, p); List<Pool> activeList = new ArrayList<Pool>(); activeList.add(activePool); 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), any(Date.class))).thenReturn(true); when(productAdapter.getProductsByIds(any(Owner.class), any(List.class))) .thenReturn(devProdDTOs); when(ownerProductCurator.getProductById(eq(owner), eq(p.getId()))).thenReturn(p); when(ownerProductCurator.getProductById(eq(owner), eq(ip.getId()))).thenReturn(ip); mockUpdateProduct(p, owner); mockUpdateProduct(ip, owner); mockProductImport(owner, p, ip); mockContentImport(owner, new Content[] {}); AutobindData ad = new AutobindData(devSystem); try { entitler.bindByProducts(ad); } catch (ForbiddenException fe) { assertEquals( i18n.tr("SKU product not available to this development unit: ''{0}''", p.getId()), fe.getMessage()); } }
@Test public void testUeberEntitlementIsGenerated() throws Exception { or.createUeberCertificate(principal, owner.getKey()); assertNotNull(poolCurator.findUeberPool(owner)); }