Search in sources :

Example 56 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class TestTransfer method testTransferBPNoTrialWithCTD.

@Test(groups = "slow")
public void testTransferBPNoTrialWithCTD() throws Exception {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    // MOVE AFTER TRIAL
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(40);
    assertListenerStatus();
    // SET CTD
    final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
    setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
    final DateTime transferRequestedDate = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.TRANSFER);
    transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, false, false, callContext);
    assertListenerStatus();
    // CHECK OLD BASE IS CANCEL AT THE TRANSFER DATE
    final SubscriptionBase oldBaseSubscription = subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    assertNotNull(oldBaseSubscription.getFutureEndDate());
    assertTrue(oldBaseSubscription.getFutureEndDate().compareTo(ctd) == 0);
    // CHECK NEW BUNDLE EXIST, WITH ONE SUBSCRIPTION STARTING ON TRANSFER_DATE
    final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
    assertEquals(bundlesForAccountAndKey.size(), 1);
    final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
    final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
    assertEquals(subscriptions.size(), 1);
    final SubscriptionBase newBaseSubscription = subscriptions.get(0);
    assertTrue(((DefaultSubscriptionBase) newBaseSubscription).getAlignStartDate().compareTo(((DefaultSubscriptionBase) baseSubscription).getAlignStartDate()) == 0);
    // CHECK ONLY ONE PHASE EXISTS
    assertEquals(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).size(), 1);
    Plan newPlan = newBaseSubscription.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), baseProduct);
    assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
    // MAKE A PLAN CHANGE IMM
    clock.addDays(5);
    final String newBaseProduct1 = "Assault-Rifle";
    final BillingPeriod newBaseTerm1 = BillingPeriod.ANNUAL;
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(newBaseProduct1, newBaseTerm1, basePriceList);
    newBaseSubscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier, null, null, null), callContext);
    assertListenerStatus();
    newPlan = newBaseSubscription.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), newBaseProduct1);
    assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
    // SET CTD AND MAKE CHANGE EOT
    clock.addDays(2);
    final DateTime newCtd = newBaseSubscription.getStartDate().plusYears(1);
    setChargedThroughDate(newBaseSubscription.getId(), newCtd, internalCallContext);
    final SubscriptionBase newBaseSubscriptionWithCtd = subscriptionInternalApi.getSubscriptionFromId(newBaseSubscription.getId(), internalCallContext);
    final String newBaseProduct2 = "Pistol";
    final BillingPeriod newBaseTerm2 = BillingPeriod.ANNUAL;
    final PlanPhaseSpecifier planPhaseSpecifier1 = new PlanPhaseSpecifier(newBaseProduct2, newBaseTerm2, basePriceList);
    newBaseSubscriptionWithCtd.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier1, null, null, null), callContext);
    newPlan = newBaseSubscriptionWithCtd.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), newBaseProduct1);
    assertEquals(newBaseSubscriptionWithCtd.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
    assertNotNull(newBaseSubscriptionWithCtd.getPendingTransition());
    assertEquals(newBaseSubscriptionWithCtd.getPendingTransition().getEffectiveTransitionTime(), newCtd);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 57 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class TestTransfer method testTransferBPInTrialWithNoCTD.

@Test(groups = "slow")
public void testTransferBPInTrialWithNoCTD() throws Exception {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final DateTime evergreenPhaseDate = ((DefaultSubscriptionBase) baseSubscription).getPendingTransition().getEffectiveTransitionTime();
    // MOVE A LITTLE, STILL IN TRIAL
    clock.addDays(20);
    final DateTime beforeTransferDate = clock.getUTCNow();
    final DateTime transferRequestedDate = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.TRANSFER);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, false, false, callContext);
    assertListenerStatus();
    final DateTime afterTransferDate = clock.getUTCNow();
    // CHECK OLD BASE IS CANCEL AT THE TRANSFER DATE
    final SubscriptionBase oldBaseSubscription = subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    assertNotNull(oldBaseSubscription.getEndDate());
    testUtil.assertDateWithin(oldBaseSubscription.getEndDate(), beforeTransferDate, afterTransferDate);
    assertTrue(oldBaseSubscription.getEndDate().compareTo(transferRequestedDate) == 0);
    // CHECK NEW BUNDLE EXIST, WITH ONE SUBSCRIPTION STARTING ON TRANSFER_DATE
    final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
    assertEquals(bundlesForAccountAndKey.size(), 1);
    final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
    final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
    assertEquals(subscriptions.size(), 1);
    final SubscriptionBase newBaseSubscription = subscriptions.get(0);
    assertTrue(((DefaultSubscriptionBase) newBaseSubscription).getAlignStartDate().compareTo(((DefaultSubscriptionBase) oldBaseSubscription).getAlignStartDate()) == 0);
    // CHECK NEXT PENDING PHASE IS ALIGNED WITH OLD SUBSCRIPTION START DATE
    assertEquals(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).size(), 2);
    assertTrue(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).get(1).getEffectiveTransitionTime().compareTo(evergreenPhaseDate) == 0);
    final Plan newPlan = newBaseSubscription.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), baseProduct);
    assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.TRIAL);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 58 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class TestPlanAligner method testCreateWithTargetPhaseType4.

// 
// Scenario : change Plan with START_OF_SUBSCRIPTION after skipping TRIAL on Create to a new Plan that has {DISCOUNT, EVERGREEN}
// 
@Test(groups = "fast")
public void testCreateWithTargetPhaseType4() throws Exception {
    final String productName = "pistol-monthly";
    final DateTime now = clock.getUTCNow();
    final DateTime bundleStartDate = now;
    final DateTime alignStartDate = bundleStartDate;
    final Plan plan = currentVersion.findPlan(productName);
    final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
    Assert.assertEquals(phases.length, 2);
    Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
    Assert.assertEquals(phases[0].getStartPhase(), now);
    Assert.assertNull(phases[1]);
    final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
    final String newProductName = "pistol-annual-gunclub-discount-notrial";
    final Plan newPlan = currentVersion.findPlan(newProductName);
    final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15);
    // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType
    final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
    Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
    Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}
Also used : DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 59 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class TestPlanAligner method testCreateWithTargetPhaseType2.

// 
// Scenario : change Plan with START_OF_SUBSCRIPTION after skipping TRIAL on Create to a new Plan that has {TRIAL, EVERGREEN}
// 
@Test(groups = "fast")
public void testCreateWithTargetPhaseType2() throws Exception {
    final String productName = "pistol-monthly";
    final DateTime now = clock.getUTCNow();
    final DateTime bundleStartDate = now;
    final DateTime alignStartDate = bundleStartDate;
    final Plan plan = currentVersion.findPlan(productName);
    final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
    Assert.assertEquals(phases.length, 2);
    Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
    Assert.assertEquals(phases[0].getStartPhase(), now);
    Assert.assertNull(phases[1]);
    final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
    final String newProductName = "shotgun-monthly";
    final Plan newPlan = currentVersion.findPlan(newProductName);
    final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15);
    final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
    Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
    Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}
Also used : DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 60 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class TestPlanAligner method testCreateWithTargetPhaseType5.

// 
// Scenario : change Plan with START_OF_SUBSCRIPTION after skipping TRIAL on Create to a new Plan that has {TRIAL, FIXEDTERM}
// 
@Test(groups = "fast")
public void testCreateWithTargetPhaseType5() throws Exception {
    final String productName = "pistol-monthly";
    final DateTime now = clock.getUTCNow();
    final DateTime bundleStartDate = now;
    final DateTime alignStartDate = bundleStartDate;
    final Plan plan = currentVersion.findPlan(productName);
    final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
    Assert.assertEquals(phases.length, 2);
    Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
    Assert.assertEquals(phases[0].getStartPhase(), now);
    Assert.assertNull(phases[1]);
    final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
    final String newProductName = "pistol-monthly-fixedterm";
    final Plan newPlan = currentVersion.findPlan(newProductName);
    final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusDays(5);
    final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
    // Initial phase EVERGREEN does not exist in the new Plan so we ignore the original skipped Phase and proceed with default alignment (we only move the clock 5 days so we are still in TRIAL)
    Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
    Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.TRIAL);
    final TimedPhase nextPhase = planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
    Assert.assertEquals(nextPhase.getStartPhase(), alignStartDate.plusDays(30));
    Assert.assertEquals(nextPhase.getPhase().getPhaseType(), PhaseType.FIXEDTERM);
}
Also used : DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)63 DateTime (org.joda.time.DateTime)38 Test (org.testng.annotations.Test)30 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)18 Plan (org.killbill.billing.catalog.api.Plan)17 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)17 ArrayList (java.util.ArrayList)16 LocalDate (org.joda.time.LocalDate)13 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)13 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)13 BigDecimal (java.math.BigDecimal)12 Account (org.killbill.billing.account.api.Account)12 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)12 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)12 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)11 LinkedList (java.util.LinkedList)10 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)10 UUID (java.util.UUID)9 Invoice (org.killbill.billing.invoice.api.Invoice)9 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)8