killbill-aplcache

beatrix: fix TestOverdueIntegration The test now passes.

8/27/2012 7:21:18 PM

Details

diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/overdue/TestOverdueIntegration.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/overdue/TestOverdueIntegration.java
index 276ae61..9109bbd 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/overdue/TestOverdueIntegration.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/overdue/TestOverdueIntegration.java
@@ -27,7 +27,6 @@ import java.util.concurrent.Callable;
 import org.joda.time.DateTime;
 import org.joda.time.LocalDate;
 import org.testng.Assert;
-import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Guice;
 import org.testng.annotations.Test;
@@ -62,6 +61,7 @@ import com.google.inject.name.Named;
 
 import static com.jayway.awaitility.Awaitility.await;
 import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
@@ -183,11 +183,6 @@ public class TestOverdueIntegration extends TestIntegrationBase {
         // configure basic OD state rules for 2 states OD1 1-2month, OD2 2-3 month
     }
 
-    @AfterMethod
-    public void cleanup() {
-        // Clear databases
-    }
-
     // We set the the property killbill.payment.retry.days=8,8,8,8,8,8,8,8 so that Payment retry logic does not end with an ABORTED state
     // preventing final instant payment to succeed.
     @Test(groups = "slow")
@@ -256,14 +251,27 @@ public class TestOverdueIntegration extends TestIntegrationBase {
                 createPaymentAndCheckForCompletion(account, invoice, NextEvent.PAYMENT);
             }
         }
-        // STEPH COMMENTED OUTE UNTIL WE SORT THAT OUT
-/*
+
         checkODState(BlockingApi.CLEAR_STATE_NAME);
         checkChangePlanWithOverdueState(baseSubscription, false);
 
-        invoiceChecker.checkInvoice(account.getId(), 4, new ExpectedItemCheck(new LocalDate(2012, 7, 25), new LocalDate(2012, 7, 31), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
+        invoiceChecker.checkRepairedInvoice(account.getId(), 3,
+                                            new ExpectedItemCheck(new LocalDate(2012, 6, 30), new LocalDate(2012, 7, 31), InvoiceItemType.RECURRING, new BigDecimal("249.95")),
+                                            // We paid up to 07-31, hence the adjustment
+                                            new ExpectedItemCheck(new LocalDate(2012, 6, 30), new LocalDate(2012, 7, 31), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-249.95")),
+                                            new ExpectedItemCheck(new LocalDate(2012, 7, 25), new LocalDate(2012, 7, 25), InvoiceItemType.CBA_ADJ, new BigDecimal("249.95")));
+        invoiceChecker.checkInvoice(account.getId(), 4,
+                                    // Note the end date here is not 07-25, but 07-15. The overdue configuration disabled invoicing between 07-15 and 07-25 (e.g. the bundle
+                                    // was inaccessible, hence we didn't want to charge the customer for that period, even though the account was overdue).
+                                    new ExpectedItemCheck(new LocalDate(2012, 6, 30), new LocalDate(2012, 7, 15), InvoiceItemType.RECURRING, new BigDecimal("124.98")),
+                                    // Item for the upgraded recurring plan
+                                    new ExpectedItemCheck(new LocalDate(2012, 7, 25), new LocalDate(2012, 7, 31), InvoiceItemType.RECURRING, new BigDecimal("116.09")),
+                                    // Credits consumed
+                                    new ExpectedItemCheck(new LocalDate(2012, 7, 25), new LocalDate(2012, 7, 25), InvoiceItemType.CBA_ADJ, new BigDecimal("-241.07")));
         invoiceChecker.checkChargedThroughDate(baseSubscription.getId(), new LocalDate(2012, 7, 31));
-*/
+
+        // Verify the account balance: 249.95 - 124.98 - 116.09
+        assertEquals(invoiceUserApi.getAccountBalance(account.getId()).compareTo(new BigDecimal("-8.88")), 0);
     }
 
     private void checkChangePlanWithOverdueState(final Subscription subscription, final boolean shouldFail) {
@@ -274,8 +282,8 @@ public class TestOverdueIntegration extends TestIntegrationBase {
                 assertTrue(e.getCause() instanceof BlockingApiException);
             }
         } else {
-            // Upgrade
-            changeSubscriptionAndCheckForCompletion(subscription, "Assault-Rifle", BillingPeriod.MONTHLY, NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.PAYMENT);
+            // Upgrade - we don't expect a payment here due to the scenario (the account will have some CBA)
+            changeSubscriptionAndCheckForCompletion(subscription, "Assault-Rifle", BillingPeriod.MONTHLY, NextEvent.CHANGE, NextEvent.INVOICE);
         }
     }
 
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/util/InvoiceChecker.java b/beatrix/src/test/java/com/ning/billing/beatrix/util/InvoiceChecker.java
index ff86dce..1cc24d8 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/util/InvoiceChecker.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/util/InvoiceChecker.java
@@ -16,11 +16,6 @@
 
 package com.ning.billing.beatrix.util;
 
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertNull;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
-
 import java.math.BigDecimal;
 import java.util.List;
 import java.util.UUID;
@@ -44,6 +39,11 @@ import com.ning.billing.invoice.api.InvoiceUserApi;
 import com.google.common.collect.ImmutableList;
 import com.google.inject.Inject;
 
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
 public class InvoiceChecker {
 
     private static final Logger log = LoggerFactory.getLogger(InvoiceChecker.class);
@@ -68,6 +68,17 @@ public class InvoiceChecker {
         checkInvoice(invoice.getId(), expected);
     }
 
+    public void checkRepairedInvoice(final UUID accountId, final int invoiceNb, final ExpectedItemCheck... expected) {
+        checkRepairedInvoice(accountId, invoiceNb, ImmutableList.<ExpectedItemCheck>copyOf(expected));
+    }
+
+    public void checkRepairedInvoice(final UUID accountId, final int invoiceNb, final List<ExpectedItemCheck> expected) {
+        final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(accountId);
+        Assert.assertTrue(invoices.size() > invoiceNb);
+        final Invoice invoice = invoices.get(invoiceNb - 1);
+        checkInvoice(invoice.getId(), expected);
+    }
+
     public void checkInvoice(final UUID invoiceId, final List<ExpectedItemCheck> expected) {
         final Invoice invoice = invoiceUserApi.getInvoice(invoiceId);
         Assert.assertNotNull(invoice);
@@ -102,7 +113,6 @@ public class InvoiceChecker {
         }
     }
 
-
     public void checkNullChargedThroughDate(final UUID subscriptionId) {
         checkChargedThroughDate(subscriptionId, null);
     }
@@ -115,7 +125,7 @@ public class InvoiceChecker {
             } else {
                 final DateTime expectedCTD = expectedLocalCTD.toDateTime(new LocalTime(subscription.getStartDate().getMillis()), DateTimeZone.UTC);
                 final String msg = String.format("Checking CTD for subscription %s : expectedLocalCTD = %s => expectedCTD = %s, got %s",
-                        subscriptionId, expectedLocalCTD, expectedCTD, subscription.getChargedThroughDate());
+                                                 subscriptionId, expectedLocalCTD, expectedCTD, subscription.getChargedThroughDate());
                 log.info(msg);
                 assertNotNull(subscription.getChargedThroughDate());
                 assertTrue(subscription.getChargedThroughDate().compareTo(expectedCTD) == 0, msg);