Search in sources :

Example 1 with SuggestedQuantity

use of org.candlepin.policy.js.quantity.SuggestedQuantity in project candlepin by candlepin.

the class CalculatedAttributesUtilTest method testCalculatedAttributesPresent.

@Test
public void testCalculatedAttributesPresent() {
    for (PoolComplianceType type : PoolComplianceType.values()) {
        when(mockPool.getComplianceType()).thenReturn(type);
        Date date = new Date();
        Map<String, String> attrs = attrUtil.buildCalculatedAttributes(mockPool, date);
        assertTrue(attrs.containsKey("compliance_type"));
    }
    SuggestedQuantity suggested = new SuggestedQuantity();
    suggested.setSuggested(1L);
    suggested.setIncrement(1L);
    Map<String, SuggestedQuantity> suggestedMap = new HashMap<>();
    suggestedMap.put(pool1.getId(), suggested);
    when(quantityRules.getSuggestedQuantities(any(List.class), any(Consumer.class), any(Date.class))).thenReturn(suggestedMap);
    Date date = new Date();
    attrUtil.setQuantityAttributes(pool1, consumer, date);
    assertTrue(pool1.getCalculatedAttributes().containsKey("suggested_quantity"));
    assertTrue(pool1.getCalculatedAttributes().containsKey("quantity_increment"));
}
Also used : Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) SuggestedQuantity(org.candlepin.policy.js.quantity.SuggestedQuantity) List(java.util.List) Date(java.util.Date) PoolComplianceType(org.candlepin.model.Pool.PoolComplianceType) Test(org.junit.Test)

Example 2 with SuggestedQuantity

use of org.candlepin.policy.js.quantity.SuggestedQuantity in project candlepin by candlepin.

the class CalculatedAttributesUtilTest method testQuantityIncrement.

@Test
public void testQuantityIncrement() {
    Product product2 = TestUtil.createProduct("blah", "blah");
    product2.setAttribute(Product.Attributes.INSTANCE_MULTIPLIER, "12");
    productCurator.create(product2);
    Pool pool2 = createPool(owner1, product2, 500L, TestUtil.createDate(2000, 1, 1), TestUtil.createDate(3000, 1, 1));
    SuggestedQuantity suggested = new SuggestedQuantity();
    suggested.setSuggested(1L);
    suggested.setIncrement(12L);
    Map<String, SuggestedQuantity> suggestedMap = new HashMap<>();
    suggestedMap.put(pool2.getId(), suggested);
    when(quantityRules.getSuggestedQuantities(any(List.class), any(Consumer.class), any(Date.class))).thenReturn(suggestedMap);
    Date date = new Date();
    attrUtil.setQuantityAttributes(pool2, consumer, date);
    assertEquals("1", pool2.getCalculatedAttributes().get("suggested_quantity"));
    assertEquals("12", pool2.getCalculatedAttributes().get("quantity_increment"));
}
Also used : Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) SuggestedQuantity(org.candlepin.policy.js.quantity.SuggestedQuantity) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) List(java.util.List) Date(java.util.Date) Test(org.junit.Test)

Example 3 with SuggestedQuantity

use of org.candlepin.policy.js.quantity.SuggestedQuantity in project candlepin by candlepin.

the class CalculatedAttributesUtil method setQuantityAttributes.

public void setQuantityAttributes(List<Pool> poolList, Consumer c, Date date) {
    if (c == null) {
        return;
    }
    Map<String, SuggestedQuantity> results = quantityRules.getSuggestedQuantities(poolList, c, date);
    for (Pool p : poolList) {
        SuggestedQuantity suggested = results.get(p.getId());
        Map<String, String> attrMap = p.getCalculatedAttributes();
        if (attrMap == null) {
            attrMap = new HashMap<>();
            p.setCalculatedAttributes(attrMap);
        }
        attrMap.put("suggested_quantity", String.valueOf(suggested.getSuggested()));
        attrMap.put("quantity_increment", String.valueOf(suggested.getIncrement()));
    }
}
Also used : SuggestedQuantity(org.candlepin.policy.js.quantity.SuggestedQuantity) Pool(org.candlepin.model.Pool)

Example 4 with SuggestedQuantity

use of org.candlepin.policy.js.quantity.SuggestedQuantity in project candlepin by candlepin.

the class ConsumerBindUtil method getQuantityToBind.

public int getQuantityToBind(Pool pool, Consumer consumer) {
    Date now = new Date();
    // If the pool is being attached in the future, calculate
    // suggested quantity on the start date
    Date onDate = now.before(pool.getStartDate()) ? pool.getStartDate() : now;
    SuggestedQuantity suggested = quantityRules.getSuggestedQuantity(pool, consumer, onDate);
    int quantity = Math.max(suggested.getIncrement().intValue(), suggested.getSuggested().intValue());
    // but whatever we do here, the bind will fail
    return quantity;
}
Also used : SuggestedQuantity(org.candlepin.policy.js.quantity.SuggestedQuantity) Date(java.util.Date)

Aggregations

SuggestedQuantity (org.candlepin.policy.js.quantity.SuggestedQuantity)4 Date (java.util.Date)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Consumer (org.candlepin.model.Consumer)2 Pool (org.candlepin.model.Pool)2 Test (org.junit.Test)2 PoolComplianceType (org.candlepin.model.Pool.PoolComplianceType)1 Product (org.candlepin.model.Product)1