Search in sources :

Example 16 with Duration

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

the class TestUserApiCancel method testDoubleFutureLaterCancellation.

@Test(groups = "slow", description = "See https://github.com/killbill/killbill/issues/1207")
public void testDoubleFutureLaterCancellation() throws SubscriptionBaseApiException {
    final String prod = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSet = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE
    DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, prod, term, planSet);
    PlanPhase trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.TRIAL);
    // NEXT PHASE
    final DateTime expectedPhaseTrialChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), trialPhase.getDuration());
    testUtil.checkNextPhaseChange(subscription, 1, expectedPhaseTrialChange);
    // MOVE TO NEXT PHASE : 2012-06-07
    testListener.pushExpectedEvent(NextEvent.PHASE);
    Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.EVERGREEN);
    // SET CTD + RE READ SUBSCRIPTION + CHANGE PLAN
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(expectedPhaseTrialChange, ctd);
    setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext);
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    assertEquals(subscription.getLastActiveProduct().getName(), prod);
    assertEquals(subscription.getLastActivePriceList().getName(), planSet);
    assertEquals(subscription.getLastActiveBillingPeriod(), term);
    assertEquals(subscription.getLastActiveCategory(), ProductCategory.BASE);
    // EARLIER CANCELLATION EOT -> 2012-07-01
    subscription.cancelWithDate(clock.getUTCNow().plusDays(25), callContext);
    try {
        // CANCEL EOT -> 2012-07-06
        subscription.cancel(callContext);
        Assert.fail("Cancellation should fail as it is already future cancelled");
    } catch (final SubscriptionBaseApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.SUB_CANCEL_BAD_STATE.getCode());
    }
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 17 with Duration

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

the class PlanAligner method getPhaseAlignments.

private List<TimedPhase> getPhaseAlignments(final Plan plan, @Nullable final PhaseType initialPhase, final DateTime initialPhaseStartDate) throws SubscriptionBaseApiException {
    if (plan == null) {
        return Collections.emptyList();
    }
    final List<TimedPhase> result = new LinkedList<TimedPhase>();
    DateTime curPhaseStart = (initialPhase == null) ? initialPhaseStartDate : null;
    DateTime nextPhaseStart;
    for (final PlanPhase cur : plan.getAllPhases()) {
        // For create we can specify the phase so skip any phase until we reach initialPhase
        if (curPhaseStart == null) {
            if (initialPhase != cur.getPhaseType()) {
                continue;
            }
            curPhaseStart = initialPhaseStartDate;
        }
        result.add(new TimedPhase(cur, curPhaseStart));
        // STEPH check for duration null instead TimeUnit UNLIMITED
        if (cur.getPhaseType() != PhaseType.EVERGREEN) {
            final Duration curPhaseDuration = cur.getDuration();
            nextPhaseStart = addDuration(curPhaseStart, curPhaseDuration);
            if (nextPhaseStart == null) {
                throw new SubscriptionBaseError(String.format("Unexpected non ending UNLIMITED phase for plan %s", plan.getName()));
            }
            curPhaseStart = nextPhaseStart;
        }
    }
    if (initialPhase != null && curPhaseStart == null) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_BAD_PHASE, initialPhase);
    }
    return result;
}
Also used : SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) LinkedList(java.util.LinkedList) DateTime(org.joda.time.DateTime) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

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