Search in sources :

Example 1 with DefaultTierPriceOverride

use of org.killbill.billing.catalog.DefaultTierPriceOverride 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 2 with DefaultTierPriceOverride

use of org.killbill.billing.catalog.DefaultTierPriceOverride in project killbill by killbill.

the class TestCatalogOverrideDao method testGetOverriddenPlanPhasesWithUsageOverrides.

@Test(groups = "slow")
public void testGetOverriddenPlanPhasesWithUsageOverrides() throws Exception {
    final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
    final Plan plan = catalog.findPlan("gas-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
    DefaultTieredBlockPriceOverride tieredBlockPriceOverride = new DefaultTieredBlockPriceOverride("gallons", new Double("1"), new BigDecimal(4), Currency.USD, new Double("100"));
    tieredBlockPriceOverrides.add(tieredBlockPriceOverride);
    List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
    DefaultTierPriceOverride tierPriceOverride = new DefaultTierPriceOverride(tieredBlockPriceOverrides);
    tierPriceOverrides.add(tierPriceOverride);
    List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
    DefaultUsagePriceOverride usagePriceOverride = new DefaultUsagePriceOverride("gas-monthly-in-arrear", UsageType.CONSUMABLE, tierPriceOverrides);
    usagePriceOverrides.add(usagePriceOverride);
    // Override the gallons price from $3.95 to $4 and also the recurring price from  $0 to $348.64
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, BigDecimal.ZERO, new BigDecimal("348.64"), 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());
    assertEquals(curPhase.getFixedPrice().compareTo(resolvedOverrides[0].getFixedPrice()), 0);
    assertEquals(curPhase.getRecurringPrice().compareTo(resolvedOverrides[0].getRecurringPrice()), 0);
    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 3 with DefaultTierPriceOverride

use of org.killbill.billing.catalog.DefaultTierPriceOverride in project killbill by killbill.

the class DefaultPriceOverride method getResolvedTierOverrides.

public List<TierPriceOverride> getResolvedTierOverrides(Tier[] tiers, List<TierPriceOverride> tierPriceOverrides) throws CatalogApiException {
    List<TierPriceOverride> resolvedTierOverrides = new ArrayList<TierPriceOverride>();
    for (final Tier curTier : tiers) {
        final TierPriceOverride curOverride = Iterables.tryFind(tierPriceOverrides, new Predicate<TierPriceOverride>() {

            @Override
            public boolean apply(final TierPriceOverride input) {
                if (input.getTieredBlockPriceOverrides() != null) {
                    for (TieredBlockPriceOverride blockPriceOverride : input.getTieredBlockPriceOverrides()) {
                        String unitName = blockPriceOverride.getUnitName();
                        Double max = blockPriceOverride.getMax();
                        Double size = blockPriceOverride.getSize();
                        for (int i = 0; i < curTier.getTieredBlocks().length; i++) {
                            TieredBlock curTieredBlock = curTier.getTieredBlocks()[i];
                            if (unitName.equals(curTieredBlock.getUnit().getName()) && Double.compare(size, curTieredBlock.getSize()) == 0 && Double.compare(max, curTieredBlock.getMax()) == 0) {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        }).orNull();
        if (curOverride != null) {
            List<TieredBlockPriceOverride> tieredBlockPriceOverrides = getResolvedTieredBlockPriceOverrides(curTier.getTieredBlocks(), curOverride.getTieredBlockPriceOverrides());
            resolvedTierOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
        } else {
            resolvedTierOverrides.add(null);
        }
    }
    return resolvedTierOverrides;
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) TieredBlock(org.killbill.billing.catalog.api.TieredBlock) Tier(org.killbill.billing.catalog.api.Tier) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) Predicate(com.google.common.base.Predicate)

Example 4 with DefaultTierPriceOverride

use of org.killbill.billing.catalog.DefaultTierPriceOverride in project killbill by killbill.

the class DefaultOverriddenPlanCache method getTierPriceOverrides.

List<TierPriceOverride> getTierPriceOverrides(Usage curUsage, CatalogOverrideUsageDefinitionModelDao overriddenUsage, final InternalTenantContext context) {
    final List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
    final List<CatalogOverrideTierDefinitionModelDao> tierDefs = overrideDao.getOverriddenUsageTiers(overriddenUsage.getRecordId(), context);
    for (int i = 0; i < curUsage.getTiers().length; i++) {
        final Tier curTier = curUsage.getTiers()[i];
        final TieredBlock[] curTieredBlocks = curTier.getTieredBlocks();
        final CatalogOverrideTierDefinitionModelDao overriddenTier = Iterables.tryFind(tierDefs, new Predicate<CatalogOverrideTierDefinitionModelDao>() {

            @Override
            public boolean apply(final CatalogOverrideTierDefinitionModelDao input) {
                final List<CatalogOverrideBlockDefinitionModelDao> blockDefs = overrideDao.getOverriddenTierBlocks(input.getRecordId(), context);
                for (CatalogOverrideBlockDefinitionModelDao blockDef : blockDefs) {
                    String unitName = blockDef.getParentUnitName();
                    Double max = blockDef.getMax();
                    Double size = blockDef.getSize();
                    for (TieredBlock curTieredBlock : curTieredBlocks) {
                        if (unitName.equals(curTieredBlock.getUnit().getName()) && Double.compare(size, curTieredBlock.getSize()) == 0 && Double.compare(max, curTieredBlock.getMax()) == 0) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }).orNull();
        if (overriddenTier != null) {
            List<TieredBlockPriceOverride> tieredBlockPriceOverrides = getTieredBlockPriceOverrides(curTier, overriddenTier, context);
            tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
        }
    }
    return tierPriceOverrides;
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) Tier(org.killbill.billing.catalog.api.Tier) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) CatalogOverrideTierDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverrideTierDefinitionModelDao) Predicate(com.google.common.base.Predicate) TieredBlock(org.killbill.billing.catalog.api.TieredBlock) CatalogOverrideBlockDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverrideBlockDefinitionModelDao) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride)

Aggregations

ArrayList (java.util.ArrayList)4 DefaultTierPriceOverride (org.killbill.billing.catalog.DefaultTierPriceOverride)4 DefaultTieredBlockPriceOverride (org.killbill.billing.catalog.DefaultTieredBlockPriceOverride)4 TierPriceOverride (org.killbill.billing.catalog.api.TierPriceOverride)4 TieredBlockPriceOverride (org.killbill.billing.catalog.api.TieredBlockPriceOverride)4 Predicate (com.google.common.base.Predicate)2 BigDecimal (java.math.BigDecimal)2 DateTime (org.joda.time.DateTime)2 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)2 DefaultUsagePriceOverride (org.killbill.billing.catalog.DefaultUsagePriceOverride)2 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)2 Plan (org.killbill.billing.catalog.api.Plan)2 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)2 Tier (org.killbill.billing.catalog.api.Tier)2 TieredBlock (org.killbill.billing.catalog.api.TieredBlock)2 UsagePriceOverride (org.killbill.billing.catalog.api.UsagePriceOverride)2 Test (org.testng.annotations.Test)2 CatalogOverrideBlockDefinitionModelDao (org.killbill.billing.catalog.dao.CatalogOverrideBlockDefinitionModelDao)1 CatalogOverrideTierDefinitionModelDao (org.killbill.billing.catalog.dao.CatalogOverrideTierDefinitionModelDao)1