Search in sources :

Example 1 with EntitlementAOStatusDryRun

use of org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun in project killbill by killbill.

the class TestUserApiAddOn method testChangeBPWithAddonNonAvailable.

@Test(groups = "slow")
public void testChangeBPWithAddonNonAvailable() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final String aoProduct = "Telescopic-Scope";
    final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
    final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE AO
    DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    // MOVE CLOCK AFTER TRIAL + AO DISCOUNT
    Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(2));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD TO CANCEL IN FUTURE
    final DateTime now = clock.getUTCNow();
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(now, ctd);
    setChargedThroughDate(baseSubscription.getId(), newChargedThroughDate, internalCallContext);
    baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    // CHANGE IMMEDIATELY WITH TO BP WITH NON AVAILABLE ADDON
    final String newBaseProduct = "Pistol";
    final BillingPeriod newBaseTerm = BillingPeriod.MONTHLY;
    final String newBasePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    final List<EntitlementAOStatusDryRun> aoStatus = subscriptionInternalApi.getDryRunChangePlanStatus(baseSubscription.getId(), newBaseProduct, now, internalCallContext);
    assertEquals(aoStatus.size(), 1);
    assertEquals(aoStatus.get(0).getId(), aoSubscription.getId());
    assertEquals(aoStatus.get(0).getProductName(), aoProduct);
    assertEquals(aoStatus.get(0).getBillingPeriod(), aoTerm);
    assertEquals(aoStatus.get(0).getPhaseType(), aoSubscription.getCurrentPhase().getPhaseType());
    assertEquals(aoStatus.get(0).getPriceList(), aoSubscription.getCurrentPriceList().getName());
    assertEquals(aoStatus.get(0).getReason(), DryRunChangeReason.AO_NOT_AVAILABLE_IN_NEW_PLAN);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(newBaseProduct, newBaseTerm, newBasePriceList);
    baseSubscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext);
    // REFETCH AO SUBSCRIPTION AND CHECK THIS IS ACTIVE
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);
    assertTrue(aoSubscription.isFutureCancelled());
    // MOVE AFTER CHANGE
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // REFETCH AO SUBSCRIPTION AND CHECK THIS CANCELLED
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.CANCELLED);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) EntitlementAOStatusDryRun(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun) Duration(org.killbill.billing.catalog.api.Duration) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 2 with EntitlementAOStatusDryRun

use of org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun in project killbill by killbill.

the class DefaultSubscriptionInternalApi method getDryRunChangePlanStatus.

@Override
public List<EntitlementAOStatusDryRun> getDryRunChangePlanStatus(final UUID subscriptionId, @Nullable final String baseProductName, final DateTime requestedDate, final InternalTenantContext context) throws SubscriptionBaseApiException {
    try {
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(context);
        final SubscriptionBase subscription = dao.getSubscriptionFromId(subscriptionId, catalog, context);
        if (subscription == null) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_INVALID_SUBSCRIPTION_ID, subscriptionId);
        }
        if (subscription.getCategory() != ProductCategory.BASE) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_DRY_RUN_NOT_BP);
        }
        final List<EntitlementAOStatusDryRun> result = new LinkedList<EntitlementAOStatusDryRun>();
        final List<DefaultSubscriptionBase> bundleSubscriptions = dao.getSubscriptions(subscription.getBundleId(), ImmutableList.<SubscriptionBaseEvent>of(), catalog, context);
        for (final SubscriptionBase cur : bundleSubscriptions) {
            if (cur.getId().equals(subscriptionId)) {
                continue;
            }
            // If ADDON is cancelled, skip
            if (cur.getState() == EntitlementState.CANCELLED) {
                continue;
            }
            final StaticCatalog catalogVersion = catalog.versionForDate(requestedDate);
            final Product baseProduct = baseProductName != null ? catalogVersion.findProduct(baseProductName) : null;
            final DryRunChangeReason reason;
            // If baseProductName is null, it's a cancellation dry-run. In this case, return all addons, so they are cancelled
            if (baseProduct != null && addonUtils.isAddonIncluded(baseProduct, cur.getCurrentPlan())) {
                reason = DryRunChangeReason.AO_INCLUDED_IN_NEW_PLAN;
            } else if (baseProduct != null && addonUtils.isAddonAvailable(baseProduct, cur.getCurrentPlan())) {
                reason = DryRunChangeReason.AO_AVAILABLE_IN_NEW_PLAN;
            } else {
                reason = DryRunChangeReason.AO_NOT_AVAILABLE_IN_NEW_PLAN;
            }
            final EntitlementAOStatusDryRun status = new DefaultSubscriptionStatusDryRun(cur.getId(), cur.getCurrentPlan().getProduct().getName(), cur.getCurrentPhase().getPhaseType(), cur.getCurrentPlan().getRecurringBillingPeriod(), cur.getCurrentPriceList().getName(), reason);
            result.add(status);
        }
        return result;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : Product(org.killbill.billing.catalog.api.Product) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) LinkedList(java.util.LinkedList) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) DryRunChangeReason(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun.DryRunChangeReason) EntitlementAOStatusDryRun(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) DefaultSubscriptionStatusDryRun(org.killbill.billing.subscription.api.user.DefaultSubscriptionStatusDryRun) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 3 with EntitlementAOStatusDryRun

use of org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun in project killbill by killbill.

the class TestUserApiAddOn method testChangeBPWithAddonIncluded.

@Test(groups = "slow")
public void testChangeBPWithAddonIncluded() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final String aoProduct = "Telescopic-Scope";
    final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
    final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    // MOVE CLOCK AFTER TRIAL + AO DISCOUNT
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(2));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    // SET CTD TO CHANGE IN FUTURE
    final DateTime now = clock.getUTCNow();
    final Duration ctd = testUtil.getDurationMonth(1);
    final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(now, ctd);
    setChargedThroughDate(baseSubscription.getId(), newChargedThroughDate, internalCallContext);
    baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    // CHANGE IMMEDIATELY WITH TO BP WITH NON INCLUDED ADDON
    final String newBaseProduct = "Assault-Rifle";
    final BillingPeriod newBaseTerm = BillingPeriod.MONTHLY;
    final String newBasePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    final List<EntitlementAOStatusDryRun> aoStatus = subscriptionInternalApi.getDryRunChangePlanStatus(baseSubscription.getId(), newBaseProduct, now, internalCallContext);
    assertEquals(aoStatus.size(), 1);
    assertEquals(aoStatus.get(0).getId(), aoSubscription.getId());
    assertEquals(aoStatus.get(0).getProductName(), aoProduct);
    assertEquals(aoStatus.get(0).getBillingPeriod(), aoTerm);
    assertEquals(aoStatus.get(0).getPhaseType(), aoSubscription.getCurrentPhase().getPhaseType());
    assertEquals(aoStatus.get(0).getPriceList(), aoSubscription.getCurrentPriceList().getName());
    assertEquals(aoStatus.get(0).getReason(), DryRunChangeReason.AO_INCLUDED_IN_NEW_PLAN);
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(newBaseProduct, newBaseTerm, newBasePriceList, null);
    baseSubscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext);
    assertListenerStatus();
    // REFETCH AO SUBSCRIPTION AND CHECK THIS CANCELLED
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.CANCELLED);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) EntitlementAOStatusDryRun(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun) Duration(org.killbill.billing.catalog.api.Duration) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Aggregations

EntitlementAOStatusDryRun (org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun)3 DateTime (org.joda.time.DateTime)2 Interval (org.joda.time.Interval)2 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)2 Duration (org.killbill.billing.catalog.api.Duration)2 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)2 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)2 Test (org.testng.annotations.Test)2 LinkedList (java.util.LinkedList)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 Product (org.killbill.billing.catalog.api.Product)1 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)1 DryRunChangeReason (org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun.DryRunChangeReason)1 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)1 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)1 DefaultSubscriptionStatusDryRun (org.killbill.billing.subscription.api.user.DefaultSubscriptionStatusDryRun)1 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)1 SubscriptionCatalog (org.killbill.billing.subscription.catalog.SubscriptionCatalog)1