Skip to content

Commit

Permalink
entitlement: move billing LocalDate -> DateTime computation to subscr…
Browse files Browse the repository at this point in the history
…iption

Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
  • Loading branch information
pierre committed Apr 24, 2018
1 parent 3d9efff commit cc6066d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 79 deletions.
Expand Up @@ -19,21 +19,21 @@


import java.util.UUID; import java.util.UUID;


import org.joda.time.DateTime; import org.joda.time.LocalDate;
import org.killbill.billing.entitlement.api.EntitlementSpecifier; import org.killbill.billing.entitlement.api.EntitlementSpecifier;


public class SubscriptionBaseWithAddOnsSpecifier { public class SubscriptionBaseWithAddOnsSpecifier {


private final UUID bundleId; private final UUID bundleId;
private final String bundleExternalKey; private final String bundleExternalKey;
private final Iterable<EntitlementSpecifier> entitlementSpecifiers; private final Iterable<EntitlementSpecifier> entitlementSpecifiers;
private final DateTime billingEffectiveDate; private final LocalDate billingEffectiveDate;
private final boolean isMigrated; private final boolean isMigrated;


public SubscriptionBaseWithAddOnsSpecifier(final UUID bundleId, public SubscriptionBaseWithAddOnsSpecifier(final UUID bundleId,
final String bundleExternalKey, final String bundleExternalKey,
final Iterable<EntitlementSpecifier> entitlementSpecifiers, final Iterable<EntitlementSpecifier> entitlementSpecifiers,
final DateTime billingEffectiveDate, final LocalDate billingEffectiveDate,
final boolean isMigrated) { final boolean isMigrated) {
this.bundleId = bundleId; this.bundleId = bundleId;
this.bundleExternalKey = bundleExternalKey; this.bundleExternalKey = bundleExternalKey;
Expand All @@ -54,7 +54,7 @@ public Iterable<EntitlementSpecifier> getEntitlementSpecifiers() {
return entitlementSpecifiers; return entitlementSpecifiers;
} }


public DateTime getBillingEffectiveDate() { public LocalDate getBillingEffectiveDate() {
return billingEffectiveDate; return billingEffectiveDate;
} }


Expand Down
Expand Up @@ -186,12 +186,10 @@ public List<UUID> doCall(final EntitlementApi entitlementApi, final EntitlementC
contextWithValidAccountRecordId); contextWithValidAccountRecordId);
upTo = upTo == null || upTo.compareTo(entitlementRequestedDate) < 0 ? entitlementRequestedDate : upTo; upTo = upTo == null || upTo.compareTo(entitlementRequestedDate) < 0 ? entitlementRequestedDate : upTo;


final DateTime billingEffectiveDateTime = (baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate() != null) ?
contextWithValidAccountRecordId.toUTCDateTime(baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate()) : null;
final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifier.getBundleId(), final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifier.getBundleId(),
baseEntitlementWithAddOnsSpecifier.getExternalKey(), baseEntitlementWithAddOnsSpecifier.getExternalKey(),
baseEntitlementWithAddOnsSpecifier.getEntitlementSpecifier(), baseEntitlementWithAddOnsSpecifier.getEntitlementSpecifier(),
billingEffectiveDateTime, baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate(),
baseEntitlementWithAddOnsSpecifier.isMigrated()); baseEntitlementWithAddOnsSpecifier.isMigrated());
subscriptionBaseWithAddOnsSpecifiers.add(subscriptionBaseWithAddOnsSpecifier); subscriptionBaseWithAddOnsSpecifiers.add(subscriptionBaseWithAddOnsSpecifier);
} }
Expand Down Expand Up @@ -289,12 +287,10 @@ public UUID doCall(final EntitlementApi entitlementApi, final EntitlementContext


try { try {
final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifierAfterPlugins = getFirstBaseEntitlementWithAddOnsSpecifier(updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers()); final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifierAfterPlugins = getFirstBaseEntitlementWithAddOnsSpecifier(updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers());
final DateTime billingEffectiveDateTime = (baseEntitlementWithAddOnsSpecifierAfterPlugins.getBillingEffectiveDate() != null) ?
context.toUTCDateTime(baseEntitlementWithAddOnsSpecifierAfterPlugins.getBillingEffectiveDate()) : null;
final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifierAfterPlugins.getBundleId(), final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifierAfterPlugins.getBundleId(),
baseEntitlementWithAddOnsSpecifierAfterPlugins.getExternalKey(), baseEntitlementWithAddOnsSpecifierAfterPlugins.getExternalKey(),
baseEntitlementWithAddOnsSpecifierAfterPlugins.getEntitlementSpecifier(), baseEntitlementWithAddOnsSpecifierAfterPlugins.getEntitlementSpecifier(),
billingEffectiveDateTime, baseEntitlementWithAddOnsSpecifierAfterPlugins.getBillingEffectiveDate(),
baseEntitlementWithAddOnsSpecifierAfterPlugins.isMigrated()); baseEntitlementWithAddOnsSpecifierAfterPlugins.isMigrated());


final List<SubscriptionBaseWithAddOns> subscriptionsWithAddOns = subscriptionBaseInternalApi.createBaseSubscriptionsWithAddOns(ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), final List<SubscriptionBaseWithAddOns> subscriptionsWithAddOns = subscriptionBaseInternalApi.createBaseSubscriptionsWithAddOns(ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier),
Expand Down
Expand Up @@ -279,7 +279,8 @@ public List<SubscriptionBaseWithAddOns> createBaseSubscriptionsWithAddOns(final


final Collection<SubscriptionAndAddOnsSpecifier> subscriptionAndAddOns = new ArrayList<SubscriptionAndAddOnsSpecifier>(); final Collection<SubscriptionAndAddOnsSpecifier> subscriptionAndAddOns = new ArrayList<SubscriptionAndAddOnsSpecifier>();
for (final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier : subscriptionWithAddOnsSpecifiers) { for (final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier : subscriptionWithAddOnsSpecifiers) {
final DateTime billingRequestedDateRaw = subscriptionBaseWithAddOnsSpecifier.getBillingEffectiveDate() != null ? subscriptionBaseWithAddOnsSpecifier.getBillingEffectiveDate() : now; final DateTime billingRequestedDateRaw = (subscriptionBaseWithAddOnsSpecifier.getBillingEffectiveDate() != null) ?
context.toUTCDateTime(subscriptionBaseWithAddOnsSpecifier.getBillingEffectiveDate()) : now;


final Collection<EntitlementSpecifier> reorderedSpecifiers = new ArrayList<EntitlementSpecifier>(); final Collection<EntitlementSpecifier> reorderedSpecifiers = new ArrayList<EntitlementSpecifier>();
// Note: billingRequestedDateRaw might not be accurate here (add-on with a too early date passed)? // Note: billingRequestedDateRaw might not be accurate here (add-on with a too early date passed)?
Expand Down
Expand Up @@ -305,15 +305,15 @@ public void testTransferWithAO() throws Exception {
clock.addDays(3); clock.addDays(3);
final String aoProduct1 = "Telescopic-Scope"; final String aoProduct1 = "Telescopic-Scope";
final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY; final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY;
final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, baseSubscription, aoProduct1, aoTerm1, basePriceList); final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, aoProduct1, aoTerm1, basePriceList);
assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE); assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE);


// MOVE ANOTHER 25 DAYS AND CREATE AO2 [ BP STILL IN TRIAL] // MOVE ANOTHER 25 DAYS AND CREATE AO2 [ BP STILL IN TRIAL]
// LASER-SCOPE IS SUBSCRIPTION ALIGN SO EVERGREN WILL ONLY START IN A MONTH // LASER-SCOPE IS SUBSCRIPTION ALIGN SO EVERGREN WILL ONLY START IN A MONTH
clock.addDays(25); clock.addDays(25);
final String aoProduct2 = "Laser-Scope"; final String aoProduct2 = "Laser-Scope";
final BillingPeriod aoTerm2 = BillingPeriod.MONTHLY; final BillingPeriod aoTerm2 = BillingPeriod.MONTHLY;
final DefaultSubscriptionBase aoSubscription2 = testUtil.createSubscription(bundle, baseSubscription, aoProduct2, aoTerm2, basePriceList); final DefaultSubscriptionBase aoSubscription2 = testUtil.createSubscription(bundle, aoProduct2, aoTerm2, basePriceList);
assertEquals(aoSubscription2.getState(), EntitlementState.ACTIVE); assertEquals(aoSubscription2.getState(), EntitlementState.ACTIVE);


// MOVE AFTER TRIAL AND AO DISCOUNT PHASE [LASER SCOPE STILL IN DISCOUNT] // MOVE AFTER TRIAL AND AO DISCOUNT PHASE [LASER SCOPE STILL IN DISCOUNT]
Expand Down Expand Up @@ -397,7 +397,7 @@ public void testTransferWithAOCancelled() throws Exception {
clock.addDays(3); clock.addDays(3);
final String aoProduct1 = "Telescopic-Scope"; final String aoProduct1 = "Telescopic-Scope";
final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY; final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY;
final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, baseSubscription, aoProduct1, aoTerm1, basePriceList); final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, aoProduct1, aoTerm1, basePriceList);
assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE); assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE);


testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
Expand Down
Expand Up @@ -44,7 +44,6 @@
import org.killbill.billing.entitlement.api.SubscriptionEventType; import org.killbill.billing.entitlement.api.SubscriptionEventType;
import org.killbill.billing.invoice.api.DryRunArguments; import org.killbill.billing.invoice.api.DryRunArguments;
import org.killbill.billing.invoice.api.DryRunType; import org.killbill.billing.invoice.api.DryRunType;
import org.killbill.billing.subscription.api.SubscriptionBase;
import org.killbill.billing.subscription.api.SubscriptionBaseInternalApi; import org.killbill.billing.subscription.api.SubscriptionBaseInternalApi;
import org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns; import org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns;
import org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier; import org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier;
Expand Down Expand Up @@ -138,40 +137,27 @@ public List<PlanPhasePriceOverride> getPlanPhasePriceOverrides() {
}; };
} }


public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final DateTime requestedDate) public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final LocalDate requestedDate)
throws SubscriptionBaseApiException { throws SubscriptionBaseApiException {
return createSubscription(bundle, productName, term, planSet, null, requestedDate); return createSubscription(bundle, productName, term, planSet, null, requestedDate);
} }


public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final DateTime requestedDate)
throws SubscriptionBaseApiException {
return createSubscription(bundle, null, productName, term, planSet, phaseType, requestedDate);
}

public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final SubscriptionBase baseSubscription, final String aoProduct, final BillingPeriod aoTerm, final String aoPriceList) throws SubscriptionBaseApiException {
return createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList, null, null);
}

public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet) public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet)
throws SubscriptionBaseApiException { throws SubscriptionBaseApiException {
return createSubscription(bundle, null, productName, term, planSet, null, null); return createSubscription(bundle, productName, term, planSet, null, null);
}

public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final SubscriptionBase baseSubscription, final String productName, final BillingPeriod term, final String planSet, final DateTime requestedDate) throws SubscriptionBaseApiException {
return createSubscription(bundle, baseSubscription, productName, term, planSet, null, requestedDate);
} }


public DefaultSubscriptionBase createSubscription(final boolean noEvents, final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet) throws SubscriptionBaseApiException { public DefaultSubscriptionBase createSubscription(final boolean noEvents, final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet) throws SubscriptionBaseApiException {
return createSubscription(noEvents, bundle, null, productName, term, planSet, null, null); return createSubscription(noEvents, bundle, productName, term, planSet, null, null);
} }


public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final SubscriptionBase baseSubscription, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final DateTime requestedDate) public DefaultSubscriptionBase createSubscription(final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final LocalDate requestedDate)
throws SubscriptionBaseApiException { throws SubscriptionBaseApiException {
return createSubscription(false, bundle, baseSubscription, productName, term, planSet, phaseType, requestedDate); return createSubscription(false, bundle, productName, term, planSet, phaseType, requestedDate);
} }


public DefaultSubscriptionBase createSubscription(final boolean noEvents, @Nullable final SubscriptionBaseBundle bundle, final SubscriptionBase baseSubscription, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final DateTime requestedDate) private DefaultSubscriptionBase createSubscription(final boolean noEvents, @Nullable final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final LocalDate requestedDate)
throws SubscriptionBaseApiException { throws SubscriptionBaseApiException {
// Make sure the right account information is used // Make sure the right account information is used
final InternalCallContext internalCallContext = bundle == null ? this.internalCallContext : internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), final InternalCallContext internalCallContext = bundle == null ? this.internalCallContext : internalCallContextFactory.createInternalCallContext(bundle.getAccountId(),
ObjectType.ACCOUNT, ObjectType.ACCOUNT,
Expand All @@ -189,7 +175,7 @@ public DefaultSubscriptionBase createSubscription(final boolean noEvents, @Nulla
} }
} }


if (!noEvents && (requestedDate == null || requestedDate.compareTo(clock.getUTCNow()) <= 0)) { if (!noEvents && (requestedDate == null || requestedDate.compareTo(clock.getUTCToday()) <= 0)) {
testListener.pushExpectedEvent(NextEvent.CREATE); testListener.pushExpectedEvent(NextEvent.CREATE);
} }


Expand Down
Expand Up @@ -59,7 +59,7 @@ public void testCreateCancelAddon() throws SubscriptionBaseApiException {
final BillingPeriod aoTerm = BillingPeriod.MONTHLY; final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;


DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE); assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);


testListener.pushExpectedEvent(NextEvent.CANCEL); testListener.pushExpectedEvent(NextEvent.CANCEL);
Expand All @@ -85,7 +85,7 @@ public void testCreateCancelAddonAndThenBP() throws SubscriptionBaseApiException
final BillingPeriod aoTerm = BillingPeriod.MONTHLY; final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;


DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE); assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);


// Move clock after a month // Move clock after a month
Expand Down Expand Up @@ -159,7 +159,7 @@ public void testCancelBPWithAddon() throws SubscriptionBaseApiException {
final BillingPeriod aoTerm = BillingPeriod.MONTHLY; final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;


DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);


testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
Expand Down Expand Up @@ -213,7 +213,7 @@ public void testCancelUncancelBPWithAddon() throws SubscriptionBaseApiException
final BillingPeriod aoTerm = BillingPeriod.MONTHLY; final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;


DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);


testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
Expand Down Expand Up @@ -277,7 +277,7 @@ public void testChangeBPWithAddonIncluded() throws SubscriptionBaseApiException
final BillingPeriod aoTerm = BillingPeriod.MONTHLY; final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;


DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);


testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
Expand Down Expand Up @@ -335,7 +335,7 @@ public void testChangeBPWithAddonNonAvailable() throws SubscriptionBaseApiExcept
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;


// CREATE AO // CREATE AO
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);


testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE); testListener.pushExpectedEvent(NextEvent.PHASE);
Expand Down Expand Up @@ -434,7 +434,7 @@ private void testAddonCreateInternal(final String aoProduct, final BillingPeriod


// CREATE ADDON // CREATE ADDON
final DateTime beforeAOCreation = clock.getUTCNow(); final DateTime beforeAOCreation = clock.getUTCNow();
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, baseSubscription, aoProduct, aoTerm, aoPriceList); DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
final DateTime afterAOCreation = clock.getUTCNow(); final DateTime afterAOCreation = clock.getUTCNow();


// CHECK EVERYTHING // CHECK EVERYTHING
Expand Down

0 comments on commit cc6066d

Please sign in to comment.