Search in sources :

Example 11 with Duration

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

the class TestUserApiChangePlan method testMultipleChangeLastIMM.

@Test(groups = "slow")
public void testMultipleChangeLastIMM() throws SubscriptionBillingApiException, SubscriptionBaseApiException {
    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);
    // CHANGE EOT
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, "gunclubDiscount");
    subscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext);
    assertListenerStatus();
    // CHANGE
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    final PlanPhaseSpecifier planPhaseSpecifier1 = new PlanPhaseSpecifier("Assault-Rifle", BillingPeriod.ANNUAL, "gunclubDiscount");
    subscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier1), callContext);
    assertListenerStatus();
    final Plan currentPlan = subscription.getCurrentPlan();
    assertNotNull(currentPlan);
    assertEquals(currentPlan.getProduct().getName(), "Assault-Rifle");
    assertEquals(currentPlan.getProduct().getCategory(), ProductCategory.BASE);
    assertEquals(currentPlan.getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
    final PlanPhase currentPhase = subscription.getCurrentPhase();
    assertNotNull(currentPhase);
    assertEquals(currentPhase.getPhaseType(), PhaseType.DISCOUNT);
    assertListenerStatus();
}
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) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 12 with Duration

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

the class TestUserApiChangePlan method testMultipleChangeLastEOT.

@Test(groups = "slow")
public void testMultipleChangeLastEOT() throws SubscriptionBillingApiException, SubscriptionBaseApiException {
    DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, "Assault-Rifle", BillingPeriod.ANNUAL, "gunclubDiscount");
    final PlanPhase trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.TRIAL);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    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());
    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);
    // CHANGE EOT
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, "gunclubDiscount");
    subscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext);
    assertListenerStatus();
    // CHANGE EOT
    final PlanPhaseSpecifier planPhaseSpecifier1 = new PlanPhaseSpecifier("Pistol", BillingPeriod.ANNUAL, "gunclubDiscount");
    subscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier1), callContext);
    assertListenerStatus();
    // CHECK NO CHANGE OCCURED YET
    Plan currentPlan = subscription.getCurrentPlan();
    assertNotNull(currentPlan);
    assertEquals(currentPlan.getProduct().getName(), "Assault-Rifle");
    assertEquals(currentPlan.getProduct().getCategory(), ProductCategory.BASE);
    assertEquals(currentPlan.getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
    PlanPhase currentPhase = subscription.getCurrentPhase();
    assertNotNull(currentPhase);
    assertEquals(currentPhase.getPhaseType(), PhaseType.DISCOUNT);
    // ACTIVATE CHANGE BY MOVING AFTER CTD
    testListener.pushExpectedEvents(NextEvent.CHANGE);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    currentPlan = subscription.getCurrentPlan();
    assertNotNull(currentPlan);
    assertEquals(currentPlan.getProduct().getName(), "Pistol");
    assertEquals(currentPlan.getProduct().getCategory(), ProductCategory.BASE);
    assertEquals(currentPlan.getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
    currentPhase = subscription.getCurrentPhase();
    assertNotNull(currentPhase);
    assertEquals(currentPhase.getPhaseType(), PhaseType.DISCOUNT);
    // MOVE TO NEXT PHASE
    testListener.pushExpectedEvent(NextEvent.PHASE);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(6));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    currentPlan = subscription.getCurrentPlan();
    assertNotNull(currentPlan);
    assertEquals(currentPlan.getProduct().getName(), "Pistol");
    assertEquals(currentPlan.getProduct().getCategory(), ProductCategory.BASE);
    assertEquals(currentPlan.getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
    currentPhase = subscription.getCurrentPhase();
    assertNotNull(currentPhase);
    assertEquals(currentPhase.getPhaseType(), PhaseType.EVERGREEN);
    assertListenerStatus();
}
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) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 13 with Duration

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

the class TestUserApiError method testChangeSubscriptionFutureCancelled.

@Test(groups = "fast")
public void testChangeSubscriptionFutureCancelled() throws SubscriptionBaseApiException {
    SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME);
    final PlanPhase trialPhase = subscription.getCurrentPhase();
    // MOVE TO NEXT PHASE
    final PlanPhase currentPhase = subscription.getCurrentPhase();
    testListener.pushExpectedEvent(NextEvent.PHASE);
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD TO CANCEL IN FUTURE
    final DateTime expectedPhaseTrialChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), trialPhase.getDuration());
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(expectedPhaseTrialChange, ctd);
    setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext);
    subscription = subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    subscription.cancelWithPolicy(BillingActionPolicy.END_OF_TERM, callContext);
    try {
        final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME);
        subscription.changePlanWithDate(new DefaultEntitlementSpecifier(planPhaseSpecifier), clock.getUTCNow(), callContext);
        Assert.fail("Exception expected, error code: " + ErrorCode.SUB_CHANGE_FUTURE_CANCELLED);
    } catch (final SubscriptionBaseApiException e) {
        assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_FUTURE_CANCELLED.getCode());
    }
    assertListenerStatus();
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) 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 14 with Duration

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

the class TestUserApiCancel method testCancelSubscription_START_OF_TERM.

@Test(groups = "slow")
public void testCancelSubscription_START_OF_TERM() throws SubscriptionBaseApiException {
    // Set date in such a way that Phase align with the first of the month (and so matches our hardcoded accountData account BCD)
    final DateTime testStartDate = new DateTime(2016, 11, 1, 0, 3, 42, 0);
    clock.setDeltaFromReality(testStartDate.getMillis() - clock.getUTCNow().getMillis());
    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 currentPhase = subscription.getCurrentPhase();
    assertEquals(currentPhase.getPhaseType(), PhaseType.TRIAL);
    // Move out of TRIAL
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    // Artificially set the CTD
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(clock.getUTCNow(), ctd);
    setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext);
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    // Move ahead a bit abd cancel START_OF_TERM
    clock.addDays(5);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    subscription.cancelWithPolicy(BillingActionPolicy.START_OF_TERM, callContext);
    assertListenerStatus();
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    Assert.assertEquals(subscription.getAllTransitions().get(subscription.getAllTransitions().size() - 1).getTransitionType(), SubscriptionBaseTransitionType.CANCEL);
    Assert.assertEquals(new LocalDate(subscription.getAllTransitions().get(subscription.getAllTransitions().size() - 1).getEffectiveTransitionTime(), accountData.getTimeZone()), new LocalDate(2016, 12, 1));
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 15 with Duration

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

the class TestUserApiCancel method testCancelSubscriptionEOTWithChargeThroughDate.

@Test(groups = "slow")
public void testCancelSubscriptionEOTWithChargeThroughDate() throws SubscriptionBillingApiException, 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
    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);
    // CANCEL
    subscription.cancel(callContext);
    assertListenerStatus();
    // CANCEL a second time (first pending CANCEL should be made inactive)
    subscription.cancel(callContext);
    assertListenerStatus();
    assertEquals(subscription.getLastActiveProduct().getName(), prod);
    assertEquals(subscription.getLastActivePriceList().getName(), planSet);
    assertEquals(subscription.getLastActiveBillingPeriod(), term);
    assertEquals(subscription.getLastActiveCategory(), ProductCategory.BASE);
    final DateTime futureEndDate = subscription.getFutureEndDate();
    Assert.assertNotNull(futureEndDate);
    // MOVE TO EOT + RECHECK
    testListener.pushExpectedEvents(NextEvent.CANCEL);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    assertTrue(futureEndDate.compareTo(subscription.getEndDate()) == 0);
    final PlanPhase currentPhase = subscription.getCurrentPhase();
    assertNull(currentPhase);
    testUtil.checkNextPhaseChange(subscription, 0, null);
    assertEquals(subscription.getLastActiveProduct().getName(), prod);
    assertEquals(subscription.getLastActivePriceList().getName(), planSet);
    assertEquals(subscription.getLastActiveBillingPeriod(), term);
    assertEquals(subscription.getLastActiveCategory(), ProductCategory.BASE);
    assertListenerStatus();
}
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)

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