@Override public Response doOperation(final CallContext ctx) throws EntitlementApiException, SubscriptionApiException { final Entitlement current = entitlementApi.getEntitlementForId(subscriptionId, ctx); final LocalDate inputLocalDate = toLocalDate(requestedDate); final Entitlement newEntitlement; if (billingPolicy == null && entitlementPolicy == null) { newEntitlement = current.cancelEntitlementWithDate(inputLocalDate, useRequestedDateForBilling, pluginProperties, ctx); } else if (billingPolicy == null && entitlementPolicy != null) { newEntitlement = current.cancelEntitlementWithPolicy(entitlementPolicy, pluginProperties, ctx); } else if (billingPolicy != null && entitlementPolicy == null) { newEntitlement = current.cancelEntitlementWithDateOverrideBillingPolicy(inputLocalDate, billingPolicy, pluginProperties, ctx); } else { newEntitlement = current.cancelEntitlementWithPolicyOverrideBillingPolicy(entitlementPolicy, billingPolicy, pluginProperties, ctx); } final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(newEntitlement.getId(), ctx); final LocalDate nowInAccountTimeZone = new LocalDate(callContext.getCreatedDate(), subscription.getBillingEndDate().getChronology().getZone()); isImmediateOp = subscription.getBillingEndDate() != null && !subscription.getBillingEndDate().isAfter(nowInAccountTimeZone); return Response.status(Status.NO_CONTENT).build(); }
@Test(groups = "slow") public void testCancelWithEntitlementPolicyEOTAndNOCTD() throws AccountApiException, EntitlementApiException, SubscriptionApiException { final LocalDate initialDate = new LocalDate(2013, 8, 7); clock.setDay(initialDate); final Account account = createAccount(getAccountData(7)); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null); // Create entitlement and check each field testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK); final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext); testListener.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK); final Entitlement cancelledEntitlement = entitlement.cancelEntitlementWithPolicy(EntitlementActionPolicy.END_OF_TERM, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); assertEquals(cancelledEntitlement.getState(), EntitlementState.CANCELLED); assertEquals(cancelledEntitlement.getEffectiveEndDate(), initialDate); // Entitlement started in trial on 2013-08-07, which is when we want the billing cancellation to occur final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(entitlement.getBaseEntitlementId(), callContext); assertEquals(subscription.getBillingEndDate(), new LocalDate(2013, 8, 7)); }
final Entitlement entitlement3 = entitlement2.cancelEntitlementWithPolicy(EntitlementActionPolicy.END_OF_TERM, ImmutableList.<PluginProperty>of(), callContext); assertEquals(entitlement3.getState(), EntitlementState.ACTIVE); assertEquals(entitlement3.getEffectiveEndDate(), new LocalDate(ctd));
@Test(groups = "slow") public void testCancelWithEntitlementPolicyEOTNoCTDAndImmediateChange() throws AccountApiException, EntitlementApiException, SubscriptionApiException, InterruptedException { final LocalDate initialDate = new LocalDate(2013, 8, 7); clock.setDay(initialDate); final Account account = createAccount(getAccountData(7)); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null); // Create entitlement and check each field testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK); final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext); // Immediate change during trial testListener.pushExpectedEvent(NextEvent.CREATE); final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Assault-Rifle", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); entitlement.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); // Verify the change is immediate final Entitlement entitlement2 = entitlementApi.getEntitlementForId(entitlement.getId(), callContext); assertEquals(entitlement2.getLastActivePlan().getProduct().getName(), "Assault-Rifle"); testListener.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK); final Entitlement cancelledEntitlement = entitlement.cancelEntitlementWithPolicy(EntitlementActionPolicy.END_OF_TERM, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); assertEquals(cancelledEntitlement.getState(), EntitlementState.CANCELLED); assertEquals(cancelledEntitlement.getEffectiveEndDate(), initialDate); // Entitlement started in trial on 2013-08-07, which is when we want the billing cancellation date to occur final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(entitlement.getBaseEntitlementId(), callContext); assertEquals(subscription.getBillingEndDate(), new LocalDate(2013, 8, 7)); }
final Entitlement entitlement3 = entitlement2.cancelEntitlementWithPolicy(EntitlementActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); assertEquals(entitlement3.getState(), EntitlementState.CANCELLED);