use of org.killbill.billing.catalog.api.PlanPhasePriceOverride in project killbill by killbill.
the class TestCatalogOverrideDao method testOverrideLastPhase.
@Test(groups = "slow")
public void testOverrideLastPhase() throws Exception {
final StandaloneCatalog catalog = getCatalog("SpyCarBasic.xml");
;
final Plan plan = catalog.findPlan("standard-monthly");
final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
resolvedOverrides[0] = null;
resolvedOverrides[1] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal("128.76"), null);
final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
assertEquals(newPlan.getParentPlanName(), "standard-monthly");
assertTrue(newPlan.getIsActive());
}
use of org.killbill.billing.catalog.api.PlanPhasePriceOverride in project killbill by killbill.
the class TestSubscriptionHelper method createSubscription.
private DefaultSubscriptionBase createSubscription(final boolean noEvents, @Nullable final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final LocalDate requestedDate) throws SubscriptionBaseApiException {
final VersionedCatalog catalog;
try {
catalog = catalogInternalApi.getFullCatalog(true, true, internalCallContext);
} catch (CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
// Make sure the right account information is used
final InternalCallContext internalCallContext = bundle == null ? this.internalCallContext : internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), ObjectType.ACCOUNT, this.internalCallContext.getUpdatedBy(), this.internalCallContext.getCallOrigin(), this.internalCallContext.getContextUserType(), this.internalCallContext.getUserToken(), this.internalCallContext.getTenantRecordId());
boolean bundleExists = false;
if (bundle != null) {
try {
bundleExists = (subscriptionApi.getBundleFromId(bundle.getId(), internalCallContext) != null);
} catch (final SubscriptionBaseApiException ignored) {
}
}
if (!noEvents && (requestedDate == null || requestedDate.compareTo(clock.getUTCToday()) <= 0)) {
testListener.pushExpectedEvent(NextEvent.CREATE);
}
final ImmutableList<EntitlementSpecifier> entitlementSpecifiers = ImmutableList.<EntitlementSpecifier>of(new EntitlementSpecifier() {
@Override
public PlanPhaseSpecifier getPlanPhaseSpecifier() {
return new PlanPhaseSpecifier(productName, term, planSet, phaseType);
}
@Override
public Integer getBillCycleDay() {
return null;
}
@Override
public String getExternalKey() {
return UUID.randomUUID().toString();
}
@Override
public List<PlanPhasePriceOverride> getOverrides() {
return null;
}
});
final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(bundle == null || !bundleExists ? null : bundle.getId(), bundle == null ? null : bundle.getExternalKey(), entitlementSpecifiers, requestedDate, false);
final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = subscriptionApi.createBaseSubscriptionsWithAddOns(catalog, ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), false, internalCallContext).get(0);
final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) subscriptionBaseWithAddOns.getSubscriptionBaseList().get(0);
assertNotNull(subscription);
testListener.assertListenerStatus();
mockNonEntityDao.addTenantRecordIdMapping(subscription.getId(), internalCallContext);
mockNonEntityDao.addAccountRecordIdMapping(subscription.getId(), internalCallContext);
mockNonEntityDao.addTenantRecordIdMapping(subscription.getBundleId(), internalCallContext);
mockNonEntityDao.addAccountRecordIdMapping(subscription.getBundleId(), internalCallContext);
return subscription;
}
use of org.killbill.billing.catalog.api.PlanPhasePriceOverride in project killbill by killbill.
the class TestDefaultPriceOverride method testOverrideOneOutOfTwoTieredBlockPrices.
@Test(groups = "slow")
public void testOverrideOneOutOfTwoTieredBlockPrices() throws Exception {
final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
final Plan plan = catalog.findPlan("chocolate-monthly");
final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
final List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
tieredBlockPriceOverrides.add(new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal("0.75"), Currency.USD, new Double("10000")));
final List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
final List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
usagePriceOverrides.add(new DefaultUsagePriceOverride("chocolate-monthly-videos", UsageType.CONSUMABLE, tierPriceOverrides));
final PlanPhasePriceOverride phase = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, null, usagePriceOverrides);
overrides.add(phase);
// Overriding only the tieredblockprice for unit - 'chocolate-videos' with size = 1 and max = 10000 from $1 to $0.75
final DefaultPlan overriddenPlan = priceOverride.getOrCreateOverriddenPlan(catalog, plan, new DateTime(catalog.getEffectiveDate()), overrides, internalCallContext);
final String[] parts = priceOverridePattern.getPlanParts(overriddenPlan.getName());
assertEquals(parts[0], plan.getName());
assertEquals(overriddenPlan.getProduct().getName(), plan.getProduct().getName());
assertEquals(overriddenPlan.getRecurringBillingPeriod(), plan.getRecurringBillingPeriod());
if (plan.getEffectiveDateForExistingSubscriptions() != null) {
assertEquals(overriddenPlan.getEffectiveDateForExistingSubscriptions().compareTo(plan.getEffectiveDateForExistingSubscriptions()), 0);
}
assertNotEquals(overriddenPlan.getFinalPhase().getName(), plan.getFinalPhase().getName());
for (int i = 0; i < overriddenPlan.getFinalPhase().getUsages().length; i++) {
final DefaultUsage initialUsage = (DefaultUsage) plan.getFinalPhase().getUsages()[i];
final DefaultUsage newUsage = (DefaultUsage) overriddenPlan.getFinalPhase().getUsages()[i];
assertEquals(newUsage.getName(), initialUsage.getName());
assertEquals(newUsage.getUsageType(), initialUsage.getUsageType());
assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
assertEquals(newUsage.getTiers().length, initialUsage.getTiers().length);
for (int j = 0; j < newUsage.getTiers().length; j++) {
final DefaultTier initialTier = (DefaultTier) initialUsage.getTiers()[j];
final DefaultTier newTier = (DefaultTier) newUsage.getTiers()[j];
assertEquals(newTier.getTieredBlocks().length, initialTier.getTieredBlocks().length);
for (int k = 0; k < newTier.getTieredBlocks().length; k++) {
final DefaultTieredBlock initialTieredBlock = (DefaultTieredBlock) initialTier.getTieredBlocks()[k];
final DefaultTieredBlock newTieredBlock = (DefaultTieredBlock) newTier.getTieredBlocks()[k];
final TieredBlockPriceOverride override = Iterables.tryFind(tieredBlockPriceOverrides, new Predicate<TieredBlockPriceOverride>() {
@Override
public boolean apply(final TieredBlockPriceOverride input) {
return input.getUnitName().equals(initialTieredBlock.getUnit().getName()) && Double.compare(input.getSize(), initialTieredBlock.getSize()) == 0 && Double.compare(input.getMax(), initialTieredBlock.getMax()) == 0;
}
}).orNull();
assertEquals(newTieredBlock.getUnit().getName(), initialTieredBlock.getUnit().getName());
assertEquals(newTieredBlock.getMax(), initialTieredBlock.getMax());
assertEquals(newTieredBlock.getSize(), initialTieredBlock.getSize());
assertTieredBlockInternationalPrice(newTieredBlock.getPrice(), initialTieredBlock.getPrice(), override);
}
}
}
}
use of org.killbill.billing.catalog.api.PlanPhasePriceOverride in project killbill by killbill.
the class TestDefaultPriceOverride method testGetOverriddenPlan.
@Test(groups = "slow")
public void testGetOverriddenPlan() throws Exception {
final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
catalog.initialize(catalog);
final Plan plan = catalog.findPlan("discount-standard-monthly");
final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
final PlanPhasePriceOverride phase1 = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[0].getName(), Currency.USD, BigDecimal.ONE, null, null);
overrides.add(phase1);
final PlanPhasePriceOverride phase3 = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[2].getName(), Currency.USD, null, new BigDecimal("142.41"), null);
overrides.add(phase3);
final DefaultPlan overriddenPlanCreated = priceOverride.getOrCreateOverriddenPlan(catalog, plan, new DateTime(catalog.getEffectiveDate()), overrides, internalCallContext);
System.out.println("overriddenPlanCreated = " + overriddenPlanCreated.getName());
final DefaultPlan overriddenPlan = priceOverride.getOverriddenPlan(overriddenPlanCreated.getName(), catalog, internalCallContext);
assertEquals(overriddenPlan.getProduct().getName(), plan.getProduct().getName());
assertEquals(overriddenPlan.getRecurringBillingPeriod(), plan.getRecurringBillingPeriod());
if (plan.getEffectiveDateForExistingSubscriptions() != null) {
assertEquals(overriddenPlan.getEffectiveDateForExistingSubscriptions().compareTo(plan.getEffectiveDateForExistingSubscriptions()), 0);
}
assertNotEquals(overriddenPlan.getFinalPhase().getName(), plan.getFinalPhase().getName());
assertEquals(overriddenPlan.getPlansAllowedInBundle(), plan.getPlansAllowedInBundle());
assertEquals(overriddenPlan.getAllPhases().length, overriddenPlan.getAllPhases().length);
for (int i = 0; i < overriddenPlan.getAllPhases().length; i++) {
final DefaultPlanPhase initialPhase = (DefaultPlanPhase) plan.getAllPhases()[i];
final DefaultPlanPhase newPhase = (DefaultPlanPhase) overriddenPlan.getAllPhases()[i];
final PlanPhasePriceOverride override = Iterables.tryFind(overrides, new Predicate<PlanPhasePriceOverride>() {
@Override
public boolean apply(final PlanPhasePriceOverride input) {
return input.getPhaseName().equals(initialPhase.getName());
}
}).orNull();
assertNotEquals(newPhase.getName(), initialPhase.getName());
assertEquals(newPhase.getName(), overriddenPlan.getName() + "-" + initialPhase.getName().split("-")[initialPhase.getName().split("-").length - 1]);
assertEquals(newPhase.getDuration(), initialPhase.getDuration());
assertEquals(newPhase.getPhaseType(), initialPhase.getPhaseType());
assertEquals(newPhase.getUsages().length, initialPhase.getUsages().length);
if (initialPhase.getFixed() != null) {
assertEquals(newPhase.getFixed().getType(), initialPhase.getFixed().getType());
assertInternationalPrice(newPhase.getFixed().getPrice(), initialPhase.getFixed().getPrice(), override, true);
}
if (initialPhase.getRecurring() != null) {
assertInternationalPrice(newPhase.getRecurring().getRecurringPrice(), initialPhase.getRecurring().getRecurringPrice(), override, false);
}
}
}
use of org.killbill.billing.catalog.api.PlanPhasePriceOverride in project killbill by killbill.
the class TestDefaultPriceOverride method testOverrideTwoOutOfTwoTieredBlockPrices.
@Test(groups = "slow")
public void testOverrideTwoOutOfTwoTieredBlockPrices() throws Exception {
final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
final Plan plan = catalog.findPlan("chocolate-monthly");
final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
final List<TieredBlockPriceOverride> tieredBlockPriceOverrides1 = new ArrayList<TieredBlockPriceOverride>();
tieredBlockPriceOverrides1.add(new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal("1.5"), Currency.USD, new Double("5")));
final List<TieredBlockPriceOverride> tieredBlockPriceOverrides2 = new ArrayList<TieredBlockPriceOverride>();
tieredBlockPriceOverrides2.add(new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal("0.75"), Currency.USD, new Double("10000")));
final List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides1));
tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides2));
final List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
usagePriceOverrides.add(new DefaultUsagePriceOverride("chocolate-monthly-videos", UsageType.CONSUMABLE, tierPriceOverrides));
final PlanPhasePriceOverride phase = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal("35"), usagePriceOverrides);
overrides.add(phase);
/* Overriding phase recurring price from $30 to $35, tieredblockprice from $2 to $1.5 for unit- 'chocolate-videos' with size = 1 and max = 5 and
also overriding tieredblockprice from $1 to $0.75 for unit - 'chocolate-videos' with size = 1 and max = 10000 */
final DefaultPlan overriddenPlan = priceOverride.getOrCreateOverriddenPlan(catalog, plan, new DateTime(catalog.getEffectiveDate()), overrides, internalCallContext);
final String[] parts = priceOverridePattern.getPlanParts(overriddenPlan.getName());
assertEquals(parts[0], plan.getName());
assertEquals(overriddenPlan.getProduct().getName(), plan.getProduct().getName());
assertEquals(overriddenPlan.getRecurringBillingPeriod(), plan.getRecurringBillingPeriod());
if (plan.getEffectiveDateForExistingSubscriptions() != null) {
assertEquals(overriddenPlan.getEffectiveDateForExistingSubscriptions().compareTo(plan.getEffectiveDateForExistingSubscriptions()), 0);
}
assertNotEquals(overriddenPlan.getFinalPhase().getName(), plan.getFinalPhase().getName());
final DefaultPlanPhase initialPhase = (DefaultPlanPhase) plan.getFinalPhase();
final DefaultPlanPhase newPhase = (DefaultPlanPhase) overriddenPlan.getFinalPhase();
final PlanPhasePriceOverride override = Iterables.tryFind(overrides, new Predicate<PlanPhasePriceOverride>() {
@Override
public boolean apply(final PlanPhasePriceOverride input) {
return input.getPhaseName().equals(initialPhase.getName());
}
}).orNull();
assertNotEquals(newPhase.getName(), initialPhase.getName());
assertEquals(newPhase.getName(), overriddenPlan.getName() + "-" + initialPhase.getName().split("-")[initialPhase.getName().split("-").length - 1]);
assertEquals(newPhase.getDuration(), initialPhase.getDuration());
assertEquals(newPhase.getPhaseType(), initialPhase.getPhaseType());
assertEquals(newPhase.getUsages().length, initialPhase.getUsages().length);
if (initialPhase.getFixed() != null) {
assertEquals(newPhase.getFixed().getType(), initialPhase.getFixed().getType());
assertInternationalPrice(newPhase.getFixed().getPrice(), initialPhase.getFixed().getPrice(), override, true);
}
if (initialPhase.getRecurring() != null) {
assertInternationalPrice(newPhase.getRecurring().getRecurringPrice(), initialPhase.getRecurring().getRecurringPrice(), override, false);
}
for (int i = 0; i < overriddenPlan.getFinalPhase().getUsages().length; i++) {
final DefaultUsage initialUsage = (DefaultUsage) plan.getFinalPhase().getUsages()[i];
final DefaultUsage newUsage = (DefaultUsage) overriddenPlan.getFinalPhase().getUsages()[i];
assertEquals(newUsage.getName(), initialUsage.getName());
assertEquals(newUsage.getUsageType(), initialUsage.getUsageType());
assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
assertEquals(newUsage.getTiers().length, initialUsage.getTiers().length);
for (int j = 0; j < newUsage.getTiers().length; j++) {
final DefaultTier initialTier = (DefaultTier) initialUsage.getTiers()[j];
final DefaultTier newTier = (DefaultTier) newUsage.getTiers()[j];
assertEquals(newTier.getTieredBlocks().length, initialTier.getTieredBlocks().length);
for (int k = 0; k < newTier.getTieredBlocks().length; k++) {
final DefaultTieredBlock initialTieredBlock = (DefaultTieredBlock) initialTier.getTieredBlocks()[k];
final DefaultTieredBlock newTieredBlock = (DefaultTieredBlock) newTier.getTieredBlocks()[k];
List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
tieredBlockPriceOverrides.addAll(tieredBlockPriceOverrides1);
tieredBlockPriceOverrides.addAll(tieredBlockPriceOverrides2);
final TieredBlockPriceOverride tieredBlockPriceOverride = Iterables.tryFind(tieredBlockPriceOverrides, new Predicate<TieredBlockPriceOverride>() {
@Override
public boolean apply(final TieredBlockPriceOverride input) {
return input.getUnitName().equals(initialTieredBlock.getUnit().getName()) && Double.compare(input.getSize(), initialTieredBlock.getSize()) == 0 && Double.compare(input.getMax(), initialTieredBlock.getMax()) == 0;
}
}).orNull();
assertEquals(newTieredBlock.getUnit().getName(), initialTieredBlock.getUnit().getName());
assertEquals(newTieredBlock.getMax(), initialTieredBlock.getMax());
assertEquals(newTieredBlock.getSize(), initialTieredBlock.getSize());
assertTieredBlockInternationalPrice(newTieredBlock.getPrice(), initialTieredBlock.getPrice(), tieredBlockPriceOverride);
}
}
}
}
Aggregations