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());
}
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();
}
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());
}
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());
}
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);
}
Aggregations