Search in sources :

Example 51 with PlanPhase

use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.

the class TestBillingApi method testBillingEventsNoBillingPeriod.

@Test(groups = "fast")
public void testBillingEventsNoBillingPeriod() throws CatalogApiException, AccountApiException, SubscriptionBaseApiException {
    final Plan nextPlan = catalog.findPlan("3-PickupTrialEvergreen10USD", clock.getUTCNow());
    // The trial has no billing period
    final PlanPhase nextPhase = nextPlan.getAllPhases()[0];
    final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
    final Account account = createAccount(10);
    final SortedSet<BillingEvent> events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, internalCallContext);
    checkFirstEvent(events, nextPlan, account.getBillCycleDayLocal(), subId, now, nextPhase, SubscriptionBaseTransitionType.CREATE.toString());
}
Also used : Account(org.killbill.billing.account.api.Account) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 52 with PlanPhase

use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.

the class TestBillingApi method testBillingEventsBundleAligned.

@Test(groups = "fast")
public void testBillingEventsBundleAligned() throws CatalogApiException, AccountApiException, SubscriptionBaseApiException {
    final Plan nextPlan = catalog.findPlan("7-Horn1USD", clock.getUTCNow());
    final PlanPhase nextPhase = nextPlan.getAllPhases()[0];
    final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
    final Account account = createAccount(1);
    catalog.setBillingAlignment(BillingAlignment.BUNDLE);
    ((MockSubscription) subscription).setPlan(catalog.findPlan("3-PickupTrialEvergreen10USD", now));
    final SortedSet<BillingEvent> events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, internalCallContext);
    // The expected BCD is when the subscription started
    checkFirstEvent(events, nextPlan, subscription.getStartDate().getDayOfMonth(), subId, now, nextPhase, SubscriptionBaseTransitionType.CREATE.toString());
}
Also used : Account(org.killbill.billing.account.api.Account) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockSubscription(org.killbill.billing.mock.MockSubscription) BillingEvent(org.killbill.billing.junction.BillingEvent) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 53 with PlanPhase

use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.

the class TestPlanDetailJson method testFromListing.

@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);
    Mockito.when(price.getPrices()).thenReturn(new DefaultPrice[0]);
    final PlanPhase planPhase = Mockito.mock(PlanPhase.class);
    final Recurring recurring = Mockito.mock(Recurring.class);
    Mockito.when(recurring.getRecurringPrice()).thenReturn(price);
    Mockito.when(planPhase.getRecurring()).thenReturn(recurring);
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getProduct()).thenReturn(product);
    Mockito.when(plan.getName()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(plan.getRecurringBillingPeriod()).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 planDetailJson = new PlanDetailJson(listing);
    Assert.assertEquals(planDetailJson.getProduct(), plan.getProduct().getName());
    Assert.assertEquals(planDetailJson.getPlan(), plan.getName());
    Assert.assertEquals(planDetailJson.getFinalPhaseBillingPeriod(), plan.getRecurringBillingPeriod());
    Assert.assertEquals(planDetailJson.getPriceList(), priceList.getName());
    Assert.assertEquals(planDetailJson.getFinalPhaseRecurringPrice().size(), 0);
}
Also used : Recurring(org.killbill.billing.catalog.api.Recurring) Listing(org.killbill.billing.catalog.api.Listing) Product(org.killbill.billing.catalog.api.Product) InternationalPrice(org.killbill.billing.catalog.api.InternationalPrice) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Plan(org.killbill.billing.catalog.api.Plan) PriceList(org.killbill.billing.catalog.api.PriceList) Test(org.testng.annotations.Test)

Example 54 with PlanPhase

use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.

the class EhCacheOverriddenPlanCache method createOverrides.

private PlanPhasePriceOverride[] createOverrides(final Plan defaultPlan, final List<CatalogOverridePhaseDefinitionModelDao> phaseDefs) {
    final PlanPhasePriceOverride[] result = new PlanPhasePriceOverride[defaultPlan.getAllPhases().length];
    for (int i = 0; i < defaultPlan.getAllPhases().length; i++) {
        final PlanPhase curPhase = defaultPlan.getAllPhases()[i];
        final CatalogOverridePhaseDefinitionModelDao overriddenPhase = Iterables.tryFind(phaseDefs, new Predicate<CatalogOverridePhaseDefinitionModelDao>() {

            @Override
            public boolean apply(final CatalogOverridePhaseDefinitionModelDao input) {
                return input.getParentPhaseName().equals(curPhase.getName());
            }
        }).orNull();
        result[i] = (overriddenPhase != null) ? new DefaultPlanPhasePriceOverride(curPhase.getName(), Currency.valueOf(overriddenPhase.getCurrency()), overriddenPhase.getFixedPrice(), overriddenPhase.getRecurringPrice()) : null;
    }
    return result;
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) CatalogOverridePhaseDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePhaseDefinitionModelDao) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) Predicate(com.google.common.base.Predicate)

Example 55 with PlanPhase

use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.

the class DefaultPriceOverride method getOrCreateOverriddenPlan.

@Override
public DefaultPlan getOrCreateOverriddenPlan(final StandaloneCatalog standaloneCatalog, final Plan parentPlan, final DateTime catalogEffectiveDate, final List<PlanPhasePriceOverride> overrides, @Nullable final InternalCallContext context) throws CatalogApiException {
    final PlanPhasePriceOverride[] resolvedOverride = new PlanPhasePriceOverride[parentPlan.getAllPhases().length];
    int index = 0;
    for (final PlanPhase curPhase : parentPlan.getAllPhases()) {
        final PlanPhasePriceOverride curOverride = Iterables.tryFind(overrides, new Predicate<PlanPhasePriceOverride>() {

            @Override
            public boolean apply(final PlanPhasePriceOverride input) {
                if (input.getPhaseName() != null) {
                    return input.getPhaseName().equals(curPhase.getName());
                }
                // If the phaseName was not passed, we infer by matching the phaseType. This obviously would not work in a case where
                // a plan is defined with multiple phases of the same type.
                final PlanPhaseSpecifier curPlanPhaseSpecifier = input.getPlanPhaseSpecifier();
                if (curPlanPhaseSpecifier.getPhaseType().equals(curPhase.getPhaseType())) {
                    return true;
                }
                return false;
            }
        }).orNull();
        resolvedOverride[index++] = curOverride != null ? new DefaultPlanPhasePriceOverride(curPhase.getName(), curOverride.getCurrency(), curOverride.getFixedPrice(), curOverride.getRecurringPrice()) : null;
    }
    for (int i = 0; i < resolvedOverride.length; i++) {
        final PlanPhasePriceOverride curOverride = resolvedOverride[i];
        if (curOverride != null) {
            final DefaultPlanPhase curPhase = (DefaultPlanPhase) parentPlan.getAllPhases()[i];
            if (curPhase.getFixed() == null && curOverride.getFixedPrice() != null) {
                final String error = String.format("There is no existing fixed price for the phase %s", curPhase.getName());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_INVALID_PRICE_OVERRIDE, parentPlan.getName(), error);
            }
            if (curPhase.getRecurring() == null && curOverride.getRecurringPrice() != null) {
                final String error = String.format("There is no existing recurring price for the phase %s", curPhase.getName());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_INVALID_PRICE_OVERRIDE, parentPlan.getName(), error);
            }
        }
    }
    final String planName;
    if (context != null) {
        final CatalogOverridePlanDefinitionModelDao overriddenPlan = overrideDao.getOrCreateOverridePlanDefinition(parentPlan.getName(), catalogEffectiveDate, resolvedOverride, context);
        planName = new StringBuffer(parentPlan.getName()).append("-").append(overriddenPlan.getRecordId()).toString();
    } else {
        planName = new StringBuffer(parentPlan.getName()).append("-dryrun-").append(DRY_RUN_PLAN_IDX.incrementAndGet()).toString();
    }
    final DefaultPlan result = new DefaultPlan(planName, (DefaultPlan) parentPlan, resolvedOverride);
    result.initialize(standaloneCatalog, standaloneCatalog.getCatalogURI());
    if (context == null) {
        overriddenPlanCache.addDryRunPlan(planName, result);
    }
    return result;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) Predicate(com.google.common.base.Predicate) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) CatalogOverridePlanDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePlanDefinitionModelDao) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)

Aggregations

PlanPhase (org.killbill.billing.catalog.api.PlanPhase)98 Plan (org.killbill.billing.catalog.api.Plan)77 Test (org.testng.annotations.Test)71 MockPlan (org.killbill.billing.catalog.MockPlan)50 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)49 LocalDate (org.joda.time.LocalDate)47 DateTime (org.joda.time.DateTime)43 BillingEventSet (org.killbill.billing.junction.BillingEventSet)43 BigDecimal (java.math.BigDecimal)42 BillingEvent (org.killbill.billing.junction.BillingEvent)41 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)40 Invoice (org.killbill.billing.invoice.api.Invoice)39 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)36 UUID (java.util.UUID)34 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)22 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)22 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)22 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)22 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)21 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)21