Search in sources :

Example 11 with PlanPhase

use of org.killbill.billing.catalog.api.PlanPhase 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)

Example 12 with PlanPhase

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

the class TestUserApiCancel method testCancelSubscriptionEOTWithNoChargeThroughDate.

@Test(groups = "slow")
public void testCancelSubscriptionEOTWithNoChargeThroughDate() 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 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);
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.EVERGREEN);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    // CANCEL
    subscription.cancel(callContext);
    assertListenerStatus();
    final PlanPhase currentPhase = subscription.getCurrentPhase();
    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 13 with PlanPhase

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

the class TestUserApiChangePlan method tChangePlanBundleAlignEOTWithNoChargeThroughDate.

private void tChangePlanBundleAlignEOTWithNoChargeThroughDate(final String fromProd, final BillingPeriod fromTerm, final String fromPlanSet, final String toProd, final BillingPeriod toTerm, final String toPlanSet) {
    try {
        // CREATE
        final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, fromProd, fromTerm, fromPlanSet);
        // MOVE TO NEXT PHASE
        PlanPhase currentPhase = subscription.getCurrentPhase();
        testListener.pushExpectedEvent(NextEvent.PHASE);
        final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
        clock.addDeltaFromReality(it.toDurationMillis());
        final DateTime futureNow = clock.getUTCNow();
        final DateTime nextExpectedPhaseChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), currentPhase.getDuration());
        assertTrue(futureNow.isAfter(nextExpectedPhaseChange));
        assertListenerStatus();
        // CHANGE PLAN
        testListener.pushExpectedEvent(NextEvent.CHANGE);
        subscription.changePlan(new PlanSpecifier(toProd, toTerm, toPlanSet), null, callContext);
        assertListenerStatus();
        // CHECK CHANGE PLAN
        currentPhase = subscription.getCurrentPhase();
        checkChangePlan(subscription, toProd, ProductCategory.BASE, toTerm, PhaseType.EVERGREEN);
        assertListenerStatus();
    } catch (SubscriptionBaseApiException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) PlanSpecifier(org.killbill.billing.catalog.api.PlanSpecifier)

Example 14 with PlanPhase

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

the class TestLimits method testLimits.

@Test(groups = "fast")
public void testLimits() throws Exception {
    PlanPhase phase = catalog.findCurrentPhase("pistol-monthly-evergreen");
    Assert.assertNotNull(phase);
    /*
                     <usages>
                    <usage>
                        <billingPeriod>NO_BILLING_PERIOD</billingPeriod>
                        <limits>
                            <limit>
                                <unit>targets</unit>
                                <min>3</min>
                            </limit>
                            <limit>
                                <unit>misfires</unit>
                                <max>20</max>
                            </limit>
                        </limits>
                    </usage>
                </usages>
         */
    Assert.assertTrue(catalog.compliesWithLimits("pistol-monthly-evergreen", "targets", 3));
    Assert.assertTrue(catalog.compliesWithLimits("pistol-monthly-evergreen", "targets", 2000));
    Assert.assertFalse(catalog.compliesWithLimits("pistol-monthly-evergreen", "targets", 2));
    Assert.assertTrue(catalog.compliesWithLimits("pistol-monthly-evergreen", "misfires", 3));
    Assert.assertFalse(catalog.compliesWithLimits("pistol-monthly-evergreen", "misfires", 21));
    Assert.assertTrue(catalog.compliesWithLimits("pistol-monthly-evergreen", "misfires", -1));
    /*
            <product name="Shotgun">
              <category>BASE</category>
              <limits>
                <limit>
                    <unit>shells</unit>
                    <max>300</max>
                </limit>
              </limits>
            </product>
        */
    Assert.assertTrue(catalog.compliesWithLimits("shotgun-monthly-evergreen", "shells", 100));
    Assert.assertFalse(catalog.compliesWithLimits("shotgun-monthly-evergreen", "shells", 400));
    Assert.assertTrue(catalog.compliesWithLimits("shotgun-monthly-evergreen", "shells", 250));
    /*
                   <!-- shotgun-annual-evergreen -->
                   <usages>
                    <usage>
                        <billingPeriod>ANNUAL</billingPeriod>
                        <limits>
                            <limit>
                                <unit>shells</unit>
                                <max>200</max>
                            </limit>
                        </limits>
                    </usage>
                </usages>
         */
    Assert.assertTrue(catalog.compliesWithLimits("shotgun-annual-evergreen", "shells", 100));
    Assert.assertFalse(catalog.compliesWithLimits("shotgun-annual-evergreen", "shells", 400));
    Assert.assertFalse(catalog.compliesWithLimits("shotgun-annual-evergreen", "shells", 250));
}
Also used : PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Test(org.testng.annotations.Test)

Example 15 with PlanPhase

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

the class DefaultPlan method getAllPhases.

@Override
public PlanPhase[] getAllPhases() {
    final int length = initialPhases.length + 1;
    final PlanPhase[] allPhases = new DefaultPlanPhase[length];
    int cnt = 0;
    if (length > 1) {
        for (final PlanPhase cur : initialPhases) {
            allPhases[cnt++] = cur;
        }
    }
    allPhases[cnt++] = finalPhase;
    return allPhases;
}
Also used : PlanPhase(org.killbill.billing.catalog.api.PlanPhase) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Aggregations

PlanPhase (org.killbill.billing.catalog.api.PlanPhase)98 Plan (org.killbill.billing.catalog.api.Plan)77 Test (org.testng.annotations.Test)71 MockPlan (org.killbill.billing.catalog.MockPlan)50 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)49 LocalDate (org.joda.time.LocalDate)47 DateTime (org.joda.time.DateTime)43 BillingEventSet (org.killbill.billing.junction.BillingEventSet)43 BigDecimal (java.math.BigDecimal)42 BillingEvent (org.killbill.billing.junction.BillingEvent)41 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)40 Invoice (org.killbill.billing.invoice.api.Invoice)39 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)36 UUID (java.util.UUID)34 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)22 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)22 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)22 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)22 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)21 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)21