killbill-aplcache

junction: Modify BillingEvent api Remove unused BillingPeriod,

9/2/2015 6:43:14 PM

Details

diff --git a/api/src/main/java/org/killbill/billing/junction/BillingEvent.java b/api/src/main/java/org/killbill/billing/junction/BillingEvent.java
index cfc0ea4..02c15d0 100644
--- a/api/src/main/java/org/killbill/billing/junction/BillingEvent.java
+++ b/api/src/main/java/org/killbill/billing/junction/BillingEvent.java
@@ -37,83 +37,78 @@ public interface BillingEvent extends Comparable<BillingEvent> {
     /**
      * @return the account that this billing event is associated with
      */
-    public Account getAccount();
+    Account getAccount();
 
     /**
      * @return the billCycleDay in the account timezone as seen for that subscription at that time
      *         <p/>
      *         Note: The billCycleDay may come from the Account, or the bundle or the subscription itself
      */
-    public int getBillCycleDayLocal();
+    int getBillCycleDayLocal();
 
     /**
      * @return the subscription
      */
-    public SubscriptionBase getSubscription();
+    SubscriptionBase getSubscription();
 
     /**
      * @return the date for when that event became effective
      */
-    public DateTime getEffectiveDate();
+    DateTime getEffectiveDate();
 
     /**
      * @return the plan phase
      */
-    public PlanPhase getPlanPhase();
+    PlanPhase getPlanPhase();
 
     /**
      * @return the plan
      */
-    public Plan getPlan();
+    Plan getPlan();
 
     /**
      * @return the billing period for the active phase
      */
-    public BillingPeriod getBillingPeriod();
-
-    /**
-     * @return the billing mode for the current event
-     */
-    public BillingMode getBillingMode();
+    BillingPeriod getBillingPeriod();
 
     /**
      * @return the description of the billing event
      */
-    public String getDescription();
+    String getDescription();
 
     /**
      * @return the fixed price for the phase
      */
-    public BigDecimal getFixedPrice();
+    BigDecimal getFixedPrice();
 
     /**
      * @return the recurring price for the phase
      */
-    public BigDecimal getRecurringPrice();
+    BigDecimal getRecurringPrice();
 
     /**
      * @return the currency for the account being invoiced
      */
-    public Currency getCurrency();
+    Currency getCurrency();
 
     /**
      * @return the transition type of the underlying subscription event that triggered this
      */
-    public SubscriptionBaseTransitionType getTransitionType();
+    SubscriptionBaseTransitionType getTransitionType();
 
     /**
      * @return a unique long indicating the ordering on which events got inserted on disk-- used for sorting only
      */
-    public Long getTotalOrdering();
+    Long getTotalOrdering();
 
     /**
      * @return the TimeZone of the account
      */
-    public DateTimeZone getTimeZone();
+    DateTimeZone getTimeZone();
 
     /**
      *
      * @return the list of {@code Usage} section
      */
-    public List<Usage> getUsages();
+    List<Usage> getUsages();
 }
diff --git a/invoice/src/main/java/org/killbill/billing/invoice/generator/DefaultInvoiceGenerator.java b/invoice/src/main/java/org/killbill/billing/invoice/generator/DefaultInvoiceGenerator.java
index 3d8fecc..b823655 100644
--- a/invoice/src/main/java/org/killbill/billing/invoice/generator/DefaultInvoiceGenerator.java
+++ b/invoice/src/main/java/org/killbill/billing/invoice/generator/DefaultInvoiceGenerator.java
@@ -372,7 +372,7 @@ public class DefaultInvoiceGenerator implements InvoiceGenerator {
                         items.add(recurringItem);
                     }
                 }
-                updatePerSubscriptionNextNotificationDate(thisEvent.getSubscription().getId(), itemDataWithNextBillingCycleDate.getNextBillingCycleDate(), items, thisEvent.getBillingMode(), perSubscriptionFutureNotificationDate);
+                updatePerSubscriptionNextNotificationDate(thisEvent.getSubscription().getId(), itemDataWithNextBillingCycleDate.getNextBillingCycleDate(), items, billingMode, perSubscriptionFutureNotificationDate);
 
             }
         }
diff --git a/invoice/src/test/java/org/killbill/billing/invoice/TestInvoiceHelper.java b/invoice/src/test/java/org/killbill/billing/invoice/TestInvoiceHelper.java
index 9d8dc34..18d1ec7 100644
--- a/invoice/src/test/java/org/killbill/billing/invoice/TestInvoiceHelper.java
+++ b/invoice/src/test/java/org/killbill/billing/invoice/TestInvoiceHelper.java
@@ -367,11 +367,6 @@ public class TestInvoiceHelper {
             }
 
             @Override
-            public BillingMode getBillingMode() {
-                return billingMode;
-            }
-
-            @Override
             public String getDescription() {
                 return description;
             }
diff --git a/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BlockingCalculator.java b/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BlockingCalculator.java
index 270e706..a55956e 100644
--- a/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BlockingCalculator.java
+++ b/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/BlockingCalculator.java
@@ -32,7 +32,6 @@ import org.joda.time.DateTimeZone;
 
 import org.killbill.billing.account.api.Account;
 import org.killbill.billing.callcontext.InternalTenantContext;
-import org.killbill.billing.catalog.api.BillingMode;
 import org.killbill.billing.catalog.api.BillingPeriod;
 import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.catalog.api.Plan;
@@ -204,14 +203,13 @@ public class BlockingCalculator {
 
         final Currency currency = previousEvent.getCurrency();
         final String description = "";
-        final BillingMode billingMode = previousEvent.getBillingMode();
         final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.START_BILLING_DISABLED;
         final Long totalOrdering = globaltotalOrder.getAndIncrement();
         final DateTimeZone tz = previousEvent.getTimeZone();
 
-        return new DefaultBillingEvent(account, subscription, effectiveDate, plan, planPhase,
+        return new DefaultBillingEvent(account, subscription, effectiveDate, true, plan, planPhase,
                                        fixedPrice, recurringPrice, currency,
-                                       billingPeriod, billCycleDay, billingMode,
+                                       billingPeriod, billCycleDay,
                                        description, totalOrdering, type, tz);
     }
 
@@ -227,15 +225,14 @@ public class BlockingCalculator {
         final BigDecimal recurringPrice = previousEvent.getRecurringPrice();
         final Currency currency = previousEvent.getCurrency();
         final String description = "";
-        final BillingMode billingMode  = previousEvent.getBillingMode();
         final BillingPeriod billingPeriod = previousEvent.getBillingPeriod();
         final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.END_BILLING_DISABLED;
         final Long totalOrdering = globaltotalOrder.getAndIncrement();
         final DateTimeZone tz = previousEvent.getTimeZone();
 
-        return new DefaultBillingEvent(account, subscription, effectiveDate, plan, planPhase,
+        return new DefaultBillingEvent(account, subscription, effectiveDate, true, plan, planPhase,
                                        fixedPrice, recurringPrice, currency,
-                                       billingPeriod, billCycleDay, billingMode,
+                                       billingPeriod, billCycleDay,
                                        description, totalOrdering, type, tz);
     }
 
diff --git a/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/DefaultBillingEvent.java b/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/DefaultBillingEvent.java
index 54cffc5..1a7f98d 100644
--- a/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/DefaultBillingEvent.java
+++ b/junction/src/main/java/org/killbill/billing/junction/plumbing/billing/DefaultBillingEvent.java
@@ -17,7 +17,6 @@
 package org.killbill.billing.junction.plumbing.billing;
 
 import java.math.BigDecimal;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -25,9 +24,7 @@ import javax.annotation.Nullable;
 
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
-
 import org.killbill.billing.account.api.Account;
-import org.killbill.billing.catalog.api.BillingMode;
 import org.killbill.billing.catalog.api.BillingPeriod;
 import org.killbill.billing.catalog.api.Catalog;
 import org.killbill.billing.catalog.api.CatalogApiException;
@@ -35,14 +32,16 @@ import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.catalog.api.Plan;
 import org.killbill.billing.catalog.api.PlanPhase;
 import org.killbill.billing.catalog.api.Usage;
-import org.killbill.billing.subscription.api.SubscriptionBaseTransitionType;
-import org.killbill.billing.subscription.api.SubscriptionBase;
 import org.killbill.billing.events.EffectiveSubscriptionInternalEvent;
 import org.killbill.billing.junction.BillingEvent;
+import org.killbill.billing.subscription.api.SubscriptionBase;
+import org.killbill.billing.subscription.api.SubscriptionBaseTransitionType;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
 public class DefaultBillingEvent implements BillingEvent {
+
     private final Account account;
     private final int billCycleDayLocal;
     private final SubscriptionBase subscription;
@@ -53,7 +52,6 @@ public class DefaultBillingEvent implements BillingEvent {
     private final BigDecimal recurringPrice;
     private final Currency currency;
     private final String description;
-    private final BillingMode billingMode;
     private final BillingPeriod billingPeriod;
     private final SubscriptionBaseTransitionType type;
     private final Long totalOrdering;
@@ -63,16 +61,16 @@ public class DefaultBillingEvent implements BillingEvent {
 
     public DefaultBillingEvent(final Account account, final EffectiveSubscriptionInternalEvent transition, final SubscriptionBase subscription, final int billCycleDayLocal, final Currency currency, final Catalog catalog) throws CatalogApiException {
 
+        final boolean isActive = transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL;
+
         this.account = account;
         this.billCycleDayLocal = billCycleDayLocal;
         this.subscription = subscription;
         this.effectiveDate = transition.getEffectiveTransitionTime();
-        final String planPhaseName = (transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL) ?
-                transition.getNextPhase() : transition.getPreviousPhase();
+        final String planPhaseName = isActive ? transition.getNextPhase() : transition.getPreviousPhase();
         this.planPhase = (planPhaseName != null) ? catalog.findPhase(planPhaseName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null;
 
-        final String planName = (transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL) ?
-                transition.getNextPlan() : transition.getPreviousPlan();
+        final String planName = isActive ? transition.getNextPlan() : transition.getPreviousPlan();
         this.plan = (planName != null) ? catalog.findPlan(planName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null;
 
         final String nextPhaseName = transition.getNextPhase();
@@ -81,24 +79,22 @@ public class DefaultBillingEvent implements BillingEvent {
         final String prevPhaseName = transition.getPreviousPhase();
         final PlanPhase prevPhase = (prevPhaseName != null) ? catalog.findPhase(prevPhaseName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null;
 
-
         this.fixedPrice = getFixedPrice(nextPhase, currency);
         this.recurringPrice = getRecurringPrice(nextPhase, currency);
 
         this.currency = currency;
         this.description = transition.getTransitionType().toString();
-        this.billingMode = BillingMode.IN_ADVANCE;
-        this.billingPeriod = getRecurringBillingPeriod((transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL) ? nextPhase : prevPhase);
+        this.billingPeriod = getRecurringBillingPeriod(isActive ? nextPhase : prevPhase);
         this.type = transition.getTransitionType();
         this.totalOrdering = transition.getTotalOrdering();
         this.timeZone = account.getTimeZone();
-        this.usages = initializeUsage();
+        this.usages = initializeUsage(isActive);
     }
 
-
-    public DefaultBillingEvent(final Account account, final SubscriptionBase subscription, final DateTime effectiveDate, final Plan plan, final PlanPhase planPhase,
+    public DefaultBillingEvent(final Account account, final SubscriptionBase subscription, final DateTime effectiveDate, final boolean isActive,
+                               final Plan plan, final PlanPhase planPhase,
                                final BigDecimal fixedPrice, final BigDecimal recurringPrice, final Currency currency,
-                               final BillingPeriod billingPeriod, final int billCycleDayLocal, final BillingMode billingMode,
+                               final BillingPeriod billingPeriod, final int billCycleDayLocal,
                                final String description, final long totalOrdering, final SubscriptionBaseTransitionType type, final DateTimeZone timeZone) {
         this.account = account;
         this.subscription = subscription;
@@ -110,12 +106,11 @@ public class DefaultBillingEvent implements BillingEvent {
         this.currency = currency;
         this.billingPeriod = billingPeriod;
         this.billCycleDayLocal = billCycleDayLocal;
-        this.billingMode = billingMode;
         this.description = description;
         this.type = type;
         this.totalOrdering = totalOrdering;
         this.timeZone = timeZone;
-        this.usages = initializeUsage();
+        this.usages = initializeUsage(isActive);
 
     }
 
@@ -201,11 +196,6 @@ public class DefaultBillingEvent implements BillingEvent {
     }
 
     @Override
-    public BillingMode getBillingMode() {
-        return billingMode;
-    }
-
-    @Override
     public String getDescription() {
         return description;
     }
@@ -245,7 +235,6 @@ public class DefaultBillingEvent implements BillingEvent {
         return usages;
     }
 
-
     @Override
     public String toString() {
         // Note: we don't use all fields here, as the output would be overwhelming
@@ -279,9 +268,6 @@ public class DefaultBillingEvent implements BillingEvent {
         if (account != null ? !account.equals(that.account) : that.account != null) {
             return false;
         }
-        if (billingMode != that.billingMode) {
-            return false;
-        }
         if (billingPeriod != that.billingPeriod) {
             return false;
         }
@@ -334,7 +320,6 @@ public class DefaultBillingEvent implements BillingEvent {
         result = 31 * result + (recurringPrice != null ? recurringPrice.hashCode() : 0);
         result = 31 * result + (currency != null ? currency.hashCode() : 0);
         result = 31 * result + (description != null ? description.hashCode() : 0);
-        result = 31 * result + (billingMode != null ? billingMode.hashCode() : 0);
         result = 31 * result + (billingPeriod != null ? billingPeriod.hashCode() : 0);
         result = 31 * result + (type != null ? type.hashCode() : 0);
         result = 31 * result + (totalOrdering != null ? totalOrdering.hashCode() : 0);
@@ -342,7 +327,6 @@ public class DefaultBillingEvent implements BillingEvent {
         return result;
     }
 
-
     private BigDecimal getFixedPrice(@Nullable final PlanPhase nextPhase, final Currency currency) throws CatalogApiException {
         return (nextPhase != null && nextPhase.getFixed() != null && nextPhase.getFixed().getPrice() != null) ? nextPhase.getFixed().getPrice().getPrice(currency) : null;
     }
@@ -358,8 +342,11 @@ public class DefaultBillingEvent implements BillingEvent {
         return nextPhase.getRecurring() != null ? nextPhase.getRecurring().getBillingPeriod() : BillingPeriod.NO_BILLING_PERIOD;
     }
 
-    private List<Usage> initializeUsage() {
-        List<Usage> result = Collections.<Usage>emptyList();
+    private List<Usage> initializeUsage(final boolean isActive) {
+        List<Usage> result = ImmutableList.<Usage>of();
+        if (!isActive) {
+            return result;
+        }
         if (planPhase != null) {
             result = Lists.newArrayList();
             for (Usage usage : planPhase.getUsages()) {
diff --git a/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBillingApi.java b/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBillingApi.java
index 4acd3e2..34ab02f 100644
--- a/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBillingApi.java
+++ b/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBillingApi.java
@@ -255,7 +255,6 @@ public class TestBillingApi extends JunctionTestSuiteNoDB {
         if (!SubscriptionBaseTransitionType.START_BILLING_DISABLED.equals(event.getTransitionType())) {
             Assert.assertEquals(nextPhase.getRecurring().getBillingPeriod(), event.getBillingPeriod());
         }
-        Assert.assertEquals(BillingMode.IN_ADVANCE, event.getBillingMode());
         Assert.assertEquals(desc, event.getTransitionType().toString());
     }
 
diff --git a/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBlockingCalculator.java b/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBlockingCalculator.java
index 867ba4f..cf02a82 100644
--- a/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBlockingCalculator.java
+++ b/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestBlockingCalculator.java
@@ -532,14 +532,13 @@ public class TestBlockingCalculator extends JunctionTestSuiteNoDB {
         final BigDecimal recurringPrice = BigDecimal.TEN;
         final Currency currency = Currency.USD;
         final String description = "";
-        final BillingMode billingModeType = BillingMode.IN_ADVANCE;
         final BillingPeriod billingPeriod = BillingPeriod.MONTHLY;
         final Long totalOrdering = 0L;
         final DateTimeZone tz = DateTimeZone.UTC;
 
-        return new DefaultBillingEvent(account, subscription, effectiveDate, plan, planPhase,
+        return new DefaultBillingEvent(account, subscription, effectiveDate, true, plan, planPhase,
                                        fixedPrice, recurringPrice, currency,
-                                       billingPeriod, billCycleDay, billingModeType,
+                                       billingPeriod, billCycleDay,
                                        description, totalOrdering, type, tz);
     }
 
@@ -578,7 +577,6 @@ public class TestBlockingCalculator extends JunctionTestSuiteNoDB {
         assertNull(result.getRecurringPrice());
         assertEquals(result.getCurrency(), event.getCurrency());
         assertEquals(result.getDescription(), "");
-        assertEquals(result.getBillingMode(), event.getBillingMode());
         assertEquals(result.getBillingPeriod(), BillingPeriod.NO_BILLING_PERIOD);
         assertEquals(result.getTransitionType(), SubscriptionBaseTransitionType.START_BILLING_DISABLED);
         // TODO - ugly, fragile
@@ -599,7 +597,6 @@ public class TestBlockingCalculator extends JunctionTestSuiteNoDB {
         assertEquals(result.getRecurringPrice(), event.getRecurringPrice());
         assertEquals(result.getCurrency(), event.getCurrency());
         assertEquals(result.getDescription(), "");
-        assertEquals(result.getBillingMode(), event.getBillingMode());
         assertEquals(result.getBillingPeriod(), event.getBillingPeriod());
         assertEquals(result.getTransitionType(), SubscriptionBaseTransitionType.END_BILLING_DISABLED);
         // TODO - ugly, fragile
@@ -609,8 +606,8 @@ public class TestBlockingCalculator extends JunctionTestSuiteNoDB {
     private class MockBillingEvent extends DefaultBillingEvent {
 
         public MockBillingEvent() {
-            super(account, subscription1, clock.getUTCNow(), null, null, BigDecimal.ZERO, BigDecimal.TEN, Currency.USD, BillingPeriod.ANNUAL,
-                  4, BillingMode.IN_ADVANCE, "", 3L, SubscriptionBaseTransitionType.CREATE, DateTimeZone.UTC);
+            super(account, subscription1, clock.getUTCNow(), true, null, null, BigDecimal.ZERO, BigDecimal.TEN, Currency.USD, BillingPeriod.ANNUAL,
+                  4, "", 3L, SubscriptionBaseTransitionType.CREATE, DateTimeZone.UTC);
         }
     }
 
diff --git a/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestDefaultBillingEvent.java b/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestDefaultBillingEvent.java
index 79b50ea..a6a2125 100644
--- a/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestDefaultBillingEvent.java
+++ b/junction/src/test/java/org/killbill/billing/junction/plumbing/billing/TestDefaultBillingEvent.java
@@ -26,7 +26,6 @@ import javax.annotation.Nullable;
 
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
-import org.killbill.billing.catalog.api.BillingMode;
 import org.mockito.Mockito;
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -187,10 +186,10 @@ public class TestDefaultBillingEvent extends JunctionTestSuiteNoDB {
         final PlanPhase shotgunMonthly = createMockMonthlyPlanPhase(null, BigDecimal.ZERO, PhaseType.TRIAL);
 
         final Account account = new MockAccountBuilder().build();
-        return new DefaultBillingEvent(account, sub, effectiveDate,
+        return new DefaultBillingEvent(account, sub, effectiveDate, true,
                                        shotgun, shotgunMonthly,
                                        BigDecimal.ZERO, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, billCycleDay,
-                                       BillingMode.IN_ADVANCE, "Test Event 1", totalOrdering, type, DateTimeZone.UTC);
+                                       "Test Event 1", totalOrdering, type, DateTimeZone.UTC);
     }
 
     private MockPlanPhase createMockMonthlyPlanPhase(@Nullable final BigDecimal recurringRate,