Search in sources :

Example 31 with Usage

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

the class TestContiguousIntervalCapacityInArrear 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 DefaultUnit unit = new DefaultUnit().setName("unit");
    final DefaultLimit limit = new DefaultLimit().setUnit(unit).setMax((double) 100);
    final DefaultTier tier = createDefaultTierWithLimits(BigDecimal.TEN, limit);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = startDate.plusDays(1);
    final ContiguousIntervalUsageInArrear intervalCapacityInArrear = createContiguousIntervalCapacityInArrear(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 Iterable<InvoiceItem> billedItems = intervalCapacityInArrear.getBilledItems(startDate, endDate, existingUsage);
    final BigDecimal result = intervalCapacityInArrear.computeBilledUsage(billedItems);
    assertEquals(result.compareTo(BigDecimal.TEN.add(BigDecimal.TEN)), 0);
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) 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) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 32 with Usage

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

the class TestContiguousIntervalCapacityInArrear method testComputeBilledUsage.

@Test(groups = "fast")
public void testComputeBilledUsage() throws Exception {
    final DefaultUnit unit1 = new DefaultUnit().setName("unit1");
    final DefaultUnit unit2 = new DefaultUnit().setName("unit2");
    final DefaultUnit unit3 = new DefaultUnit().setName("unit3");
    final DefaultLimit limit1_1 = new DefaultLimit().setUnit(unit1).setMax((double) 100).setMin((double) -1);
    final DefaultLimit limit1_2 = new DefaultLimit().setUnit(unit2).setMax((double) 1000).setMin((double) -1);
    final DefaultLimit limit1_3 = new DefaultLimit().setUnit(unit3).setMax((double) 50).setMin((double) -1);
    final DefaultTier tier1 = createDefaultTierWithLimits(BigDecimal.TEN, limit1_1, limit1_2, limit1_3);
    final DefaultLimit limit2_1 = new DefaultLimit().setUnit(unit1).setMax((double) 200).setMin((double) -1);
    final DefaultLimit limit2_2 = new DefaultLimit().setUnit(unit2).setMax((double) 2000).setMin((double) -1);
    final DefaultLimit limit2_3 = new DefaultLimit().setUnit(unit3).setMax((double) 100).setMin((double) -1);
    final DefaultTier tier2 = createDefaultTierWithLimits(new BigDecimal("20.0"), limit2_1, limit2_2, limit2_3);
    // Don't define any max for last tier to allow any number
    final DefaultLimit limit3_1 = new DefaultLimit().setUnit(unit1).setMin((double) -1).setMax((double) -1);
    final DefaultLimit limit3_2 = new DefaultLimit().setUnit(unit2).setMin((double) -1).setMax((double) -1);
    final DefaultLimit limit3_3 = new DefaultLimit().setUnit(unit3).setMax((double) -1).setMin((double) -1);
    final DefaultTier tier3 = createDefaultTierWithLimits(new BigDecimal("30.0"), limit3_1, limit3_2, limit3_3);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier1, tier2, tier3);
    final LocalDate targetDate = new LocalDate(2014, 03, 20);
    final ContiguousIntervalCapacityUsageInArrear intervalCapacityInArrear = createContiguousIntervalCapacityInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
    // Tier 1 (both units from tier 1)
    UsageCapacityInArrearAggregate result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 100L), new DefaultRolledUpUnit("unit2", 1000L), new DefaultRolledUpUnit("unit3", 50L)));
    assertEquals(result.getTierDetails().size(), 3);
    assertTrue(result.getAmount().compareTo(BigDecimal.TEN) == 0);
    // Tier 2 (only one unit from tier 1)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 100L), new DefaultRolledUpUnit("unit2", 1001L)));
    assertTrue(result.getAmount().compareTo(new BigDecimal("20.0")) == 0);
    // Tier 2 (only one unit from tier 1)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 101L), new DefaultRolledUpUnit("unit2", 1000L)));
    assertTrue(result.getAmount().compareTo(new BigDecimal("20.0")) == 0);
    // Tier 2 (both units from tier 2)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 101L), new DefaultRolledUpUnit("unit2", 1001L)));
    assertTrue(result.getAmount().compareTo(new BigDecimal("20.0")) == 0);
    // Tier 3 (only one unit from tier 3)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 10L), new DefaultRolledUpUnit("unit2", 2001L)));
    assertTrue(result.getAmount().compareTo(new BigDecimal("30.0")) == 0);
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) UsageCapacityInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageCapacityInArrearAggregate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 33 with Usage

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

the class TestSubscriptionConsumableInArrear method testComputeInArrearUsageInterval.

@Test(groups = "fast")
public void testComputeInArrearUsageInterval() throws Exception {
    final List<BillingEvent> billingEvents = Lists.newArrayList();
    final String usageName1 = "erw";
    final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
    final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
    final Usage usage1 = createConsumableInArrearUsage(usageName1, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier1);
    final String usageName2 = "hghg";
    final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
    final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
    final Usage usage2 = createConsumableInArrearUsage(usageName2, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier2);
    final DateTime dt1 = new DateTime(2013, 3, 23, 4, 34, 59, DateTimeZone.UTC);
    final BillingEvent evt1 = createMockBillingEvent(dt1, BillingPeriod.MONTHLY, ImmutableList.<Usage>builder().add(usage1).add(usage2).build(), catalogEffectiveDate);
    billingEvents.add(evt1);
    final DateTime dt2 = new DateTime(2013, 4, 23, 4, 34, 59, DateTimeZone.UTC);
    final BillingEvent evt2 = createMockBillingEvent(dt2, BillingPeriod.MONTHLY, ImmutableList.<Usage>builder().add(usage1).build(), catalogEffectiveDate);
    billingEvents.add(evt2);
    final DateTime dt3 = new DateTime(2013, 5, 23, 4, 34, 59, DateTimeZone.UTC);
    final BillingEvent evt3 = createMockBillingEvent(dt3, BillingPeriod.MONTHLY, ImmutableList.<Usage>builder().add(usage1).add(usage2).build(), catalogEffectiveDate);
    billingEvents.add(evt3);
    LocalDate targetDate = new LocalDate(2013, 6, 23);
    final SubscriptionUsageInArrear foo = new SubscriptionUsageInArrear(accountId, invoiceId, billingEvents, ImmutableList.<RawUsageRecord>of(), ImmutableSet.of(), targetDate, new LocalDate(dt1, DateTimeZone.UTC), usageDetailMode, invoiceConfig, internalCallContext);
    final List<ContiguousIntervalUsageInArrear> result = foo.computeInArrearUsageInterval(false);
    assertEquals(result.size(), 3);
    final ContiguousIntervalUsageInArrear firstResult = Iterables.tryFind(result, new Predicate<ContiguousIntervalUsageInArrear>() {

        @Override
        public boolean apply(final ContiguousIntervalUsageInArrear input) {
            return input.getUsage().getName().equals(usageName2) && input.transitionTimes.size() == 3;
        }
    }).get();
    assertEquals(firstResult.getUsage().getName(), usageName2);
    assertEquals(firstResult.getTransitionTimes().size(), 3);
    assertTrue(firstResult.getTransitionTimes().get(0).compareTo(new LocalDate(2013, 3, 23)) == 0);
    assertTrue(firstResult.getTransitionTimes().get(1).compareTo(new LocalDate(2013, 4, 15)) == 0);
    assertTrue(firstResult.getTransitionTimes().get(2).compareTo(new LocalDate(2013, 4, 23)) == 0);
    final ContiguousIntervalUsageInArrear secondResult = Iterables.tryFind(result, new Predicate<ContiguousIntervalUsageInArrear>() {

        @Override
        public boolean apply(final ContiguousIntervalUsageInArrear input) {
            return input.getUsage().getName().equals(usageName1);
        }
    }).get();
    assertEquals(secondResult.getUsage().getName(), usageName1);
    assertEquals(secondResult.getTransitionTimes().size(), 4);
    assertTrue(secondResult.getTransitionTimes().get(0).compareTo(new LocalDate(2013, 3, 23)) == 0);
    assertTrue(secondResult.getTransitionTimes().get(1).compareTo(new LocalDate(2013, 4, 15)) == 0);
    assertTrue(secondResult.getTransitionTimes().get(2).compareTo(new LocalDate(2013, 5, 15)) == 0);
    assertTrue(secondResult.getTransitionTimes().get(3).compareTo(new LocalDate(2013, 6, 15)) == 0);
    final ContiguousIntervalUsageInArrear thirdResult = Iterables.tryFind(result, new Predicate<ContiguousIntervalUsageInArrear>() {

        @Override
        public boolean apply(final ContiguousIntervalUsageInArrear input) {
            return input.getUsage().getName().equals(usageName2) && input.transitionTimes.size() == 2;
        }
    }).get();
    assertEquals(thirdResult.getUsage().getName(), usageName2);
    assertEquals(thirdResult.getTransitionTimes().size(), 2);
    assertTrue(thirdResult.getTransitionTimes().get(0).compareTo(new LocalDate(2013, 5, 23)) == 0);
    assertTrue(thirdResult.getTransitionTimes().get(1).compareTo(new LocalDate(2013, 6, 15)) == 0);
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Example 34 with Usage

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

the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsageWith_TOP_TIER.

@Test(groups = "fast")
public void testComputeBilledUsageWith_TOP_TIER() throws Exception {
    final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
    final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
    final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 1000, 100, BigDecimal.ONE);
    final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
    final DefaultTieredBlock block3 = createDefaultTieredBlock("unit", 1000, 100, new BigDecimal("0.5"));
    final DefaultTier tier3 = createDefaultTierWithBlocks(block3);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.TOP_TIER, tier1, tier2, tier3);
    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", 1000L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
    assertEquals(inputTier1.size(), 1);
    // 1000 units => (tier1) : 1000 / 100 + 1000 % 100 = 10
    assertEquals(inputTier1.get(0).getAmount(), new BigDecimal("10"));
    List<UsageConsumableInArrearTierUnitAggregate> inputTier2 = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 101000L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
    assertEquals(inputTier2.size(), 1);
    // 101000 units => (tier2) :  101000 / 1000 + 101000 % 1000 = 101 + 0 = 101
    assertEquals(inputTier2.get(0).getAmount(), new BigDecimal("101"));
    List<UsageConsumableInArrearTierUnitAggregate> inputTier3 = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 101001L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
    assertEquals(inputTier3.size(), 1);
    // 101001 units => (tier3) : 101001 / 1000 + 101001 % 1000 = 101 + 1 = 102 units => $51
    assertEquals(inputTier3.get(0).getAmount(), new BigDecimal("51.0"));
    // If we pass the maximum of the last tier, we price all units at the last tier
    List<UsageConsumableInArrearTierUnitAggregate> inputLastTier = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 300000L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
    assertEquals(inputLastTier.size(), 1);
    // 300000 units => (tier3) : 300000 / 1000 + 300000 % 1000 = 300 units => $150
    assertEquals(inputLastTier.get(0).getAmount(), new BigDecimal("150.0"));
}
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 35 with Usage

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

the class TestContiguousIntervalConsumableInArrear method testBilledUsage.

@Test(groups = "fast")
public void testBilledUsage() throws Exception {
    final DefaultTieredBlock block1 = createDefaultTieredBlock("cell-phone-minutes", 1000, 10000, new BigDecimal("0.5"));
    final DefaultTieredBlock block2 = createDefaultTieredBlock("Mbytes", 512, 512000, new BigDecimal("0.3"));
    final DefaultTier tier = createDefaultTierWithBlocks(block1, block2);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
    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));
    final List<UsageConsumableInArrearTierUnitAggregate> tierUnitDetails = Lists.newArrayList();
    tierUnitDetails.addAll(intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("cell-phone-minutes", 1000L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of()));
    tierUnitDetails.addAll(intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("Mbytes", 30720L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of()));
    assertEquals(tierUnitDetails.size(), 2);
    final UsageConsumableInArrearAggregate details = new UsageConsumableInArrearAggregate(tierUnitDetails);
    assertEquals(details.getAmount().compareTo(new BigDecimal("18.5")), 0);
}
Also used : UsageConsumableInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate) 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)

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