killbill-memoizeit

Return first catalog version (if any) for effectiveDate in

6/23/2017 11:18:40 PM

Details

diff --git a/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java b/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java
index e52d6a8..dd599bd 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java
@@ -122,6 +122,13 @@ public class VersionedCatalog extends ValidatingConfig<VersionedCatalog> impleme
                 return i;
             }
         }
+        // If the only version we have are after the input date, we return the first version
+        // This is not strictly correct from an api point of view, but there is no real good use case
+        // where the system would ask for the catalog for a date prior any catalog was uploaded and
+        // yet time manipulation could end of inn that state -- see https://github.com/killbill/killbill/issues/760
+        if (versions.size() > 0) {
+            return 0;
+        }
         throw new CatalogApiException(ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE, date.toString());
     }
 
diff --git a/catalog/src/test/java/org/killbill/billing/catalog/TestVersionedCatalog.java b/catalog/src/test/java/org/killbill/billing/catalog/TestVersionedCatalog.java
index c4bd0b9..3dcb60f 100644
--- a/catalog/src/test/java/org/killbill/billing/catalog/TestVersionedCatalog.java
+++ b/catalog/src/test/java/org/killbill/billing/catalog/TestVersionedCatalog.java
@@ -18,24 +18,16 @@
 
 package org.killbill.billing.catalog;
 
-import java.io.IOException;
 import java.math.BigDecimal;
-import java.net.URISyntaxException;
-
-import javax.xml.bind.JAXBException;
-import javax.xml.transform.TransformerException;
 
 import org.joda.time.DateTime;
 import org.killbill.billing.ErrorCode;
 import org.killbill.billing.catalog.api.CatalogApiException;
 import org.killbill.billing.catalog.api.Currency;
-import org.killbill.billing.catalog.api.InvalidConfigException;
 import org.killbill.billing.catalog.api.Plan;
-import org.killbill.billing.platform.api.KillbillService.ServiceException;
 import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
-import org.xml.sax.SAXException;
 
 public class TestVersionedCatalog extends CatalogTestSuiteNoDB {
 
@@ -88,13 +80,35 @@ public class TestVersionedCatalog extends CatalogTestSuiteNoDB {
     }
 
     @Test(groups = "fast")
-    public void testErrorOnDateTooEarly() {
+    public void testErrorOnDateTooEarly() throws CatalogApiException {
         final DateTime dt0 = new DateTime("1977-01-01T00:00:00+00:00");
+
+        // We find it although the date provided is too early because we default to first catalog version
+        vc.findPlan("shotgun-monthly", dt0);
+
         try {
-            vc.findPlan("foo", dt0);
+            // We **don't find it** because date is too early and not part of first catalog version
+            vc.findPlan("shotgun-quarterly", dt0);
             Assert.fail("Date is too early an exception should have been thrown");
         } catch (CatalogApiException e) {
-            Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE.getCode());
+            Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PLAN.getCode());
         }
     }
+
+
+    @Test(groups = "fast")
+    public void testWithDeletedPlan() throws CatalogApiException {
+
+        // We find it because this is version 2 whose effectiveDate is "2011-02-02T00:00:00+00:00"
+        vc.findPlan("shotgun-quarterly", new DateTime("2011-02-02T00:01:00+00:00"));
+
+        try {
+            // We **don't find it** because date provided matches version 3 where plan was removed
+            vc.findPlan("shotgun-quarterly", new DateTime("2011-03-03T00:01:00+00:00"));
+            Assert.fail("Plan has been removed");
+        } catch (CatalogApiException e) {
+            Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PLAN.getCode());
+        }
+
+    }
 }
diff --git a/catalog/src/test/resources/versionedCatalog/WeaponsHireSmall-2.xml b/catalog/src/test/resources/versionedCatalog/WeaponsHireSmall-2.xml
index df1c09d..3fee3ea 100644
--- a/catalog/src/test/resources/versionedCatalog/WeaponsHireSmall-2.xml
+++ b/catalog/src/test/resources/versionedCatalog/WeaponsHireSmall-2.xml
@@ -183,6 +183,44 @@
                 </recurring>
             </finalPhase>
         </plan>
+        <plan name="shotgun-quarterly">
+            <product>Shotgun</product>
+            <initialPhases>
+                <phase type="TRIAL">
+                    <duration>
+                        <unit>DAYS</unit>
+                        <number>30</number>
+                    </duration>
+                    <fixed>
+                        <fixedPrice> <!-- empty price implies $0 -->
+                        </fixedPrice>
+                    </fixed>
+                </phase>
+            </initialPhases>
+            <finalPhase type="EVERGREEN">
+                <duration>
+                    <unit>UNLIMITED</unit>
+                    <number>-1</number>
+                </duration>
+                <recurring>
+                    <billingPeriod>QUARTERLY</billingPeriod>
+                    <recurringPrice>
+                        <price>
+                            <currency>USD</currency>
+                            <value>249.95</value>
+                        </price>
+                        <price>
+                            <currency>EUR</currency>
+                            <value>149.95</value>
+                        </price>
+                        <price>
+                            <currency>GBP</currency>
+                            <value>169.95</value>
+                        </price>
+                    </recurringPrice>
+                </recurring>
+            </finalPhase>
+        </plan>
         <plan name="shotgun-annual">
             <product>Shotgun</product>
             <initialPhases>
@@ -285,6 +323,7 @@
             <plans>
                 <plan>pistol-monthly</plan>
                 <plan>shotgun-monthly</plan>
+                <plan>shotgun-quarterly</plan>
                 <plan>shotgun-annual</plan>
                 <plan>laser-scope-monthly</plan>
                 <plan>extra-ammo-monthly</plan>