killbill-memoizeit

Refactoring for test testCancelBPWithAOTheSameDay. Fix

8/22/2012 9:55:16 PM

Details

diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java
index 9bb0ed4..e5173be 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java
@@ -29,100 +29,60 @@ import org.testng.annotations.Test;
 
 import com.ning.billing.account.api.Account;
 import com.ning.billing.api.TestApiListener.NextEvent;
+import com.ning.billing.beatrix.util.InvoiceChecker.ExpectedItemCheck;
 import com.ning.billing.catalog.api.BillingPeriod;
 import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.catalog.api.PhaseType;
 import com.ning.billing.catalog.api.PlanPhaseSpecifier;
 import com.ning.billing.catalog.api.PriceListSet;
 import com.ning.billing.catalog.api.ProductCategory;
+import com.ning.billing.entitlement.api.user.Subscription;
 import com.ning.billing.entitlement.api.user.SubscriptionBundle;
 import com.ning.billing.entitlement.api.user.SubscriptionData;
 import com.ning.billing.invoice.api.Invoice;
-import com.ning.billing.invoice.api.InvoiceItem;
 import com.ning.billing.invoice.api.InvoiceItemType;
 
 import com.google.common.collect.ImmutableList;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 @Guice(modules = {BeatrixModule.class})
 public class TestIntegration extends TestIntegrationBase {
     @Test(groups = "slow")
     public void testCancelBPWithAOTheSameDay() throws Exception {
-        // We take april as it has 30 days (easier to play with BCD)
-        final LocalDate today = new LocalDate(2012, 4, 1);
-        final LocalDate trialEndDate = new LocalDate(2012, 5, 1);
+
         final Account account = createAccountWithPaymentMethod(getAccountData(1));
 
+        // We take april as it has 30 days (easier to play with BCD)
         // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
-        clock.setDeltaFromReality(today.toDateTimeAtCurrentTime().getMillis() - clock.getUTCNow().getMillis());
-        final SubscriptionBundle bundle = entitlementUserApi.createBundleForAccount(account.getId(), "whatever", context);
+        clock.setDay(new LocalDate(2012, 4, 1));
 
-        final String productName = "Shotgun";
-        final BillingPeriod term = BillingPeriod.MONTHLY;
-        final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
+        final SubscriptionBundle bundle = entitlementUserApi.createBundleForAccount(account.getId(), "whatever", context);
 
         //
         // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE NextEvent.INVOICE
         //
-        busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.INVOICE);
-        final PlanPhaseSpecifier bpPlanPhaseSpecifier = new PlanPhaseSpecifier(productName, ProductCategory.BASE, term, planSetName, null);
-        final SubscriptionData bpSubscription = subscriptionDataFromSubscription(entitlementUserApi.createSubscription(bundle.getId(),
-                                                                                                                       bpPlanPhaseSpecifier,
-                                                                                                                       null,
-                                                                                                                       context));
-        assertNotNull(bpSubscription);
-        assertTrue(busHandler.isCompleted(DELAY));
-        assertListenerStatus();
+        final Subscription bpSubscription = createSubscriptionAndCheckForCompletion(bundle.getId(), "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.INVOICE);
+        invoiceChecker.checkInvoice(account.getId(), 1, new ExpectedItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
 
         //
         // ADD ADD_ON ON THE SAME DAY
         //
-        busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.INVOICE, NextEvent.PAYMENT);
-        final PlanPhaseSpecifier addonPlanPhaseSpecifier = new PlanPhaseSpecifier("Telescopic-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
-        entitlementUserApi.createSubscription(bundle.getId(), addonPlanPhaseSpecifier, null, context);
-        assertTrue(busHandler.isCompleted(DELAY));
-        assertListenerStatus();
+        createSubscriptionAndCheckForCompletion(bundle.getId(), "Telescopic-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.PAYMENT);
+        invoiceChecker.checkInvoice(account.getId(), 2, new ExpectedItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("399.95")));
 
         //
         // CANCEL BP ON THE SAME DAY (we should have two cancellations, BP and AO)
-        //
-        busHandler.pushExpectedEvents(NextEvent.CANCEL, NextEvent.CANCEL, NextEvent.INVOICE);
-        bpSubscription.cancel(clock.getUTCNow(), false, context);
-
-        //Thread.sleep(10000000);
-
-        assertTrue(busHandler.isCompleted(DELAY));
-        assertListenerStatus();
-
-        final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId());
-        assertEquals(invoices.size(), 2);
-        // The first invoice is for the trial BP
-        assertEquals(invoices.get(0).getNumberOfItems(), 1);
-        assertEquals(invoices.get(0).getInvoiceItems().get(0).getStartDate().compareTo(today), 0);
-        // No end date for the trial item (fixed price of zero)
-        assertNull(invoices.get(0).getInvoiceItems().get(0).getEndDate());
-        // The second invoice should be adjusted for the AO (we paid for the full period)
-        assertEquals(invoices.get(1).getNumberOfItems(), 3);
-        for (final InvoiceItem item : invoices.get(1).getInvoiceItems()) {
-            if (InvoiceItemType.RECURRING.equals(item.getInvoiceItemType())) {
-                assertEquals(item.getStartDate().compareTo(today), 0);
-                assertEquals(item.getEndDate().compareTo(trialEndDate), 0);
-                assertEquals(item.getAmount().compareTo(new BigDecimal("399.9500")), 0);
-            } else if (InvoiceItemType.REPAIR_ADJ.equals(item.getInvoiceItemType())) {
-                assertEquals(item.getStartDate().compareTo(today), 0);
-                assertEquals(item.getEndDate().compareTo(trialEndDate), 0);
-                assertEquals(item.getAmount().compareTo(new BigDecimal("-399.9500")), 0);
-            } else {
-                assertEquals(item.getInvoiceItemType(), InvoiceItemType.CBA_ADJ);
-                assertEquals(item.getStartDate().compareTo(today), 0);
-                assertEquals(item.getEndDate().compareTo(today), 0);
-                assertEquals(item.getAmount().compareTo(new BigDecimal("399.9500")), 0);
-            }
-        }
+        // There is no invoice created as we only adjust the previous invoice.
+        //
+        cancelSubscriptionAndCheckForCompletion(bpSubscription, clock.getUTCNow(), NextEvent.CANCEL, NextEvent.CANCEL);
+        invoiceChecker.checkInvoice(account.getId(), 2,
+                new ExpectedItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("399.95")),
+                // The second invoice should be adjusted for the AO (we paid for the full period) and since we paid we should also see a CBA
+                new ExpectedItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-399.95")),
+                new ExpectedItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("399.95")));
     }
 
     @Test(groups = "slow")
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
index d33cfa9..25d2e14 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
@@ -225,9 +225,9 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
     }
 
     protected void verifyTestResult(final UUID accountId, final UUID subscriptionId,
-                                    final DateTime startDate, @Nullable final DateTime endDate,
-                                    final BigDecimal amount, final DateTime chargeThroughDate,
-                                    final int totalInvoiceItemCount) throws EntitlementUserApiException {
+            final DateTime startDate, @Nullable final DateTime endDate,
+            final BigDecimal amount, final DateTime chargeThroughDate,
+            final int totalInvoiceItemCount) throws EntitlementUserApiException {
         final SubscriptionData subscription = subscriptionDataFromSubscription(entitlementUserApi.getSubscriptionFromId(subscriptionId));
 
         final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(accountId);
@@ -299,17 +299,17 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
 
     protected AccountData getAccountData(final int billingDay) {
         return new MockAccountBuilder().name(UUID.randomUUID().toString().substring(1, 8))
-                                       .firstNameLength(6)
-                                       .email(UUID.randomUUID().toString().substring(1, 8))
-                                       .phone(UUID.randomUUID().toString().substring(1, 8))
-                                       .migrated(false)
-                                       .isNotifiedForInvoices(false)
-                                       .externalKey(UUID.randomUUID().toString().substring(1, 8))
-                                       .billingCycleDay(new MockBillCycleDay(billingDay))
-                                       .currency(Currency.USD)
-                                       .paymentMethodId(UUID.randomUUID())
-                                       .timeZone(DateTimeZone.UTC)
-                                       .build();
+        .firstNameLength(6)
+        .email(UUID.randomUUID().toString().substring(1, 8))
+        .phone(UUID.randomUUID().toString().substring(1, 8))
+        .migrated(false)
+        .isNotifiedForInvoices(false)
+        .externalKey(UUID.randomUUID().toString().substring(1, 8))
+        .billingCycleDay(new MockBillCycleDay(billingDay))
+        .currency(Currency.USD)
+        .paymentMethodId(UUID.randomUUID())
+        .timeZone(DateTimeZone.UTC)
+        .build();
     }
 
     protected void addMonthsAndCheckForCompletion(final int nbMonth, final NextEvent... events) {
@@ -347,18 +347,18 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
     }
 
     protected Subscription createSubscriptionAndCheckForCompletion(final UUID bundleId,
-                                                                   final String productName,
-                                                                   final ProductCategory productCategory,
-                                                                   final BillingPeriod billingPeriod,
-                                                                   final NextEvent... events) {
+            final String productName,
+            final ProductCategory productCategory,
+            final BillingPeriod billingPeriod,
+            final NextEvent... events) {
         return doCallAndCheckForCompletion(new Function<Void, Subscription>() {
             @Override
             public Subscription apply(@Nullable final Void dontcare) {
                 try {
                     final Subscription subscription = entitlementUserApi.createSubscription(bundleId,
-                                                                                            new PlanPhaseSpecifier(productName, productCategory, billingPeriod, PriceListSet.DEFAULT_PRICELIST_NAME, null),
-                                                                                            null,
-                                                                                            context);
+                            new PlanPhaseSpecifier(productName, productCategory, billingPeriod, PriceListSet.DEFAULT_PRICELIST_NAME, null),
+                            null,
+                            context);
                     assertNotNull(subscription);
                     return subscription;
                 } catch (EntitlementUserApiException e) {
@@ -370,9 +370,9 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
     }
 
     protected Subscription changeSubscriptionAndCheckForCompletion(final Subscription subscription,
-                                                                   final String productName,
-                                                                   final BillingPeriod billingPeriod,
-                                                                   final NextEvent... events) {
+            final String productName,
+            final BillingPeriod billingPeriod,
+            final NextEvent... events) {
         return doCallAndCheckForCompletion(new Function<Void, Subscription>() {
             @Override
             public Subscription apply(@Nullable final Void dontcare) {
@@ -387,6 +387,23 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
         }, events);
     }
 
+    protected Subscription cancelSubscriptionAndCheckForCompletion(final Subscription subscription,
+            final DateTime requestedDate,
+            final NextEvent... events) {
+        return doCallAndCheckForCompletion(new Function<Void, Subscription>() {
+            @Override
+            public Subscription apply(@Nullable final Void dontcare) {
+                try {
+                    subscription.cancel(requestedDate, true, context);
+                    return subscription;
+                } catch (EntitlementUserApiException e) {
+                    fail();
+                    return null;
+                }
+            }
+        }, events);
+    }
+
     private <T> T doCallAndCheckForCompletion(Function<Void, T> f, final NextEvent... events) {
         log.info("            ************    STARTING BUS HANDLER CHECK    ********************");