killbill-memoizeit

invoice: See #321 Add unit test for IN_ARREAR recurring BillingMode Refactor

8/29/2015 1:49:50 PM

Changes

Details

diff --git a/invoice/src/main/java/org/killbill/billing/invoice/generator/BillingIntervalDetail.java b/invoice/src/main/java/org/killbill/billing/invoice/generator/BillingIntervalDetail.java
index 2f8edca..513cd87 100644
--- a/invoice/src/main/java/org/killbill/billing/invoice/generator/BillingIntervalDetail.java
+++ b/invoice/src/main/java/org/killbill/billing/invoice/generator/BillingIntervalDetail.java
@@ -120,9 +120,12 @@ public class BillingIntervalDetail {
         final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths();
         int numberOfPeriods = 0;
         LocalDate proposedDate = firstBillingCycleDate;
+        LocalDate nextProposedDate = proposedDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
 
-        while (proposedDate.isBefore(targetDate)) {
-            proposedDate = firstBillingCycleDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
+
+        while (!nextProposedDate.isAfter(targetDate)) {
+            proposedDate = nextProposedDate;
+            nextProposedDate = firstBillingCycleDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
             numberOfPeriods += 1;
         }
         proposedDate = alignProposedBillCycleDate(proposedDate, billingCycleDay);
diff --git a/invoice/src/test/java/org/killbill/billing/invoice/api/invoice/TestDefaultInvoicePaymentApi.java b/invoice/src/test/java/org/killbill/billing/invoice/api/invoice/TestDefaultInvoicePaymentApi.java
index 5f9477d..0af1018 100644
--- a/invoice/src/test/java/org/killbill/billing/invoice/api/invoice/TestDefaultInvoicePaymentApi.java
+++ b/invoice/src/test/java/org/killbill/billing/invoice/api/invoice/TestDefaultInvoicePaymentApi.java
@@ -33,8 +33,8 @@ import org.testng.annotations.Test;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
-import static org.killbill.billing.invoice.tests.InvoiceTestUtils.createAndPersistInvoice;
-import static org.killbill.billing.invoice.tests.InvoiceTestUtils.createAndPersistPayment;
+import static org.killbill.billing.invoice.proRations.InvoiceTestUtils.createAndPersistInvoice;
+import static org.killbill.billing.invoice.proRations.InvoiceTestUtils.createAndPersistPayment;
 
 public class TestDefaultInvoicePaymentApi extends InvoiceTestSuiteWithEmbeddedDB {
 
diff --git a/invoice/src/test/java/org/killbill/billing/invoice/model/TestInArrearBillingMode.java b/invoice/src/test/java/org/killbill/billing/invoice/model/TestInArrearBillingMode.java
new file mode 100644
index 0000000..aa4733e
--- /dev/null
+++ b/invoice/src/test/java/org/killbill/billing/invoice/model/TestInArrearBillingMode.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2014-2015 Groupon, Inc
+ * Copyright 2014-2015 The Billing Project, LLC
+ *
+ * The Billing Project licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.killbill.billing.invoice.model;
+
+import java.math.BigDecimal;
+import java.util.LinkedHashMap;
+import java.util.List;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.LocalDate;
+import org.killbill.billing.catalog.api.BillingMode;
+import org.killbill.billing.catalog.api.BillingPeriod;
+import org.killbill.billing.invoice.InvoiceTestSuiteNoDB;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestInArrearBillingMode extends InvoiceTestSuiteNoDB {
+
+    private static final DateTimeZone TIMEZONE = DateTimeZone.forID("Pacific/Pitcairn");
+    private static final BillingPeriod BILLING_PERIOD = BillingPeriod.MONTHLY;
+
+    @Test(groups = "fast")
+    public void testItemShouldNotStartInThePast() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = new LocalDate(2012, 7, 16);
+        final LocalDate targetDate = new LocalDate(2012, 7, 16);
+        final int billingCycleDayLocal = 15;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithNoEndDate() throws Exception {
+        final LocalDate startDate = new LocalDate(new DateTime("2012-07-17T02:25:33.000Z", DateTimeZone.UTC), TIMEZONE);
+        final LocalDate endDate = null;
+        final LocalDate targetDate = new LocalDate(2012, 7, 16);
+        final int billingCycleDayLocal = 15;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithBCDBeforeStartDay() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = new LocalDate(2012, 8, 16);
+        final LocalDate targetDate = new LocalDate(2012, 7, 16);
+        final int billingCycleDayLocal = 15;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+
+        final LocalDate targetDate2 = new LocalDate(2012, 8, 15);
+        expectedDates.put(new LocalDate(2012, 7, 16), new LocalDate(2012, 8, 15));
+        verifyInvoiceItems(startDate, endDate, targetDate2, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithBCDEqualsStartDay() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = new LocalDate(2012, 8, 16);
+        final LocalDate targetDate = new LocalDate(2012, 7, 16);
+        final int billingCycleDayLocal = 16;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+
+        expectedDates.put(new LocalDate(2012, 7, 16), new LocalDate(2012, 8, 16));
+        final LocalDate targetDate2 = new LocalDate(2012, 8, 16);
+        verifyInvoiceItems(startDate, endDate, targetDate2, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithBCDAfterStartDay() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = new LocalDate(2012, 8, 16);
+        final LocalDate targetDate = new LocalDate(2012, 7, 16);
+        final int billingCycleDayLocal = 17;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+
+        final LocalDate targetDate2 = new LocalDate(2012, 7, 17);
+        expectedDates.put(new LocalDate(2012, 7, 16), new LocalDate(2012, 7, 17));
+        verifyInvoiceItems(startDate, endDate, targetDate2, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithBCDBeforeStartDayWithTargetDateIn3Months() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = null;
+        final LocalDate targetDate = new LocalDate(2012, 10, 16);
+        final int billingCycleDayLocal = 15;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        expectedDates.put(new LocalDate(2012, 7, 16), new LocalDate(2012, 8, 15));
+        expectedDates.put(new LocalDate(2012, 8, 15), new LocalDate(2012, 9, 15));
+        expectedDates.put(new LocalDate(2012, 9, 15), new LocalDate(2012, 10, 15));
+
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithBCDEqualsStartDayWithTargetDateIn3Months() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = null;
+        final LocalDate targetDate = new LocalDate(2012, 10, 16);
+        final int billingCycleDayLocal = 16;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        expectedDates.put(new LocalDate(2012, 7, 16), new LocalDate(2012, 8, 16));
+        expectedDates.put(new LocalDate(2012, 8, 16), new LocalDate(2012, 9, 16));
+        expectedDates.put(new LocalDate(2012, 9, 16), new LocalDate(2012, 10, 16));
+
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    @Test(groups = "fast")
+    public void testCalculateSimpleInvoiceItemWithBCDAfterStartDayWithTargetDateIn3Months() throws Exception {
+        final LocalDate startDate = new LocalDate(2012, 7, 16);
+        final LocalDate endDate = null;
+        final LocalDate targetDate = new LocalDate(2012, 10, 16);
+        final int billingCycleDayLocal = 17;
+
+        final LinkedHashMap<LocalDate, LocalDate> expectedDates = new LinkedHashMap<LocalDate, LocalDate>();
+        expectedDates.put(new LocalDate(2012, 7, 16), new LocalDate(2012, 7, 17));
+        expectedDates.put(new LocalDate(2012, 7, 17), new LocalDate(2012, 8, 17));
+        expectedDates.put(new LocalDate(2012, 8, 17), new LocalDate(2012, 9, 17));
+
+        verifyInvoiceItems(startDate, endDate, targetDate, TIMEZONE, billingCycleDayLocal, BILLING_PERIOD, expectedDates);
+    }
+
+    private void verifyInvoiceItems(final LocalDate startDate, final LocalDate endDate, final LocalDate targetDate,
+                                    final DateTimeZone dateTimeZone, final int billingCycleDayLocal, final BillingPeriod billingPeriod,
+                                    final LinkedHashMap<LocalDate, LocalDate> expectedDates) throws InvalidDateSequenceException {
+        final DefaultBillingModeGenerator billingMode = new DefaultBillingModeGenerator();
+
+        final List<RecurringInvoiceItemData> invoiceItems = billingMode.generateInvoiceItemData(startDate, endDate, targetDate, billingCycleDayLocal, billingPeriod, BillingMode.IN_ARREAR);
+
+        int i = 0;
+        for (final LocalDate periodStartDate : expectedDates.keySet()) {
+            Assert.assertEquals(invoiceItems.get(i).getStartDate(), periodStartDate);
+            Assert.assertEquals(invoiceItems.get(i).getEndDate(), expectedDates.get(periodStartDate));
+            Assert.assertTrue(invoiceItems.get(0).getNumberOfCycles().compareTo(BigDecimal.ONE) <= 0);
+            i++;
+        }
+        Assert.assertEquals(invoiceItems.size(), i);
+    }
+}