@Override public String getName() { if (plan == null) { return null; } else { return plan.getName() + "-" + cohort; } }
private void checkChangePlan(final DefaultSubscriptionBase subscription, final String expProduct, final ProductCategory expCategory, final BillingPeriod expBillingPeriod, final PhaseType expPhase) { final Plan currentPlan = subscription.getCurrentPlan(); assertNotNull(currentPlan); assertEquals(currentPlan.getProduct().getName(), expProduct); assertEquals(currentPlan.getProduct().getCategory(), expCategory); assertEquals(currentPlan.getBillingPeriod(), expBillingPeriod); final PlanPhase currentPhase = subscription.getCurrentPhase(); assertNotNull(currentPhase); assertEquals(currentPhase.getPhaseType(), expPhase); }
for (final Plan plan : plans) { final Product product = plan.getProduct(); ProductJson productJson = productMap.get(product.getName()); if (productJson == null) { for (final PlanPhase phase : plan.getAllPhases()) { final List<PriceJson> prices = new LinkedList<PriceJson>(); if (phase.getRecurringPrice() != null) { final PlanJson planJson = new PlanJson(plan.getName(), phases); productJson.getPlans().add(planJson);
public PlanDetailJson(final Listing listing) { final Plan plan = listing.getPlan(); if (plan == null) { this.productName = null; this.planName = null; this.billingPeriod = null; this.finalPhasePrice = ImmutableList.<PriceJson>of(); } else { this.productName = plan.getProduct() == null ? null : plan.getProduct().getName(); this.planName = plan.getName(); this.billingPeriod = plan.getBillingPeriod(); if (plan.getFinalPhase() == null || plan.getFinalPhase().getRecurringPrice() == null || plan.getFinalPhase().getRecurringPrice().getPrices() == null) { this.finalPhasePrice = ImmutableList.<PriceJson>of(); } else { this.finalPhasePrice = Lists.transform(ImmutableList.<Price>copyOf(plan.getFinalPhase().getRecurringPrice().getPrices()), new Function<Price, PriceJson>() { @Override public PriceJson apply(final Price price) { try { return new PriceJson(price); } catch (CurrencyValueNull e) { return new PriceJson(price.getCurrency().toString(), BigDecimal.ZERO); } } }); } } this.priceListName = listing.getPriceList() == null ? null : listing.getPriceList().getName(); }
@Override public List<Listing> getAvailableBasePlanListings() { final List<Listing> availBasePlans = new ArrayList<Listing>(); for (Plan plan : getCurrentPlans()) { if (plan.getProduct().getCategory().equals(ProductCategory.BASE)) { for (PriceList priceList : getPriceLists().getAllPriceLists()) { for (Plan priceListPlan : priceList.getPlans()) { if (priceListPlan.getName().equals(plan.getName()) && priceListPlan.getProduct().getName().equals(plan.getProduct().getName())) { availBasePlans.add(new DefaultListing(priceListPlan, priceList)); } } } } } return availBasePlans; } }
private static ProductCategory getTypeFromSubscription(final Plan plan) { if (plan != null && plan.getProduct() != null) { final Product product = plan.getProduct(); if (product.getCatalogName() != null && product.getCategory() != null) { return product.getCategory(); } } return null; }
final Plan newSubPlan3 = vc.findPlan("pistol-monthly", dt3, dt3); Assert.assertEquals(newSubPlan1.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("1.0")); Assert.assertEquals(newSubPlan2.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("2.0")); Assert.assertEquals(newSubPlan214.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("2.0")); Assert.assertEquals(newSubPlan3.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("3.0")); final Plan exSubPlan3 = vc.findPlan("pistol-monthly", dt3, dt1); Assert.assertEquals(exSubPlan2.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("1.0")); Assert.assertEquals(exSubPlan214.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("2.0")); Assert.assertEquals(exSubPlan3.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("2.0"));
@Test(groups = "fast") public void testChangeSubscriptionWithPolicy() throws Exception { final SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME); try { subscription.changePlanWithPolicy("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, BillingActionPolicy.ILLEGAL, callContext); Assert.fail(); } catch (SubscriptionBaseError error) { assertTrue(true); assertEquals(subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext).getCurrentPlan().getBillingPeriod(), BillingPeriod.ANNUAL); } // Assume the call takes less than a second assertEquals(DefaultClock.truncateMs(subscription.changePlanWithPolicy("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, BillingActionPolicy.IMMEDIATE, callContext)), DefaultClock.truncateMs(clock.getUTCNow())); assertEquals(subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext).getCurrentPlan().getBillingPeriod(), BillingPeriod.MONTHLY); }
private void verifyBCDCalculation(final DateTimeZone accountTimeZone, final DateTime startDateUTC, final int bcdLocal) throws AccountApiException, CatalogApiException { final BillCycleDayCalculator billCycleDayCalculator = new BillCycleDayCalculator(Mockito.mock(CatalogService.class), Mockito.mock(SubscriptionBaseInternalApi.class)); final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class); Mockito.when(subscription.getStartDate()).thenReturn(startDateUTC); final Plan plan = Mockito.mock(Plan.class); Mockito.when(plan.dateOfFirstRecurringNonZeroCharge(startDateUTC, null)).thenReturn(startDateUTC); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(accountTimeZone); final Integer bcd = billCycleDayCalculator.calculateBcdFromSubscription(subscription, plan, account, Mockito.mock(Catalog.class), internalCallContext); Assert.assertEquals(bcd, (Integer) bcdLocal); } }
@Test(groups = "fast") public void testFromListing() throws Exception { final Product product = Mockito.mock(Product.class); Mockito.when(product.getName()).thenReturn(UUID.randomUUID().toString()); final InternationalPrice price = Mockito.mock(InternationalPrice.class); final PlanPhase planPhase = Mockito.mock(PlanPhase.class); Mockito.when(planPhase.getRecurringPrice()).thenReturn(price); final Plan plan = Mockito.mock(Plan.class); Mockito.when(plan.getProduct()).thenReturn(product); Mockito.when(plan.getName()).thenReturn(UUID.randomUUID().toString()); Mockito.when(plan.getBillingPeriod()).thenReturn(BillingPeriod.QUARTERLY); Mockito.when(plan.getFinalPhase()).thenReturn(planPhase); final PriceList priceList = Mockito.mock(PriceList.class); Mockito.when(priceList.getName()).thenReturn(UUID.randomUUID().toString()); final Listing listing = Mockito.mock(Listing.class); Mockito.when(listing.getPlan()).thenReturn(plan); Mockito.when(listing.getPriceList()).thenReturn(priceList); final PlanDetailJson planDetailJason = new PlanDetailJson(listing); Assert.assertEquals(planDetailJason.getProductName(), plan.getProduct().getName()); Assert.assertEquals(planDetailJason.getPlanName(), plan.getName()); Assert.assertEquals(planDetailJason.getBillingPeriod(), plan.getBillingPeriod()); Assert.assertEquals(planDetailJason.getPriceListName(), priceList.getName()); Assert.assertEquals(planDetailJason.getFinalPhasePrice().size(), 0); } }
@Override public boolean apply(final SubscriptionBase subscription) { return ProductCategory.ADD_ON.equals(subscription.getCategory()) && // Check the entitlement for that add-on hasn't been cancelled yet getEntitlementCancellationEvent(subscription.getId()) == null && ( // Base subscription cancelled baseTransitionTriggerNextProduct == null || ( // Change plan - check which add-ons to cancel includedAddonsForProduct.contains(subscription.getLastActivePlan().getProduct().getName()) || !availableAddonsForProduct.contains(subscription.getLastActivePlan().getProduct().getName()) ) ); } });
assertEquals(subscriptions.size(), 2); final SubscriptionBase baseSubscription = (subscriptions.get(0).getCurrentPlan().getProduct().getCategory() == ProductCategory.BASE) ? subscriptions.get(0) : subscriptions.get(1); assertTrue(baseSubscription.getStartDate().compareTo(initalBPStart) == 0); assertEquals(baseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN); assertEquals(baseSubscription.getState(), EntitlementState.ACTIVE); assertEquals(baseSubscription.getCurrentPlan().getName(), "shotgun-annual"); assertEquals(baseSubscription.getChargedThroughDate(), initalBPStart.plusYears(1)); final SubscriptionBase aoSubscription = (subscriptions.get(0).getCurrentPlan().getProduct().getCategory() == ProductCategory.ADD_ON) ? subscriptions.get(0) : subscriptions.get(1); assertEquals(aoSubscription.getCurrentPhase().getPhaseType(), PhaseType.DISCOUNT); assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE); assertEquals(aoSubscription.getCurrentPlan().getName(), "telescopic-scope-monthly"); assertEquals(aoSubscription.getChargedThroughDate(), initalAddonStart.plusMonths(1));
@Test(groups = "fast") public void testBillingEventsAutoInvoicingOffBundle() throws CatalogApiException, AccountApiException, TagApiException { final Plan nextPlan = catalog.findPlan("PickupTrialEvergreen10USD", clock.getUTCNow()); final PlanPhase nextPhase = nextPlan.getAllPhases()[1]; createSubscriptionCreationEvent(nextPlan, nextPhase); final Account account = createAccount(32); tagInternalApi.addTag(bunId, ObjectType.BUNDLE, ControlTagType.AUTO_INVOICING_OFF.getId(), internalCallContext); final BillingEventSet events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), internalCallContext); assertEquals(events.getSubscriptionIdsWithAutoInvoiceOff().size(), 1); assertEquals(events.getSubscriptionIdsWithAutoInvoiceOff().get(0), subId); assertEquals(events.size(), 0); }
@Override public Response doOperation(final CallContext ctx) throws EntitlementApiException, InterruptedException, TimeoutException, AccountApiException { final UUID uuid = UUID.fromString(subscriptionId); final Entitlement current = entitlementApi.getEntitlementForId(uuid, callContext); final LocalDate inputLocalDate = toLocalDate(current.getAccountId(), requestedDate, callContext); final Entitlement newEntitlement; if (requestedDate == null && policyString == null) { newEntitlement = current.changePlan(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), ctx); } else if (policyString == null) { newEntitlement = current.changePlanWithDate(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), inputLocalDate, ctx); } else { final BillingActionPolicy policy = BillingActionPolicy.valueOf(policyString.toUpperCase()); newEntitlement = current.changePlanOverrideBillingPolicy(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), inputLocalDate, policy, ctx); } isImmediateOp = newEntitlement.getLastActiveProduct().getName().equals(entitlement.getProductName()) && newEntitlement.getLastActivePlan().getBillingPeriod() == BillingPeriod.valueOf(entitlement.getBillingPeriod()) && newEntitlement.getLastActivePriceList().getName().equals(entitlement.getPriceList()); return Response.status(Status.OK).build(); }
@Test(groups = "fast") public void testCalculateBCDForAOWithBPCancelledBundleAligned() throws Exception { final DateTimeZone accountTimeZone = DateTimeZone.UTC; final DateTime bpStartDateUTC = new DateTime(2012, 7, 16, 21, 0, 0, DateTimeZone.UTC); final int expectedBCDUTC = 16; // Create a Bundle associated with a subscription final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class); final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class); Mockito.when(subscription.getStartDate()).thenReturn(bpStartDateUTC); // subscription.getCurrentPlan() will return null as expected (cancelled BP) Mockito.when(subscriptionInternalApi.getBaseSubscription(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscription); // Create a the base plan associated with that subscription final Plan plan = Mockito.mock(Plan.class); Mockito.when(plan.dateOfFirstRecurringNonZeroCharge(bpStartDateUTC, null)).thenReturn(bpStartDateUTC); final Catalog catalog = Mockito.mock(Catalog.class); Mockito.when(catalog.findPlan(Mockito.anyString(), Mockito.<DateTime>any(), Mockito.<DateTime>any())).thenReturn(plan); Mockito.when(subscription.getLastActivePlan()).thenReturn(plan); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(accountTimeZone); final Integer billCycleDayLocal = billCycleDayCalculator.calculateBcdForAlignment(BillingAlignment.BUNDLE, bundle, subscription, account, catalog, null, internalCallContext); Assert.assertEquals(billCycleDayLocal, (Integer) expectedBCDUTC); }
private SubscriptionEvent toSubscriptionEvent(final SubscriptionBaseTransition in, final SubscriptionEventType eventType, final DateTimeZone accountTimeZone) { return new DefaultSubscriptionEvent(in.getId(), in.getSubscriptionId(), in.getEffectiveTransitionTime(), in.getRequestedTransitionTime(), eventType, false, false, getServiceName(eventType), eventType.toString(), (in.getPreviousPlan() != null ? in.getPreviousPlan().getProduct() : null), in.getPreviousPlan(), in.getPreviousPhase(), in.getPreviousPriceList(), (in.getPreviousPlan() != null ? in.getPreviousPlan().getBillingPeriod() : null), (in.getNextPlan() != null ? in.getNextPlan().getProduct() : null), in.getNextPlan(), in.getNextPhase(), in.getNextPriceList(), (in.getNextPlan() != null ? in.getNextPlan().getBillingPeriod() : null), in.getCreatedDate(), accountTimeZone); }
@Override public String getName() { return phaseName(plan.getName(), this.getPhaseType()); }
@Override public boolean compliesWithLimits(String unit, double value) { Limit l = findLimit(unit); if (l == null) { return getPlan().getProduct().compliesWithLimits(unit, value); } return l.compliesWith(value); }
@Test(groups = "fast") public void testBillingEventsWithBlock() throws CatalogApiException, AccountApiException { final Plan nextPlan = catalog.findPlan("PickupTrialEvergreen10USD", clock.getUTCNow()); final PlanPhase nextPhase = nextPlan.getAllPhases()[1]; final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase); final Account account = createAccount(32); blockingStateDao.setBlockingState(new DefaultBlockingState(bunId, BlockingStateType.SUBSCRIPTION_BUNDLE, DISABLED_BUNDLE, "test", true, true, true, now.plusDays(1)), clock, internalCallContext); blockingStateDao.setBlockingState(new DefaultBlockingState(bunId, BlockingStateType.SUBSCRIPTION_BUNDLE, CLEAR_BUNDLE, "test", false, false, false, now.plusDays(2)), clock, internalCallContext); final SortedSet<BillingEvent> events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), internalCallContext); Assert.assertEquals(events.size(), 3); final Iterator<BillingEvent> it = events.iterator(); checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now, nextPhase, SubscriptionBaseTransitionType.CREATE.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice()); checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now.plusDays(1), nextPhase, SubscriptionBaseTransitionType.START_BILLING_DISABLED.toString(), null, null); checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now.plusDays(2), nextPhase, SubscriptionBaseTransitionType.END_BILLING_DISABLED.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice()); }
this.productName = subscription.getLastActiveProduct().getName(); this.productCategory = subscription.getLastActiveProductCategory().name(); this.billingPeriod = subscription.getLastActivePlan().getBillingPeriod().toString(); this.priceList = subscription.getLastActivePriceList().getName(); this.cancelledDate = subscription.getEffectiveEndDate();