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;
}
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"));
}
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());
}
}
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());
}
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);
}
Aggregations