Search in sources :

Example 6 with Duration

use of org.killbill.billing.catalog.api.Duration in project killbill by killbill.

the class TestUserApiChangePlan method testChangePlanBundleAlignEOTWithChargeThroughDate.

private void testChangePlanBundleAlignEOTWithChargeThroughDate(final String fromProd, final BillingPeriod fromTerm, final String fromPlanSet, final String toProd, final BillingPeriod toTerm, final String toPlanSet) throws SubscriptionBillingApiException, SubscriptionBaseApiException {
    // CREATE
    DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, fromProd, fromTerm, fromPlanSet);
    final PlanPhase trialPhase = subscription.getCurrentPhase();
    final DateTime expectedPhaseTrialChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), trialPhase.getDuration());
    assertEquals(trialPhase.getPhaseType(), PhaseType.TRIAL);
    // MOVE TO NEXT PHASE
    testListener.pushExpectedEvent(NextEvent.PHASE);
    Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    PlanPhase currentPhase = subscription.getCurrentPhase();
    assertEquals(currentPhase.getPhaseType(), PhaseType.DISCOUNT);
    // SET CTD
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(expectedPhaseTrialChange, ctd);
    setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext);
    // RE READ SUBSCRIPTION + CHANGE PLAN
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(toProd, toTerm, toPlanSet);
    subscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext);
    assertListenerStatus();
    // CHECK CHANGE PLAN
    currentPhase = subscription.getCurrentPhase();
    checkChangePlan(subscription, fromProd, ProductCategory.BASE, fromTerm, PhaseType.DISCOUNT);
    // NEXT PHASE
    final DateTime nextExpectedPhaseChange = TestSubscriptionHelper.addDuration(expectedPhaseTrialChange, currentPhase.getDuration());
    testUtil.checkNextPhaseChange(subscription, 2, nextExpectedPhaseChange);
    // ALSO VERIFY PENDING CHANGE EVENT
    final List<SubscriptionBaseEvent> events = dao.getPendingEventsForSubscription(subscription.getId(), internalCallContext);
    assertTrue(events.get(0) instanceof ApiEvent);
    // MOVE TO EOT
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    currentPhase = subscription.getCurrentPhase();
    checkChangePlan(subscription, toProd, ProductCategory.BASE, toTerm, PhaseType.DISCOUNT);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) ApiEvent(org.killbill.billing.subscription.events.user.ApiEvent) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval)

Example 7 with Duration

use of org.killbill.billing.catalog.api.Duration in project killbill by killbill.

the class TestUserApiChangePlan method testChangePlanWithBCD.

@Test(groups = "slow")
public void testChangePlanWithBCD() throws SubscriptionBillingApiException, SubscriptionBaseApiException {
    final DateTime init = clock.getUTCNow();
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), ObjectType.ACCOUNT, this.internalCallContext.getUpdatedBy(), this.internalCallContext.getCallOrigin(), this.internalCallContext.getContextUserType(), this.internalCallContext.getUserToken(), this.internalCallContext.getTenantRecordId());
    DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, "Assault-Rifle", BillingPeriod.MONTHLY, "gunclubDiscount");
    final PlanPhase trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.TRIAL);
    // MOVE TO NEXT PHASE
    testListener.pushExpectedEvent(NextEvent.PHASE);
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD
    final List<Duration> durationList = new ArrayList<Duration>();
    durationList.add(trialPhase.getDuration());
    // durationList.add(subscription.getCurrentPhase().getDuration());
    final DateTime startDiscountPhase = TestSubscriptionHelper.addDuration(subscription.getStartDate(), durationList);
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(startDiscountPhase, ctd);
    setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext);
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, "gunclubDiscount");
    testListener.pushExpectedEvents(NextEvent.CHANGE, NextEvent.BCD_CHANGE);
    subscription.changePlanWithDate(new DefaultEntitlementSpecifier(planPhaseSpecifier, 18, null, null), clock.getUTCNow(), callContext);
    assertListenerStatus();
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    assertNotNull(subscription.getBillCycleDayLocal());
    assertEquals(subscription.getBillCycleDayLocal().intValue(), 18);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) ArrayList(java.util.ArrayList) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 8 with Duration

use of org.killbill.billing.catalog.api.Duration in project killbill by killbill.

the class TestUserApiAddOn method testCreateCancelAddonAndThenBP.

@Test(groups = "slow")
public void testCreateCancelAddonAndThenBP() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.ANNUAL;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final String aoProduct = "Telescopic-Scope";
    final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
    final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
    assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);
    // Move clock after a month
    Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD TO CANCEL IN FUTURE
    final DateTime now = clock.getUTCNow();
    final Duration aoCtd = testUtil.getDurationMonth(1);
    final DateTime newAOChargedThroughDate = TestSubscriptionHelper.addDuration(now, aoCtd);
    setChargedThroughDate(aoSubscription.getId(), newAOChargedThroughDate, internalCallContext);
    final Duration bpCtd = testUtil.getDurationMonth(11);
    final DateTime newBPChargedThroughDate = TestSubscriptionHelper.addDuration(now, bpCtd);
    setChargedThroughDate(baseSubscription.getId(), newBPChargedThroughDate, internalCallContext);
    baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    // CANCEL AO
    aoSubscription.cancel(callContext);
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);
    assertTrue(aoSubscription.isFutureCancelled());
    // CANCEL BASE NOW
    baseSubscription.cancel(callContext);
    baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    assertEquals(baseSubscription.getState(), EntitlementState.ACTIVE);
    assertTrue(baseSubscription.isFutureCancelled());
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    List<SubscriptionBaseTransition> aoTransitions = aoSubscription.getAllTransitions();
    assertEquals(aoTransitions.size(), 3);
    assertEquals(aoTransitions.get(0).getTransitionType(), SubscriptionBaseTransitionType.CREATE);
    assertEquals(aoTransitions.get(1).getTransitionType(), SubscriptionBaseTransitionType.PHASE);
    assertEquals(aoTransitions.get(2).getTransitionType(), SubscriptionBaseTransitionType.CANCEL);
    assertTrue(aoSubscription.getFutureEndDate().compareTo(newAOChargedThroughDate) == 0);
    testListener.pushExpectedEvent(NextEvent.UNCANCEL);
    aoSubscription.uncancel(callContext);
    assertListenerStatus();
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    aoTransitions = aoSubscription.getAllTransitions();
    assertEquals(aoTransitions.size(), 3);
    assertEquals(aoTransitions.get(0).getTransitionType(), SubscriptionBaseTransitionType.CREATE);
    assertEquals(aoTransitions.get(1).getTransitionType(), SubscriptionBaseTransitionType.PHASE);
    assertEquals(aoTransitions.get(2).getTransitionType(), SubscriptionBaseTransitionType.CANCEL);
    assertTrue(aoSubscription.getFutureEndDate().compareTo(newBPChargedThroughDate) == 0);
    assertListenerStatus();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Duration(org.killbill.billing.catalog.api.Duration) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 9 with Duration

use of org.killbill.billing.catalog.api.Duration in project killbill by killbill.

the class TestUserApiAddOn method testCancelBPWithAddon.

@Test(groups = "slow")
public void testCancelBPWithAddon() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final String aoProduct = "Telescopic-Scope";
    final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
    final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    // MOVE CLOCK AFTER TRIAL + AO DISCOUNT
    Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(2));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD TO CANCEL IN FUTURE
    final DateTime now = clock.getUTCNow();
    final Duration ctd = testUtil.getDurationMonth(1);
    // Why not just use clock.getUTCNow().plusMonths(1) ?
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(now, ctd);
    setChargedThroughDate(baseSubscription.getId(), newChargedThroughDate, internalCallContext);
    baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    // FUTURE CANCELLATION
    baseSubscription.cancel(callContext);
    // REFETCH AO SUBSCRIPTION AND CHECK THIS IS ACTIVE
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);
    assertTrue(aoSubscription.isFutureCancelled());
    // MOVE AFTER CANCELLATION
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // REFETCH AO SUBSCRIPTION AND CHECK THIS IS CANCELLED
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.CANCELLED);
    assertListenerStatus();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Duration(org.killbill.billing.catalog.api.Duration) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 10 with Duration

use of org.killbill.billing.catalog.api.Duration in project killbill by killbill.

the class TestUserApiAddOn method testChangeBPWithAddonIncluded.

@Test(groups = "slow")
public void testChangeBPWithAddonIncluded() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final String aoProduct = "Telescopic-Scope";
    final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
    final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    // MOVE CLOCK AFTER TRIAL + AO DISCOUNT
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(2));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD TO CHANGE IN FUTURE
    final DateTime now = clock.getUTCNow();
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(now, ctd);
    setChargedThroughDate(baseSubscription.getId(), newChargedThroughDate, internalCallContext);
    baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    // CHANGE IMMEDIATELY WITH TO BP WITH NON INCLUDED ADDON
    final String newBaseProduct = "Assault-Rifle";
    final BillingPeriod newBaseTerm = BillingPeriod.MONTHLY;
    final String newBasePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    final List<EntitlementAOStatusDryRun> aoStatus = subscriptionInternalApi.getDryRunChangePlanStatus(baseSubscription.getId(), newBaseProduct, now, internalCallContext);
    assertEquals(aoStatus.size(), 1);
    assertEquals(aoStatus.get(0).getId(), aoSubscription.getId());
    assertEquals(aoStatus.get(0).getProductName(), aoProduct);
    assertEquals(aoStatus.get(0).getBillingPeriod(), aoTerm);
    assertEquals(aoStatus.get(0).getPhaseType(), aoSubscription.getCurrentPhase().getPhaseType());
    assertEquals(aoStatus.get(0).getPriceList(), aoSubscription.getCurrentPriceList().getName());
    assertEquals(aoStatus.get(0).getReason(), DryRunChangeReason.AO_INCLUDED_IN_NEW_PLAN);
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(newBaseProduct, newBaseTerm, newBasePriceList, null);
    baseSubscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext);
    assertListenerStatus();
    // REFETCH AO SUBSCRIPTION AND CHECK THIS CANCELLED
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.CANCELLED);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) EntitlementAOStatusDryRun(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun) Duration(org.killbill.billing.catalog.api.Duration) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Aggregations

DateTime (org.joda.time.DateTime)17 Duration (org.killbill.billing.catalog.api.Duration)17 Interval (org.joda.time.Interval)14 Test (org.testng.annotations.Test)14 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)12 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)10 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)8 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)8 ArrayList (java.util.ArrayList)3 Plan (org.killbill.billing.catalog.api.Plan)3 EntitlementAOStatusDryRun (org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun)2 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)2 LinkedList (java.util.LinkedList)1 LocalDate (org.joda.time.LocalDate)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)1 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)1 SubscriptionEventSqlDao (org.killbill.billing.subscription.engine.dao.SubscriptionEventSqlDao)1 SubscriptionEventModelDao (org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)1 ApiEvent (org.killbill.billing.subscription.events.user.ApiEvent)1