Search in sources :

Example 31 with PlanPhasePriceOverride

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

the class SubscriptionResource method buildEntitlementSpecifierList.

private List<EntitlementSpecifier> buildEntitlementSpecifierList(final List<SubscriptionJson> entitlements, final Currency currency) {
    final List<EntitlementSpecifier> entitlementSpecifierList = new ArrayList<EntitlementSpecifier>();
    for (final SubscriptionJson entitlement : entitlements) {
        // verifications
        verifyNonNullOrEmpty(entitlement, "SubscriptionJson body should be specified for each element");
        if (entitlement.getPlanName() == null) {
            verifyNonNullOrEmpty(entitlement.getProductName(), "SubscriptionJson productName needs to be set for each element", entitlement.getProductCategory(), "SubscriptionJson productCategory needs to be set for each element", entitlement.getBillingPeriod(), "SubscriptionJson billingPeriod needs to be set for each element", entitlement.getPriceList(), "SubscriptionJson priceList needs to be set for each element");
        }
        // create the entitlementSpecifier
        final PlanPhaseSpecifier planPhaseSpecifier = entitlement.getPlanName() != null ? new PlanPhaseSpecifier(entitlement.getPlanName(), null) : new PlanPhaseSpecifier(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), null);
        final List<PlanPhasePriceOverride> overrides = PhasePriceOverrideJson.toPlanPhasePriceOverrides(entitlement.getPriceOverrides(), planPhaseSpecifier, currency);
        EntitlementSpecifier specifier = new EntitlementSpecifier() {

            @Override
            public PlanPhaseSpecifier getPlanPhaseSpecifier() {
                return planPhaseSpecifier;
            }

            @Override
            public List<PlanPhasePriceOverride> getOverrides() {
                return overrides;
            }
        };
        entitlementSpecifierList.add(specifier);
    }
    return entitlementSpecifierList;
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) SubscriptionJson(org.killbill.billing.jaxrs.json.SubscriptionJson) ArrayList(java.util.ArrayList) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 32 with PlanPhasePriceOverride

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

the class TestStandaloneCatalogWithPriceOverride method testCreateAmbiguousPlan.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/842")
public void testCreateAmbiguousPlan() throws Exception {
    final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
    final StaticCatalog standaloneCatalogWithPriceOverride = new StandaloneCatalogWithPriceOverride(catalog, priceOverride, internalCallContext.getTenantRecordId(), internalCallContextFactory);
    // Create ambiguous plan name
    final PlanSpecifier spec = new PlanSpecifier("standard-monthly-67890");
    final PlanPhasePriceOverridesWithCallContext overrides = Mockito.mock(PlanPhasePriceOverridesWithCallContext.class);
    Mockito.when(overrides.getCallContext()).thenReturn(callContext);
    final PlanPhasePriceOverride override = new DefaultPlanPhasePriceOverride("standard-monthly-evergreen", Currency.USD, null, BigDecimal.ONE, ImmutableList.<UsagePriceOverride>of());
    Mockito.when(overrides.getOverrides()).thenReturn(ImmutableList.of(override));
    final Plan plan = standaloneCatalogWithPriceOverride.createOrFindPlan(spec, overrides);
    Assert.assertTrue(plan.getName().startsWith("standard-monthly-67890-"));
    Assert.assertTrue(priceOverridePattern.isOverriddenPlan(plan.getName()));
    // From the catalog
    Assert.assertNotNull(catalog.findPlan("standard-monthly"));
    Assert.assertNotNull(standaloneCatalogWithPriceOverride.findPlan("standard-monthly"));
    // Created on the fly
    try {
        catalog.findPlan(plan.getName());
        Assert.fail();
    } catch (final CatalogApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PLAN.getCode());
    }
    Assert.assertNotNull(standaloneCatalogWithPriceOverride.findPlan("standard-monthly-1"));
    // From the catalog
    Assert.assertNotNull(catalog.findPlan("standard-monthly-12345"));
    Assert.assertNotNull(standaloneCatalogWithPriceOverride.findPlan("standard-monthly-12345"));
}
Also used : PlanPhasePriceOverridesWithCallContext(org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Plan(org.killbill.billing.catalog.api.Plan) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) PlanSpecifier(org.killbill.billing.catalog.api.PlanSpecifier) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Test(org.testng.annotations.Test)

Example 33 with PlanPhasePriceOverride

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

the class TestCatalogOverrideDao method testGetOverriddenPlanPhases.

@Test(groups = "slow")
public void testGetOverriddenPlanPhases() throws Exception {
    final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
    final Plan plan = catalog.findPlan("discount-standard-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[0].getName(), Currency.USD, BigDecimal.TEN, BigDecimal.ONE, null);
    resolvedOverrides[1] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[1].getName(), Currency.USD, BigDecimal.ONE, BigDecimal.TEN, null);
    resolvedOverrides[2] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, BigDecimal.ZERO, new BigDecimal("348.64"), null);
    final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
    final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
    assertEquals(phases.size(), 3);
    for (int i = 0; i < 3; i++) {
        final CatalogOverridePhaseDefinitionModelDao curPhase = phases.get(i);
        assertEquals(curPhase.getCurrency(), resolvedOverrides[i].getCurrency().name());
        assertEquals(curPhase.getFixedPrice().compareTo(resolvedOverrides[i].getFixedPrice()), 0);
        assertEquals(curPhase.getRecurringPrice().compareTo(resolvedOverrides[i].getRecurringPrice()), 0);
        assertEquals(curPhase.getParentPhaseName(), resolvedOverrides[i].getPhaseName());
    }
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Test(org.testng.annotations.Test)

Example 34 with PlanPhasePriceOverride

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

the class TestCatalogOverrideDao method testGetOverrideOneOutOfTwoTieredBlocks.

@Test(groups = "slow")
public void testGetOverrideOneOutOfTwoTieredBlocks() throws Exception {
    final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
    final Plan plan = catalog.findPlan("chocolate-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
    DefaultTieredBlockPriceOverride tieredBlockPriceOverride = new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal(0.75), Currency.USD, new Double("10000"));
    tieredBlockPriceOverrides.add(tieredBlockPriceOverride);
    List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
    DefaultTierPriceOverride tierPriceOverride = new DefaultTierPriceOverride(tieredBlockPriceOverrides);
    tierPriceOverrides.add(null);
    tierPriceOverrides.add(tierPriceOverride);
    List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
    DefaultUsagePriceOverride usagePriceOverride = new DefaultUsagePriceOverride("chocolate-monthly-videos", UsageType.CONSUMABLE, tierPriceOverrides);
    usagePriceOverrides.add(usagePriceOverride);
    // Override chocolate-videos unit price with size = 1 and max value = 10000 from $1 to $0.75
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, null, usagePriceOverrides);
    final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
    final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
    assertEquals(phases.size(), 1);
    final CatalogOverridePhaseDefinitionModelDao curPhase = phases.get(0);
    assertEquals(curPhase.getCurrency(), resolvedOverrides[0].getCurrency().name());
    Assert.assertNull(curPhase.getFixedPrice());
    Assert.assertNull(curPhase.getRecurringPrice());
    assertEquals(curPhase.getParentPhaseName(), resolvedOverrides[0].getPhaseName());
    final List<CatalogOverrideUsageDefinitionModelDao> usages = catalogOverrideDao.getOverriddenPhaseUsages(curPhase.getRecordId(), internalCallContext);
    assertEquals(usages.size(), 1);
    final CatalogOverrideUsageDefinitionModelDao curUsage = usages.get(0);
    assertEquals(curUsage.getParentUsageName(), usagePriceOverride.getName());
    assertEquals(curUsage.getType(), usagePriceOverride.getUsageType().toString());
    final List<CatalogOverrideTierDefinitionModelDao> tiers = catalogOverrideDao.getOverriddenUsageTiers(curUsage.getRecordId(), internalCallContext);
    assertEquals(tiers.size(), 1);
    final CatalogOverrideTierDefinitionModelDao curTier = tiers.get(0);
    final List<CatalogOverrideBlockDefinitionModelDao> tierBlocks = catalogOverrideDao.getOverriddenTierBlocks(curTier.getRecordId(), internalCallContext);
    assertEquals(tierBlocks.size(), 1);
    final CatalogOverrideBlockDefinitionModelDao curTieredBlock = tierBlocks.get(0);
    assertEquals(curTieredBlock.getParentUnitName(), tieredBlockPriceOverride.getUnitName());
    assertEquals(curTieredBlock.getPrice().compareTo(tieredBlockPriceOverride.getPrice()), 0);
    assertEquals(curTieredBlock.getSize(), (double) tieredBlockPriceOverride.getSize());
    assertEquals(curTieredBlock.getMax(), (double) tieredBlockPriceOverride.getMax());
}
Also used : DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) DefaultUsagePriceOverride(org.killbill.billing.catalog.DefaultUsagePriceOverride) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) DateTime(org.joda.time.DateTime) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) DefaultUsagePriceOverride(org.killbill.billing.catalog.DefaultUsagePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) Test(org.testng.annotations.Test)

Example 35 with PlanPhasePriceOverride

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

the class TestCatalogOverrideDao method testOverrideTwoOutOfThreePhases.

@Test(groups = "slow")
public void testOverrideTwoOutOfThreePhases() throws Exception {
    final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
    final Plan plan = catalog.findPlan("discount-standard-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[0].getName(), Currency.USD, BigDecimal.TEN, null, null);
    resolvedOverrides[1] = null;
    resolvedOverrides[2] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal("348.64"), null);
    final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
    assertEquals(newPlan.getParentPlanName(), "discount-standard-monthly");
    assertTrue(newPlan.getIsActive());
    final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
    assertEquals(phases.size(), 2);
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Test(org.testng.annotations.Test)

Aggregations

PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)49 ArrayList (java.util.ArrayList)28 DateTime (org.joda.time.DateTime)26 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)24 Test (org.testng.annotations.Test)24 BigDecimal (java.math.BigDecimal)22 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)16 Plan (org.killbill.billing.catalog.api.Plan)15 LocalDate (org.joda.time.LocalDate)14 Account (org.killbill.billing.account.api.Account)14 UsagePriceOverride (org.killbill.billing.catalog.api.UsagePriceOverride)13 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)12 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)11 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)10 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)9 Predicate (com.google.common.base.Predicate)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)8 Entitlement (org.killbill.billing.entitlement.api.Entitlement)8 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)7 UUID (java.util.UUID)6