use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.
the class TestPlanAligner method testChangeWithTargetPhaseType1.
//
// Scenario : change Plan with START_OF_SUBSCRIPTION to a new Plan that has {TRIAL, DISCOUNT, EVERGREEN} and specifying a target PhaseType = DISCOUNT
//
@Test(groups = "fast")
public void testChangeWithTargetPhaseType1() 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, null, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
Assert.assertEquals(phases.length, 2);
Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.TRIAL);
Assert.assertEquals(phases[0].getStartPhase(), now);
Assert.assertEquals(phases[1].getPhase().getPhaseType(), PhaseType.EVERGREEN);
Assert.assertEquals(phases[1].getStartPhase(), now.plusDays(30));
final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.TRIAL);
final String newProductName = "pistol-annual-gunclub-discount";
final Plan newPlan = currentVersion.findPlan(newProductName);
final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusDays(5);
final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.DISCOUNT, catalog, internalCallContext);
// We end up straight on DISCOUNT but because we are using START_OF_SUBSCRIPTION alignment, such Phase starts with beginning of subscription
Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.DISCOUNT);
final TimedPhase nextPhase = planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.DISCOUNT, catalog, internalCallContext);
Assert.assertEquals(nextPhase.getStartPhase(), alignStartDate.plusMonths(6));
Assert.assertEquals(nextPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}
use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.
the class TestPlanAligner method testCreationSubscriptionAlignment.
@Test(groups = "fast")
public void testCreationSubscriptionAlignment() throws Exception {
final String productName = "laser-scope-monthly";
final PhaseType initialPhase = PhaseType.DISCOUNT;
final DateTime now = clock.getUTCNow();
final DateTime bundleStartDate = now.minusHours(10);
final DateTime alignStartDate = bundleStartDate.plusHours(5);
final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, initialPhase);
// Look now, after the bundle and the subscription started
final DateTime effectiveDate = clock.getUTCNow();
final TimedPhase[] phases = getTimedPhasesOnCreate(productName, initialPhase, defaultSubscriptionBase, effectiveDate);
// Laser-Scope is START_OF_SUBSCRIPTION aligned on creation
Assert.assertEquals(phases[0].getStartPhase(), defaultSubscriptionBase.getStartDate());
Assert.assertEquals(phases[1].getStartPhase(), defaultSubscriptionBase.getStartDate().plusMonths(1));
// Verify the next phase via the other API
final TimedPhase nextTimePhase = planAligner.getNextTimedPhase(defaultSubscriptionBase, effectiveDate, catalog, internalCallContext);
Assert.assertEquals(nextTimePhase.getStartPhase(), defaultSubscriptionBase.getStartDate().plusMonths(1));
// Now look at the past, before the subscription started
final DateTime effectiveDateInThePast = defaultSubscriptionBase.getStartDate().minusHours(10);
final TimedPhase[] phasesInThePast = getTimedPhasesOnCreate(productName, initialPhase, defaultSubscriptionBase, effectiveDateInThePast);
Assert.assertNull(phasesInThePast[0]);
Assert.assertEquals(phasesInThePast[1].getStartPhase(), defaultSubscriptionBase.getStartDate());
// Try a change plan (simulate END_OF_TERM policy)
final String newProductName = "telescopic-scope-monthly";
final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(1);
changeSubscription(effectiveChangeDate, defaultSubscriptionBase, productName, newProductName, initialPhase);
// All non rescue plans are START_OF_SUBSCRIPTION aligned on change. Since we're END_OF_TERM here, we'll
// never see the discount phase of telescopic-scope-monthly and jump right into evergreen.
// But in this test, since we didn't create the future change event from discount to evergreen (see changeSubscription,
// the subscription has only two transitions), we'll see null
final TimedPhase newPhase = getNextTimedPhaseOnChange(defaultSubscriptionBase, newProductName, effectiveChangeDate);
Assert.assertNull(newPhase);
}
use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.
the class TestPlanAligner method createSubscription.
private DefaultSubscriptionBase createSubscription(final DateTime bundleStartDate, final DateTime alignStartDate, final String productName, final PhaseType phaseType) throws CatalogApiException {
final SubscriptionBuilder builder = new SubscriptionBuilder();
builder.setBundleStartDate(bundleStartDate);
// Make sure to set the dates apart
builder.setAlignStartDate(new DateTime(alignStartDate));
// Create the transitions
final DefaultSubscriptionBase defaultSubscriptionBase = new DefaultSubscriptionBase(builder, null, clock);
final SubscriptionBaseEvent event = createSubscriptionEvent(builder.getAlignStartDate(), productName, phaseType, ApiEventType.CREATE);
final List<SubscriptionBaseEvent> events = new ArrayList<SubscriptionBaseEvent>();
events.add(event);
defaultSubscriptionBase.rebuildTransitions(events, catalog);
Assert.assertEquals(defaultSubscriptionBase.getAllTransitions().size(), 1);
Assert.assertNull(defaultSubscriptionBase.getAllTransitions().get(0).getPreviousPhase());
Assert.assertNotNull(defaultSubscriptionBase.getAllTransitions().get(0).getNextPhase());
return defaultSubscriptionBase;
}
use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.
the class TestPlanAligner method testCreateWithTargetPhaseType1.
//
// Scenario : change Plan with START_OF_SUBSCRIPTION after skipping TRIAL on Create to a new Plan that only has EVERGREEN
//
@Test(groups = "fast")
public void testCreateWithTargetPhaseType1() 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-notrial";
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);
}
use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.
the class TestPlanAligner method testCreationBundleAlignment.
@Test(groups = "fast")
public void testCreationBundleAlignment() throws Exception {
final String productName = "pistol-monthly";
final PhaseType initialPhase = PhaseType.TRIAL;
final DateTime now = clock.getUTCNow();
final DateTime bundleStartDate = now.minusHours(10);
final DateTime alignStartDate = bundleStartDate.plusHours(5);
final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, initialPhase);
// Make the creation effective now, after the bundle and the subscription started
final DateTime effectiveDate = clock.getUTCNow();
final TimedPhase[] phases = getTimedPhasesOnCreate(productName, initialPhase, defaultSubscriptionBase, effectiveDate);
// All plans but Laser-Scope are START_OF_BUNDLE aligned on creation
Assert.assertEquals(phases[0].getStartPhase(), defaultSubscriptionBase.getBundleStartDate());
Assert.assertEquals(phases[1].getStartPhase(), defaultSubscriptionBase.getBundleStartDate().plusDays(30));
// Verify the next phase via the other API
final TimedPhase nextTimePhase = planAligner.getNextTimedPhase(defaultSubscriptionBase, effectiveDate, catalog, internalCallContext);
Assert.assertEquals(nextTimePhase.getStartPhase(), defaultSubscriptionBase.getBundleStartDate().plusDays(30));
// Now look at the past, before the bundle started
final DateTime effectiveDateInThePast = defaultSubscriptionBase.getBundleStartDate().minusHours(10);
final TimedPhase[] phasesInThePast = getTimedPhasesOnCreate(productName, initialPhase, defaultSubscriptionBase, effectiveDateInThePast);
Assert.assertNull(phasesInThePast[0]);
Assert.assertEquals(phasesInThePast[1].getStartPhase(), defaultSubscriptionBase.getBundleStartDate());
// Try a change plan now (simulate an IMMEDIATE policy)
final String newProductName = "shotgun-monthly";
final DateTime effectiveChangeDate = clock.getUTCNow();
changeSubscription(effectiveChangeDate, defaultSubscriptionBase, productName, newProductName, initialPhase);
// All non rescue plans are START_OF_SUBSCRIPTION aligned on change
final TimedPhase newPhase = getNextTimedPhaseOnChange(defaultSubscriptionBase, newProductName, effectiveChangeDate);
Assert.assertEquals(newPhase.getStartPhase(), defaultSubscriptionBase.getStartDate().plusDays(30), String.format("Start phase: %s, but bundle start date: %s and subscription start date: %s", newPhase.getStartPhase(), defaultSubscriptionBase.getBundleStartDate(), defaultSubscriptionBase.getStartDate()));
}
Aggregations