Search in sources :

Example 6 with PoolRules

use of org.candlepin.policy.js.pool.PoolRules in project candlepin by candlepin.

the class PoolRulesStackDerivedTest method setUp.

@Before
public void setUp() {
    // Load the default production rules:
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCuratorMock.getUpdated()).thenReturn(new Date());
    when(rulesCuratorMock.getRules()).thenReturn(rules);
    when(configMock.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    poolRules = new PoolRules(poolManagerMock, configMock, entCurMock, ownerProductCuratorMock, productCurator);
    principal = TestUtil.createOwnerPrincipal();
    owner = principal.getOwners().get(0);
    ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.SYSTEM);
    ctype.setId("test-ctype");
    consumer = new Consumer("consumer", "registeredbybob", owner, ctype);
    // Two subtly different products stacked together:
    prod1 = TestUtil.createProduct("prod1", "prod1");
    prod1.setAttribute(Product.Attributes.VIRT_LIMIT, "2");
    prod1.setAttribute(Product.Attributes.STACKING_ID, STACK);
    prod1.setAttribute("testattr1", "1");
    when(ownerProductCuratorMock.getProductById(owner, prod1.getId())).thenReturn(prod1);
    prod2 = TestUtil.createProduct("prod2", "prod2");
    prod2.setAttribute(Product.Attributes.VIRT_LIMIT, "unlimited");
    prod2.setAttribute(Product.Attributes.STACKING_ID, STACK);
    prod2.setAttribute("testattr2", "2");
    when(ownerProductCuratorMock.getProductById(owner, prod2.getId())).thenReturn(prod2);
    prod3 = TestUtil.createProduct("prod3", "prod3");
    prod3.setAttribute(Product.Attributes.VIRT_LIMIT, "9");
    prod3.setAttribute(Product.Attributes.STACKING_ID, STACK + "3");
    prod3.setAttribute("testattr2", "2");
    when(ownerProductCuratorMock.getProductById(owner, prod3.getId())).thenReturn(prod3);
    provided1 = TestUtil.createProduct();
    provided2 = TestUtil.createProduct();
    provided3 = TestUtil.createProduct();
    provided4 = TestUtil.createProduct();
    // Create three subscriptions with various start/end dates:
    sub1 = createStackedVirtSub(owner, prod1, TestUtil.createDate(2010, 1, 1), TestUtil.createDate(2015, 1, 1));
    sub1.getProvidedProducts().add(provided1.toDTO());
    pool1 = copyFromSub(sub1);
    sub2 = createStackedVirtSub(owner, prod2, TestUtil.createDate(2011, 1, 1), TestUtil.createDate(2017, 1, 1));
    sub2.getProvidedProducts().add(provided2.toDTO());
    pool2 = copyFromSub(sub2);
    sub3 = createStackedVirtSub(owner, prod2, TestUtil.createDate(2012, 1, 1), TestUtil.createDate(2020, 1, 1));
    sub3.getProvidedProducts().add(provided3.toDTO());
    pool3 = copyFromSub(sub3);
    sub4 = createStackedVirtSub(owner, prod3, TestUtil.createDate(2012, 1, 1), TestUtil.createDate(2020, 1, 1));
    sub4.getProvidedProducts().add(provided4.toDTO());
    pool4 = copyFromSub(sub4);
    // Initial entitlement from one of the pools:
    stackedEnts.add(createEntFromPool(pool2));
    CandlepinQuery cqmock = mock(CandlepinQuery.class);
    when(cqmock.list()).thenReturn(stackedEnts);
    when(entCurMock.findByStackId(consumer, STACK)).thenReturn(cqmock);
    pool2.setAttribute(Product.Attributes.VIRT_LIMIT, "60");
    pool4.setAttribute(Product.Attributes.VIRT_LIMIT, "80");
    List<Pool> reqPools = new ArrayList<>();
    reqPools.add(pool2);
    Map<String, Entitlement> entitlements = new HashMap<>();
    entitlements.put(pool2.getId(), stackedEnts.get(0));
    Map<String, Map<String, String>> attributes = new HashMap<>();
    attributes.put(pool2.getId(), PoolHelper.getFlattenedAttributes(pool2));
    when(poolManagerMock.createPools(Matchers.anyListOf(Pool.class))).then(returnsFirstArg());
    List<Pool> resPools = PoolHelper.createHostRestrictedPools(poolManagerMock, consumer, reqPools, entitlements, attributes, productCurator);
    stackDerivedPool = resPools.get(0);
    reqPools.clear();
    reqPools.add(pool4);
    entitlements.clear();
    entitlements.put(pool4.getId(), createEntFromPool(pool4));
    attributes.clear();
    attributes.put(pool4.getId(), PoolHelper.getFlattenedAttributes(pool4));
    stackDerivedPool2 = PoolHelper.createHostRestrictedPools(poolManagerMock, consumer, reqPools, entitlements, attributes, productCurator).get(0);
}
Also used : PoolRules(org.candlepin.policy.js.pool.PoolRules) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CandlepinQuery(org.candlepin.model.CandlepinQuery) PoolRules(org.candlepin.policy.js.pool.PoolRules) Rules(org.candlepin.model.Rules) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) Pool(org.candlepin.model.Pool) ConsumerType(org.candlepin.model.ConsumerType) Entitlement(org.candlepin.model.Entitlement) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 7 with PoolRules

use of org.candlepin.policy.js.pool.PoolRules in project candlepin by candlepin.

the class PoolRulesTest method setUp.

@Before
public void setUp() {
    // Load the default production rules:
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCuratorMock.getUpdated()).thenReturn(new Date());
    when(rulesCuratorMock.getRules()).thenReturn(rules);
    when(configMock.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    poolRules = new PoolRules(poolManagerMock, configMock, entCurMock, ownerProdCuratorMock, productCurator);
    principal = TestUtil.createOwnerPrincipal();
    owner = principal.getOwners().get(0);
}
Also used : PoolRules(org.candlepin.policy.js.pool.PoolRules) InputStream(java.io.InputStream) PoolRules(org.candlepin.policy.js.pool.PoolRules) Rules(org.candlepin.model.Rules) Date(java.util.Date) Before(org.junit.Before)

Example 8 with PoolRules

use of org.candlepin.policy.js.pool.PoolRules in project candlepin by candlepin.

the class PoolManagerTest method createPoolsForExistingSubscriptionsNoneExist.

@Test
public void createPoolsForExistingSubscriptionsNoneExist() {
    Owner owner = this.getOwner();
    PoolRules pRules = new PoolRules(manager, mockConfig, entitlementCurator, mockOwnerProductCurator, mockProductCurator);
    List<Subscription> subscriptions = new ArrayList<>();
    Product prod = TestUtil.createProduct();
    Set<Product> products = new HashSet<>();
    products.add(prod);
    prod.setAttribute(Product.Attributes.VIRT_LIMIT, "4");
    // productCache.addProducts(products);
    Subscription s = TestUtil.createSubscription(owner, prod);
    subscriptions.add(s);
    this.mockProducts(owner, prod);
    when(mockSubAdapter.getSubscriptions(any(Owner.class))).thenReturn(subscriptions);
    when(mockConfig.getBoolean(ConfigProperties.STANDALONE)).thenReturn(false);
    List<Pool> existingPools = new LinkedList<>();
    List<Pool> newPools = pRules.createAndEnrichPools(s, existingPools);
    assertEquals(newPools.size(), 2);
    assertTrue(newPools.get(0).getSourceSubscription().getSubscriptionSubKey().equals("derived") || newPools.get(1).getSourceSubscription().getSubscriptionSubKey().equals("derived"));
    assertTrue(newPools.get(0).getSourceSubscription().getSubscriptionSubKey().startsWith("master") || newPools.get(1).getSourceSubscription().getSubscriptionSubKey().startsWith("master"));
}
Also used : Owner(org.candlepin.model.Owner) PoolRules(org.candlepin.policy.js.pool.PoolRules) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with PoolRules

use of org.candlepin.policy.js.pool.PoolRules in project candlepin by candlepin.

the class PoolManagerTest method createPoolsForPoolBonusExist.

@Test(expected = IllegalStateException.class)
public void createPoolsForPoolBonusExist() {
    Owner owner = this.getOwner();
    PoolRules pRules = new PoolRules(manager, mockConfig, entitlementCurator, mockOwnerProductCurator, mockProductCurator);
    Product prod = TestUtil.createProduct();
    prod.setAttribute(Product.Attributes.VIRT_LIMIT, "4");
    List<Pool> existingPools = new LinkedList<>();
    Pool p = TestUtil.createPool(prod);
    p.setSourceSubscription(new SourceSubscription(TestUtil.randomString(), "derived"));
    existingPools.add(p);
    pRules.createAndEnrichPools(p, existingPools);
    List<Pool> newPools = pRules.createAndEnrichPools(p, existingPools);
    assertEquals(1, newPools.size());
    assertEquals("master", newPools.get(0).getSourceSubscription().getSubscriptionSubKey());
}
Also used : Owner(org.candlepin.model.Owner) SourceSubscription(org.candlepin.model.SourceSubscription) PoolRules(org.candlepin.policy.js.pool.PoolRules) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 10 with PoolRules

use of org.candlepin.policy.js.pool.PoolRules in project candlepin by candlepin.

the class PoolManagerTest method createPoolsForExistingPoolNoneExist.

@Test
public void createPoolsForExistingPoolNoneExist() {
    Owner owner = this.getOwner();
    PoolRules pRules = new PoolRules(manager, mockConfig, entitlementCurator, mockOwnerProductCurator, mockProductCurator);
    Product prod = TestUtil.createProduct();
    prod.setAttribute(Product.Attributes.VIRT_LIMIT, "4");
    Pool p = TestUtil.createPool(owner, prod);
    List<Pool> existingPools = new LinkedList<>();
    List<Pool> newPools = pRules.createAndEnrichPools(p, existingPools);
    assertEquals(2, newPools.size());
    assertTrue(newPools.get(0).getSourceSubscription().getSubscriptionSubKey().equals("derived") || newPools.get(1).getSourceSubscription().getSubscriptionSubKey().equals("derived"));
    assertTrue(newPools.get(0).getSourceSubscription().getSubscriptionSubKey().startsWith("master") || newPools.get(1).getSourceSubscription().getSubscriptionSubKey().startsWith("master"));
}
Also used : Owner(org.candlepin.model.Owner) PoolRules(org.candlepin.policy.js.pool.PoolRules) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

PoolRules (org.candlepin.policy.js.pool.PoolRules)15 Pool (org.candlepin.model.Pool)12 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)11 Product (org.candlepin.model.Product)11 Test (org.junit.Test)11 SourceSubscription (org.candlepin.model.SourceSubscription)10 Subscription (org.candlepin.model.dto.Subscription)8 Owner (org.candlepin.model.Owner)7 LinkedList (java.util.LinkedList)6 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Rules (org.candlepin.model.Rules)4 Before (org.junit.Before)4 Date (java.util.Date)3 Consumer (org.candlepin.model.Consumer)2 ConsumerType (org.candlepin.model.ConsumerType)2 Matchers.anyString (org.mockito.Matchers.anyString)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1