@Override public Entitlement apply(@Nullable final Void dontcare) { try { // Need to fetch again to get latest CTD updated from the system Entitlement refreshedEntitlement = entitlementApi.getEntitlementForId(entitlement.getId(), callContext); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(productName, billingPeriod, priceList); if (billingPolicy == null) { refreshedEntitlement = refreshedEntitlement.changePlan(new DefaultEntitlementSpecifier(spec), ImmutableList.<PluginProperty>of(), callContext); } else { refreshedEntitlement = refreshedEntitlement.changePlanOverrideBillingPolicy(new DefaultEntitlementSpecifier(spec), null, billingPolicy, ImmutableList.<PluginProperty>of(), callContext); } return refreshedEntitlement; } catch (final EntitlementApiException e) { fail(e.getMessage()); return null; } } }, events);
@Test(groups = "slow") public void testBasic() throws Exception { // Create a per-tenant catalog with one plan final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("foo-monthly", "Foo", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); catalogUserApi.addSimplePlan(desc1, init, testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); assertEquals(catalog.getCurrentPlans().size(), 1); final Entitlement baseEntitlement = createEntitlement("foo-monthly", true); invoiceChecker.checkInvoice(account.getId(), 1, testCallContext, new ExpectedInvoiceItemCheck(new LocalDate(2016, 6, 1), new LocalDate(2016, 7, 1), InvoiceItemType.RECURRING, BigDecimal.TEN)); // Add another Plan in the catalog final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("superfoo-monthly", "SuperFoo", ProductCategory.BASE, account.getCurrency(), new BigDecimal("20.00"), BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); catalogUserApi.addSimplePlan(desc2, init, testCallContext); catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); assertEquals(catalog.getCurrentPlans().size(), 2); // Change Plan to the newly added Plan and verify correct default rules behavior (IMMEDIATE change) busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("SuperFoo", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); baseEntitlement.changePlan(new DefaultEntitlementSpecifier(spec), ImmutableList.<PluginProperty>of(), testCallContext); assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 2, testCallContext, new ExpectedInvoiceItemCheck(new LocalDate(2016, 6, 1), new LocalDate(2016, 7, 1), InvoiceItemType.RECURRING, new BigDecimal("20.00")), new ExpectedInvoiceItemCheck(new LocalDate(2016, 6, 1), new LocalDate(2016, 7, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-10.00"))); assertListenerStatus(); }
@Override public Response doOperation(final CallContext ctx) throws EntitlementApiException, AccountApiException { final Entitlement current = entitlementApi.getEntitlementForId(subscriptionId, callContext); final LocalDate inputLocalDate = toLocalDate(requestedDate); final Entitlement newEntitlement; final Account account = accountUserApi.getAccountById(current.getAccountId(), callContext); final PhaseType phaseType = entitlement.getPhaseType(); final PlanPhaseSpecifier planSpec = entitlement.getPlanName() != null ? new PlanPhaseSpecifier(entitlement.getPlanName(), phaseType) : new PlanPhaseSpecifier(entitlement.getProductName(), entitlement.getBillingPeriod(), entitlement.getPriceList(), phaseType); final List<PlanPhasePriceOverride> overrides = buildPlanPhasePriceOverrides(entitlement.getPriceOverrides(), account.getCurrency(), planSpec); if (requestedDate == null && billingPolicy == null) { newEntitlement = current.changePlan(new DefaultEntitlementSpecifier(planSpec, null, overrides), pluginProperties, ctx); } else if (billingPolicy == null) { newEntitlement = current.changePlanWithDate(new DefaultEntitlementSpecifier(planSpec, null, overrides), inputLocalDate, pluginProperties, ctx); } else { newEntitlement = current.changePlanOverrideBillingPolicy(new DefaultEntitlementSpecifier(planSpec, null, overrides), null, billingPolicy, pluginProperties, ctx); } isImmediateOp = newEntitlement.getLastActiveProduct().getName().equals(entitlement.getProductName()) && newEntitlement.getLastActivePlan().getRecurringBillingPeriod() == entitlement.getBillingPeriod() && newEntitlement.getLastActivePriceList().getName().equals(entitlement.getPriceList()); return Response.status(Status.NO_CONTENT).build(); }
entitlement.changePlan(new DefaultEntitlementSpecifier(spec2), ImmutableList.<PluginProperty>of(), callContext); fail("Changing plan immediately prior the subscription is active is not allowed"); } catch (EntitlementApiException e) {
entitlement.changePlan(new DefaultEntitlementSpecifier(spec1), ImmutableList.<PluginProperty>of(), callContext); fail(); } catch (final EntitlementApiException e) {
@Test(groups = "slow") public void testWithPriceOverride() throws Exception { // Create a per-tenant catalog with one plan final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("bar-monthly", "Bar", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); catalogUserApi.addSimplePlan(desc1, init, testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); assertEquals(catalog.getCurrentPlans().size(), 1); final Plan plan = catalog.getCurrentPlans().iterator().next(); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("bar-monthly", null); final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>(); overrides.add(new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), account.getCurrency(), null, BigDecimal.ONE, null)); final Entitlement baseEntitlement = createEntitlement(spec, overrides, true); List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, testCallContext); assertEquals(invoices.size(), 1); assertEquals(invoices.get(0).getChargedAmount().compareTo(BigDecimal.ONE), 0); busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addMonths(1); assertListenerStatus(); invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, testCallContext); assertEquals(invoices.size(), 2); assertEquals(invoices.get(1).getChargedAmount().compareTo(BigDecimal.ONE), 0); // Change plan to original (non overridden plan) busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); baseEntitlement.changePlan( new DefaultEntitlementSpecifier(spec), ImmutableList.<PluginProperty>of(), testCallContext); assertListenerStatus(); invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, testCallContext); assertEquals(invoices.size(), 3); assertEquals(invoices.get(2).getChargedAmount().compareTo(new BigDecimal("9.00")), 0); // 10 (recurring) - 1 (repair) }
@Test(groups = "slow") public void testWithBCDOnOperations() throws Exception { final DateTime initialDate = new DateTime(2018, 6, 21, 0, 13, 42, 0, testTimeZone); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis()); final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(21)); assertNotNull(account); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial"); busHandler.pushExpectedEvents( NextEvent.CREATE, NextEvent.BLOCK, NextEvent.BCD_CHANGE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); // We will realign the BCD on the 15 as we create the subscription - ignoring the account setting on 21. final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec, 15, null), null, null, null, false, false, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2018, 6, 21), new LocalDate(2018, 7, 15), InvoiceItemType.RECURRING, new BigDecimal("15.96"))); // Verify next month busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addDays(24); // 2018-7-15 assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2018, 7, 15), new LocalDate(2018, 8, 15), InvoiceItemType.RECURRING, new BigDecimal("19.95"))); final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext); final PlanPhaseSpecifier spec2 = new PlanPhaseSpecifier("blowdart-monthly-notrial"); // Change plan EOT // We will now realign the BCD on the 21 as we change the plan for the subscription. entitlement.changePlan(new DefaultEntitlementSpecifier(spec2, 21, null), ImmutableList.<PluginProperty>of(), callContext); busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.BCD_CHANGE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addMonths(1); assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2018, 8, 15), new LocalDate(2018, 8, 21), InvoiceItemType.RECURRING, new BigDecimal("5.80"))); }
private void checkChangePlanWithOverdueState(final Entitlement entitlement, final boolean shouldFail, final boolean expectedPayment) { if (shouldFail) { try { final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Pistol", term, PriceListSet.DEFAULT_PRICELIST_NAME); entitlement.changePlan(new DefaultEntitlementSpecifier(spec), ImmutableList.<PluginProperty>of(), callContext); } catch (EntitlementApiException e) { assertTrue(e.getCause() instanceof BlockingApiException || e.getCode() == ErrorCode.SUB_CHANGE_NON_ACTIVE.getCode(), String.format("Cause is %s, message is %s", e.getCause(), e.getMessage())); } } else { // Upgrade - we don't expect a payment here due to the scenario (the account will have enough CBA) if (expectedPayment) { changeEntitlementAndCheckForCompletion(entitlement, "Assault-Rifle", BillingPeriod.MONTHLY, null, NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT); } else { changeEntitlementAndCheckForCompletion(entitlement, "Assault-Rifle", BillingPeriod.MONTHLY, null, NextEvent.CHANGE, NextEvent.INVOICE); } } } }
@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)); }
addOn3.changePlan(new DefaultEntitlementSpecifier(addOnSpecChangedPlan), ImmutableList.<PluginProperty>of(), callContext); } catch (final EntitlementApiException e) { assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_AO_MAX_PLAN_ALLOWED_BY_BUNDLE.getCode());