Search in sources :

Example 31 with BillingPeriod

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

the class TestUserApiCancel method testCancelPlanOnPendingSubscription2.

@Test(groups = "slow")
public void testCancelPlanOnPendingSubscription2() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    final LocalDate startDate = clock.getUTCToday().plusDays(5);
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList, startDate);
    assertEquals(subscription.getState(), Entitlement.EntitlementState.PENDING);
    assertEquals(subscription.getStartDate().compareTo(startDate.toDateTime(accountData.getReferenceTime())), 0);
    try {
        subscription.cancelWithDate(null, callContext);
        fail("Cancel plan should have failed : subscription PENDING");
    } catch (SubscriptionBaseApiException e) {
        assertEquals(e.getCode(), ErrorCode.SUB_INVALID_REQUESTED_DATE.getCode());
    }
    try {
        subscription.cancelWithDate(subscription.getStartDate().minusDays(1), callContext);
        fail("Cancel plan should have failed : subscription PENDING");
    } catch (SubscriptionBaseApiException e) {
        assertEquals(e.getCode(), ErrorCode.SUB_INVALID_REQUESTED_DATE.getCode());
    }
    subscription.cancelWithDate(subscription.getStartDate(), callContext);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.CANCEL);
    clock.addDays(5);
    assertListenerStatus();
    final DefaultSubscriptionBase subscription2 = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    assertEquals(subscription2.getStartDate().compareTo(subscription.getStartDate()), 0);
    assertEquals(subscription2.getState(), Entitlement.EntitlementState.CANCELLED);
    assertNull(subscription2.getCurrentPlan());
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 32 with BillingPeriod

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

the class TestUserApiCancel method testCancelSubscriptionIMM.

@Test(groups = "slow")
public void testCancelSubscriptionIMM() throws SubscriptionBaseApiException {
    final DateTime init = clock.getUTCNow();
    final String prod = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSet = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, prod, term, planSet);
    PlanPhase currentPhase = subscription.getCurrentPhase();
    assertEquals(currentPhase.getPhaseType(), PhaseType.TRIAL);
    // ADVANCE TIME still in trial
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(3));
    clock.addDeltaFromReality(it.toDurationMillis());
    final DateTime future = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    assertEquals(subscription.getLastActiveProduct().getName(), prod);
    assertEquals(subscription.getLastActivePriceList().getName(), planSet);
    assertEquals(subscription.getLastActiveBillingPeriod(), term);
    assertEquals(subscription.getLastActiveCategory(), ProductCategory.BASE);
    // CANCEL in trial period to get IMM policy
    subscription.cancel(callContext);
    currentPhase = subscription.getCurrentPhase();
    assertListenerStatus();
    assertEquals(subscription.getLastActiveProduct().getName(), prod);
    assertEquals(subscription.getLastActivePriceList().getName(), planSet);
    assertEquals(subscription.getLastActiveBillingPeriod(), term);
    assertEquals(subscription.getLastActiveCategory(), ProductCategory.BASE);
    assertNull(currentPhase);
    testUtil.checkNextPhaseChange(subscription, 0, null);
    assertListenerStatus();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 33 with BillingPeriod

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

the class TestUserApiCancel method testWithMultipleCancellationEvent.

@Test(groups = "slow")
public void testWithMultipleCancellationEvent() 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();
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    Assert.assertEquals(subscription.getAllTransitions().size(), 3);
    // Manually add a CANCEL event on the same EOT date as the previous one to verify the code is resilient enough to ignore it
    final SubscriptionBaseEvent cancelEvent = subscription.getEvents().get(subscription.getEvents().size() - 1);
    final SubscriptionEventModelDao newCancelEvent = new SubscriptionEventModelDao(cancelEvent);
    newCancelEvent.setId(UUID.randomUUID());
    final Handle handle = dbi.open();
    final SubscriptionEventSqlDao sqlDao = handle.attach(SubscriptionEventSqlDao.class);
    sqlDao.create(newCancelEvent, internalCallContext);
    subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    // The extra cancel event is being ignored
    Assert.assertEquals(subscription.getEvents().size(), 3);
    Assert.assertEquals(subscription.getAllTransitions().size(), 3);
    // We expect only one CANCEL event, this other one is skipped
    testListener.pushExpectedEvents(NextEvent.CANCEL);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // Our previous transition should be a CANCEL with a valid previous plan
    final SubscriptionBaseTransition previousTransition = subscription.getPreviousTransition();
    Assert.assertEquals(previousTransition.getPreviousState(), EntitlementState.ACTIVE);
    Assert.assertNotNull(previousTransition.getPreviousPlan());
}
Also used : SubscriptionEventSqlDao(org.killbill.billing.subscription.engine.dao.SubscriptionEventSqlDao) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Handle(org.skife.jdbi.v2.Handle) Test(org.testng.annotations.Test)

Example 34 with BillingPeriod

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

the class TestUserApiCancel method testCancelPlanOnPendingSubscription1.

@Test(groups = "slow")
public void testCancelPlanOnPendingSubscription1() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    final LocalDate startDate = clock.getUTCToday().plusDays(5);
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList, startDate);
    assertEquals(subscription.getState(), Entitlement.EntitlementState.PENDING);
    assertEquals(subscription.getStartDate().compareTo(startDate.toDateTime(accountData.getReferenceTime())), 0);
    // The code will be smart to infer the cancelation date as being the future startDate
    subscription.cancel(callContext);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.CANCEL);
    clock.addDays(5);
    assertListenerStatus();
    final DefaultSubscriptionBase subscription2 = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    assertEquals(subscription2.getStartDate().compareTo(subscription.getStartDate()), 0);
    assertEquals(subscription2.getState(), Entitlement.EntitlementState.CANCELLED);
    assertNull(subscription2.getCurrentPlan());
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 35 with BillingPeriod

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

the class TestUserApiCancel method testCancelSubscriptionWithInvalidRequestedDate.

@Test(groups = "slow", expectedExceptions = SubscriptionBaseApiException.class)
public void testCancelSubscriptionWithInvalidRequestedDate() throws SubscriptionBaseApiException {
    final String prod = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSet = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, prod, term, planSet);
    PlanPhase currentPhase = subscription.getCurrentPhase();
    assertEquals(currentPhase.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();
    currentPhase = subscription.getCurrentPhase();
    assertEquals(currentPhase.getPhaseType(), PhaseType.EVERGREEN);
    final DateTime invalidDate = subscription.getBundleStartDate().minusDays(3);
    // CANCEL in EVERGREEN period with an invalid Date (prior to the Creation Date)
    subscription.cancelWithDate(invalidDate, callContext);
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Aggregations

BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)111 Test (org.testng.annotations.Test)91 LocalDate (org.joda.time.LocalDate)61 DateTime (org.joda.time.DateTime)59 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)45 BigDecimal (java.math.BigDecimal)44 Account (org.killbill.billing.account.api.Account)44 Invoice (org.killbill.billing.invoice.api.Invoice)40 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)31 Plan (org.killbill.billing.catalog.api.Plan)20 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)20 Interval (org.joda.time.Interval)17 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)17 UUID (java.util.UUID)15 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)15 ArrayList (java.util.ArrayList)14 Duration (org.killbill.billing.catalog.api.Duration)10 Entitlement (org.killbill.billing.entitlement.api.Entitlement)10 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)10 HashMap (java.util.HashMap)9