Search in sources :

Example 36 with Usage

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

the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsageWithUnlimitedMaxWith_TOP_TIER.

@Test(groups = "fast")
public void testComputeBilledUsageWithUnlimitedMaxWith_TOP_TIER() throws Exception {
    final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 100, 10, BigDecimal.TEN);
    final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
    final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 100, -1, BigDecimal.ONE);
    final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.TOP_TIER, tier1, tier2);
    final LocalDate targetDate = new LocalDate(2014, 03, 20);
    final ContiguousIntervalConsumableUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
    // 
    // In this model unit amount is first used to figure out which tier we are in, and then we price all unit at that 'target' tier
    // 
    List<UsageConsumableInArrearTierUnitAggregate> inputTier1 = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 2000L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
    // Target tier 2:
    assertEquals(inputTier1.size(), 1);
    assertEquals(inputTier1.get(0).getAmount(), new BigDecimal("20"));
}
Also used : DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) UsageConsumableInArrearTierUnitAggregate(org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 37 with Usage

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

the class TestContiguousIntervalConsumableInArrear method testComputeToBeBilledUsage.

@Test(groups = "fast")
public void testComputeToBeBilledUsage() throws Exception {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate endDate = new LocalDate(2014, 04, 20);
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
    final LocalDate targetDate = startDate.plusDays(1);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
    final List<InvoiceItem> existingUsage = Lists.newArrayList();
    final UsageInvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii1);
    final UsageInvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii2);
    // Will be ignored as is starts one day earlier.
    final UsageInvoiceItem ii3 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, startDate.minusDays(1), endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii3);
    // Will be ignored as it is for a different udsage section
    final UsageInvoiceItem ii4 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, "other", null, startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii4);
    // Will be ignored because non usage item
    final FixedPriceInvoiceItem ii5 = new FixedPriceInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, BigDecimal.TEN, currency);
    existingUsage.add(ii5);
    final BigDecimal result = intervalConsumableInArrear.computeBilledUsage(intervalConsumableInArrear.getBilledItems(startDate, endDate, existingUsage));
    assertEquals(result.compareTo(BigDecimal.TEN.add(BigDecimal.TEN)), 0);
}
Also used : DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) DefaultTier(org.killbill.billing.catalog.DefaultTier) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 38 with Usage

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

the class DefaultBillingEventSet method getUsages.

@Override
public Map<String, Usage> getUsages() {
    final Iterable<Usage> allUsages = Iterables.concat(Iterables.transform(this, new Function<BillingEvent, List<Usage>>() {

        @Override
        public List<Usage> apply(final BillingEvent input) {
            try {
                return input.getUsages();
            } catch (final CatalogApiException e) {
                throw new IllegalStateException(String.format("Failed to retrieve usage section for billing event %s", input), e);
            }
        }
    }));
    if (!allUsages.iterator().hasNext()) {
        return Collections.emptyMap();
    }
    final Map<String, Usage> result = new HashMap<String, Usage>();
    for (Usage cur : Sets.<Usage>newHashSet(allUsages)) {
        result.put(cur.getName(), cur);
    }
    return result;
}
Also used : Function(com.google.common.base.Function) Usage(org.killbill.billing.catalog.api.Usage) HashMap(java.util.HashMap) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) BillingEvent(org.killbill.billing.junction.BillingEvent)

Aggregations

Usage (org.killbill.billing.catalog.api.Usage)38 Test (org.testng.annotations.Test)22 LocalDate (org.joda.time.LocalDate)21 BigDecimal (java.math.BigDecimal)18 DefaultTier (org.killbill.billing.catalog.DefaultTier)17 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)16 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)15 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)12 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)12 HashMap (java.util.HashMap)11 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)9 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)8 UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)8 ArrayList (java.util.ArrayList)7 BillingEvent (org.killbill.billing.junction.BillingEvent)7 Predicate (com.google.common.base.Predicate)5 LinkedList (java.util.LinkedList)5 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)5 Function (com.google.common.base.Function)4 ImmutableList (com.google.common.collect.ImmutableList)4