killbill-memoizeit

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 e5173be..9401b39 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
@@ -365,19 +365,12 @@ public class TestIntegration extends TestIntegrationBase {
         clock.setDeltaFromReality(initialCreationDate.getMillis() - clock.getUTCNow().getMillis());
         final SubscriptionBundle bundle = entitlementUserApi.createBundleForAccount(account.getId(), "whatever", context);
 
-        final String productName = "Shotgun";
-        final BillingPeriod term = BillingPeriod.MONTHLY;
-        final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
 
         //
         // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE NextEvent.INVOICE
         //
-        busHandler.pushExpectedEvent(NextEvent.CREATE);
-        busHandler.pushExpectedEvent(NextEvent.INVOICE);
-        SubscriptionData subscription = subscriptionDataFromSubscription(entitlementUserApi.createSubscription(bundle.getId(),
-                                                                                                               new PlanPhaseSpecifier(productName, ProductCategory.BASE, term, planSetName, null), null, context));
-        assertNotNull(subscription);
-        assertTrue(busHandler.isCompleted(DELAY));
+        SubscriptionData subscription = subscriptionDataFromSubscription(createSubscriptionAndCheckForCompletion(bundle.getId(), "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.INVOICE));
+        invoiceChecker.checkInvoice(account.getId(), 1, new ExpectedItemCheck(initialCreationDate.toLocalDate(), null, InvoiceItemType.FIXED, new BigDecimal("0")));
 
         //
         // VERIFY CTD HAS BEEN SET
@@ -391,15 +384,7 @@ public class TestIntegration extends TestIntegrationBase {
         //
         // CHANGE PLAN IMMEDIATELY AND EXPECT BOTH EVENTS: NextEvent.CHANGE NextEvent.INVOICE
         //
-        busHandler.pushExpectedEvent(NextEvent.CHANGE);
-        busHandler.pushExpectedEvent(NextEvent.INVOICE);
-
-        BillingPeriod newTerm = BillingPeriod.MONTHLY;
-        String newPlanSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
-        String newProductName = "Assault-Rifle";
-        subscription.changePlan(newProductName, newTerm, newPlanSetName, clock.getUTCNow(), context);
-
-        assertTrue(busHandler.isCompleted(DELAY));
+        subscription = subscriptionDataFromSubscription(changeSubscriptionAndCheckForCompletion(subscription, "Assault-Rifle", BillingPeriod.MONTHLY, NextEvent.CHANGE, NextEvent.INVOICE));
 
         //
         // VERIFY AGAIN CTD HAS BEEN SET
@@ -463,24 +448,13 @@ public class TestIntegration extends TestIntegrationBase {
         //
         // CHANGE PLAN EOT AND EXPECT NOTHING
         //
-        newTerm = BillingPeriod.MONTHLY;
-        newPlanSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
-        newProductName = "Pistol";
-        subscription = subscriptionDataFromSubscription(entitlementUserApi.getSubscriptionFromId(subscription.getId()));
-        subscription.changePlan(newProductName, newTerm, newPlanSetName, clock.getUTCNow(), context);
+        subscription = subscriptionDataFromSubscription(changeSubscriptionAndCheckForCompletion(subscription, "Pistol", BillingPeriod.MONTHLY));
 
         //
         // MOVE TIME AFTER CTD AND EXPECT BOTH EVENTS : NextEvent.CHANGE NextEvent.INVOICE
         //
-        busHandler.pushExpectedEvent(NextEvent.CHANGE);
-        busHandler.pushExpectedEvent(NextEvent.INVOICE);
-        busHandler.pushExpectedEvent(NextEvent.PAYMENT);
-        //clock.addDeltaFromReality(ctd.getMillis() - clock.getUTCNow().getMillis());
-        clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
+        addDaysAndCheckForCompletion(32, NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.PAYMENT);
 
-        //waitForDebug();
-
-        assertTrue(busHandler.isCompleted(DELAY));
         startDate = chargeThroughDate;
 
         // Resync latest with real CTD so test does not drift
@@ -516,22 +490,18 @@ public class TestIntegration extends TestIntegrationBase {
         //
         // FINALLY CANCEL SUBSCRIPTION EOT
         //
-        subscription = subscriptionDataFromSubscription(entitlementUserApi.getSubscriptionFromId(subscription.getId()));
-        subscription.cancel(clock.getUTCNow(), false, context);
+        subscription = subscriptionDataFromSubscription(cancelSubscriptionAndCheckForCompletion(subscription, clock.getUTCNow()));
+
 
         // MOVE AFTER CANCEL DATE AND EXPECT EVENT : NextEvent.CANCEL
-        busHandler.pushExpectedEvent(NextEvent.CANCEL);
         realChargeThroughDate = entitlementUserApi.getSubscriptionFromId(subscription.getId()).getChargedThroughDate();
         final Interval it = new Interval(clock.getUTCNow(), realChargeThroughDate.plusSeconds(5));
-        clock.addDeltaFromReality(it.toDurationMillis());
-        assertTrue(busHandler.isCompleted(DELAY));
+        addDaysAndCheckForCompletion((int) it.toDuration().getStandardDays(), NextEvent.CANCEL);
 
         //
         // CHECK AGAIN THERE IS NO MORE INVOICES GENERATED
         //
-        busHandler.reset();
-        clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS + 1000);
-        assertTrue(busHandler.isCompleted(DELAY));
+        addDaysAndCheckForCompletion(32);
 
         subscription = subscriptionDataFromSubscription(entitlementUserApi.getSubscriptionFromId(subscription.getId()));
         final DateTime lastCtd = subscription.getChargedThroughDate();
@@ -539,10 +509,6 @@ public class TestIntegration extends TestIntegrationBase {
         log.info("Checking CTD: " + lastCtd.toString() + "; clock is " + clock.getUTCNow().toString());
         assertTrue(lastCtd.isBefore(clock.getUTCNow()));
 
-        // The invoice system is still working to verify there is nothing to do
-        Thread.sleep(DELAY);
-
-        assertListenerStatus();
 
         log.info("TEST PASSED !");
     }
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 25d2e14..6837c43 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
@@ -377,8 +377,10 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
             @Override
             public Subscription apply(@Nullable final Void dontcare) {
                 try {
-                    subscription.changePlan(productName, billingPeriod, PriceListSet.DEFAULT_PRICELIST_NAME, clock.getUTCNow(), context);
-                    return subscription;
+                    // Need to fetch again to get latest CTD updated from the system
+                    final Subscription refreshedSubscription = entitlementUserApi.getSubscriptionFromId(subscription.getId());
+                    refreshedSubscription.changePlan(productName, billingPeriod, PriceListSet.DEFAULT_PRICELIST_NAME, clock.getUTCNow(), context);
+                    return refreshedSubscription;
                 } catch (EntitlementUserApiException e) {
                     fail();
                     return null;
@@ -394,8 +396,10 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
             @Override
             public Subscription apply(@Nullable final Void dontcare) {
                 try {
-                    subscription.cancel(requestedDate, true, context);
-                    return subscription;
+                    // Need to fetch again to get latest CTD updated from the system
+                    final Subscription refreshedSubscription = entitlementUserApi.getSubscriptionFromId(subscription.getId());
+                    refreshedSubscription.cancel(requestedDate, true, context);
+                    return refreshedSubscription;
                 } catch (EntitlementUserApiException e) {
                     fail();
                     return null;
diff --git a/invoice/src/main/java/com/ning/billing/invoice/InvoiceDispatcher.java b/invoice/src/main/java/com/ning/billing/invoice/InvoiceDispatcher.java
index 392e045..81636fe 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/InvoiceDispatcher.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/InvoiceDispatcher.java
@@ -200,7 +200,9 @@ public class InvoiceDispatcher {
                                                                                        invoice.getBalance(), invoice.getCurrency(),
                                                                                        context.getUserToken());
 
-                    postEvent(event, accountId);
+                    if (isRealInvoiceWithItems) {
+                        postEvent(event, accountId);
+                    }
                 }
             }
 
diff --git a/util/src/test/java/com/ning/billing/util/clock/ClockMock.java b/util/src/test/java/com/ning/billing/util/clock/ClockMock.java
index 39c8643..4552087 100644
--- a/util/src/test/java/com/ning/billing/util/clock/ClockMock.java
+++ b/util/src/test/java/com/ning/billing/util/clock/ClockMock.java
@@ -66,6 +66,10 @@ public class ClockMock implements Clock {
         logChange(prev);
     }
 
+    public synchronized void setDay(final LocalDate date) {
+        setTime(date.toDateTimeAtStartOfDay(DateTimeZone.UTC));
+    }
+
     public synchronized void addDays(final int days) {
         adjustTo(Days.days(days));
     }