Search in sources :

Example 6 with SubscriptionEventModelDao

use of org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao 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);
    subscriptionInternalApi.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);
    try {
        sqlDao.create(newCancelEvent, internalCallContext);
    } catch (EntityPersistenceException e) {
        Assert.fail(e.getMessage());
    }
    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 : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Duration(org.killbill.billing.catalog.api.Duration) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao) DateTime(org.joda.time.DateTime) Handle(org.skife.jdbi.v2.Handle) SubscriptionEventSqlDao(org.killbill.billing.subscription.engine.dao.SubscriptionEventSqlDao) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) EntityPersistenceException(org.killbill.billing.entity.EntityPersistenceException) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Aggregations

SubscriptionEventModelDao (org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)6 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)3 Date (java.util.Date)2 UUID (java.util.UUID)2 EntityPersistenceException (org.killbill.billing.entity.EntityPersistenceException)2 SubscriptionNotificationKey (org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey)2 SubscriptionBaseError (org.killbill.billing.subscription.exceptions.SubscriptionBaseError)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 Interval (org.joda.time.Interval)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 Duration (org.killbill.billing.catalog.api.Duration)1 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)1 SubscriptionApiException (org.killbill.billing.entitlement.api.SubscriptionApiException)1