killbill-aplcache

Extending limits so they can be represented in products but

2/22/2013 11:55:13 PM

Details

diff --git a/analytics/src/test/java/com/ning/billing/analytics/MockProduct.java b/analytics/src/test/java/com/ning/billing/analytics/MockProduct.java
index 333b16f..452471d 100644
--- a/analytics/src/test/java/com/ning/billing/analytics/MockProduct.java
+++ b/analytics/src/test/java/com/ning/billing/analytics/MockProduct.java
@@ -16,6 +16,7 @@
 
 package com.ning.billing.analytics;
 
+import com.ning.billing.catalog.api.Limit;
 import com.ning.billing.catalog.api.Product;
 import com.ning.billing.catalog.api.ProductCategory;
 
@@ -60,4 +61,14 @@ public class MockProduct implements Product {
     public boolean isRetired() {
         return false;
     }
+
+    @Override
+    public Limit[] getLimits() {
+        return new Limit[0];
+    }
+
+    @Override
+    public boolean compliesWithLimits(String unit, double value) {
+        return false;
+    }
 }
diff --git a/api/src/main/java/com/ning/billing/catalog/api/Product.java b/api/src/main/java/com/ning/billing/catalog/api/Product.java
index 16b540a..52fe527 100644
--- a/api/src/main/java/com/ning/billing/catalog/api/Product.java
+++ b/api/src/main/java/com/ning/billing/catalog/api/Product.java
@@ -16,6 +16,7 @@
 
 package com.ning.billing.catalog.api;
 
+
 /**
  * The interface {@code Product}
  */
@@ -53,9 +54,21 @@ public interface Product {
 
     /**
      * 
-     * @return the name of tha catalog where this {@code Product} has been defined
+     * @return the name of the catalog where this {@code Product} has been defined
      */
     public String getCatalogName();
 
+    /**
+     * 
+     * @return the limits associated with this product
+     */
+    public Limit[] getLimits();
+
+    /**
+     * 
+     * @return whether the given unit-value pair meets the limits of the product
+     */
+    public boolean compliesWithLimits(String unit, double value);
+
 
 }
diff --git a/catalog/src/main/java/com/ning/billing/catalog/DefaultPlanPhase.java b/catalog/src/main/java/com/ning/billing/catalog/DefaultPlanPhase.java
index 336605c..a8fe365 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/DefaultPlanPhase.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/DefaultPlanPhase.java
@@ -163,7 +163,7 @@ public class DefaultPlanPhase extends ValidatingConfig<StandaloneCatalog> implem
     public boolean compliesWithLimits(String unit, double value) {
         Limit l = findLimit(unit);
         if (l == null) {
-            return true;
+            return getPlan().getProduct().compliesWithLimits(unit, value);
         }
         return l.compliesWith(value);
     }
diff --git a/catalog/src/main/java/com/ning/billing/catalog/DefaultProduct.java b/catalog/src/main/java/com/ning/billing/catalog/DefaultProduct.java
index 3251517..92873cd 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/DefaultProduct.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/DefaultProduct.java
@@ -26,6 +26,7 @@ import javax.xml.bind.annotation.XmlIDREF;
 import java.net.URI;
 import java.util.Arrays;
 
+import com.ning.billing.catalog.api.Limit;
 import com.ning.billing.catalog.api.Product;
 import com.ning.billing.catalog.api.ProductCategory;
 import com.ning.billing.util.config.catalog.ValidatingConfig;
@@ -54,6 +55,10 @@ public class DefaultProduct extends ValidatingConfig<StandaloneCatalog> implemen
     @XmlIDREF
     @XmlElement(name = "addonProduct", required = true)
     private DefaultProduct[] available = EMPTY_PRODUCT_LIST;
+    
+    @XmlElementWrapper(name = "limits", required = false)
+    @XmlElement(name = "limit", required = true)
+    private DefaultLimit[] limits = new DefaultLimit[0];
 
     //Not included in XML
     private String catalogName;
@@ -115,6 +120,32 @@ public class DefaultProduct extends ValidatingConfig<StandaloneCatalog> implemen
     }
 
     @Override
+    public DefaultLimit[] getLimits() {
+        return limits;
+    }
+    
+    
+    protected Limit findLimit(String unit) {
+        for(Limit limit: limits) {
+            if(limit.getUnit().getName().equals(unit) ) {
+                    return limit;
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public boolean compliesWithLimits(String unit, double value) {
+        Limit l = findLimit(unit);
+        if (l == null) {
+            return true;
+        }
+        return l.compliesWith(value);
+    }
+
+
+    
+    @Override
     public void initialize(final StandaloneCatalog catalog, final URI sourceURI) {
         catalogName = catalog.getCatalogName();
     }
diff --git a/catalog/src/test/java/com/ning/billing/catalog/TestLimits.java b/catalog/src/test/java/com/ning/billing/catalog/TestLimits.java
index 1e6f760..7761c3f 100644
--- a/catalog/src/test/java/com/ning/billing/catalog/TestLimits.java
+++ b/catalog/src/test/java/com/ning/billing/catalog/TestLimits.java
@@ -54,6 +54,38 @@ public class TestLimits extends CatalogTestSuiteNoDB {
         Assert.assertTrue(catalog.compliesWithLimits("pistol-monthly-evergreen", "misfires", 3));
         Assert.assertFalse(catalog.compliesWithLimits("pistol-monthly-evergreen", "misfires", 21));
         Assert.assertTrue(catalog.compliesWithLimits("pistol-monthly-evergreen", "misfires", -1));
-
+/*      <product name="Shotgun">
+            <category>BASE</category>
+            <limits>
+                <limit>
+                    <unit>shells</unit>
+                    <max>300</max>
+                </limit>
+            </limits>
+        </product>
+        <plan name="shotgun-annual">
+            <product>Shotgun</product>
+        ...
+            <finalPhase type="EVERGREEN">
+                <limits>
+                    <limit>
+                        <unit>shells</unit>
+                        <max>200</max>
+                    </limit>
+                </limits>
+            </finalPhase>
+        </plan>
+*/
+        Assert.assertTrue(catalog.compliesWithLimits("shotgun-monthly-evergreen", "shells", 100));
+        Assert.assertFalse(catalog.compliesWithLimits("shotgun-monthly-evergreen", "shells", 400));
+        Assert.assertTrue(catalog.compliesWithLimits("shotgun-monthly-evergreen", "shells", 250));
+     
+        Assert.assertTrue(catalog.compliesWithLimits("shotgun-annual-evergreen", "shells", 100));
+        Assert.assertFalse(catalog.compliesWithLimits("shotgun-annual-evergreen", "shells", 400));
+        Assert.assertFalse(catalog.compliesWithLimits("shotgun-annual-evergreen", "shells", 250));
+     
+        
+        
+        
     }
 }
diff --git a/catalog/src/test/resources/WeaponsHireSmall.xml b/catalog/src/test/resources/WeaponsHireSmall.xml
index 82df5f0..5b43feb 100644
--- a/catalog/src/test/resources/WeaponsHireSmall.xml
+++ b/catalog/src/test/resources/WeaponsHireSmall.xml
@@ -30,6 +30,7 @@
     <units>
         <unit name="targets"/>
         <unit name="misfires"/>
+        <unit name="shells"/>
     </units>
 
 	<products>
@@ -38,6 +39,12 @@
 		</product>
 		<product name="Shotgun">
 			<category>BASE</category>
+                <limits>
+                    <limit>
+                        <unit>shells</unit>
+                        <max>300</max>
+                    </limit>
+                </limits>
 		</product>
 		<product name="Laser-Scope">
 			<category>ADD_ON</category>
@@ -161,6 +168,12 @@
 					<price><currency>EUR</currency><value>1499.95</value></price>
 					<price><currency>GBP</currency><value>1699.95</value></price>
 				</recurringPrice>
+                <limits>
+                    <limit>
+                        <unit>shells</unit>
+                        <max>200</max>
+                    </limit>
+                </limits>
 			</finalPhase>
 		</plan>
 		<plan name="laser-scope-monthly">