Search in sources :

Example 16 with Usage

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

the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsage.

@Test(groups = "fast")
public void testComputeBilledUsage() throws CatalogApiException {
    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 DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier1, tier2);
    final LocalDate targetDate = new LocalDate(2014, 03, 20);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsage>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList()));
    final BigDecimal result = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(new DefaultRolledUpUnit("unit", 5325L));
    // 5000 = 1000 (tier1) + 4325 (tier2) => 10 + 5 = 15
    assertEquals(result, new BigDecimal("15"));
}
Also used : DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RawUsage(org.killbill.billing.usage.RawUsage) 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 17 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() {
    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, tier);
    final LocalDate targetDate = startDate.plusDays(1);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsage>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList()));
    final List<InvoiceItem> existingUsage = Lists.newArrayList();
    final UsageInvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii1);
    final UsageInvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), 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, planName, phaseName, usage.getName(), 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, planName, phaseName, "other", startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii4);
    // Will be ignored because non usage item
    final FixedPriceInvoiceItem ii5 = new FixedPriceInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, 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) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RawUsage(org.killbill.billing.usage.RawUsage) 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 18 with Usage

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

the class TestRawUsageOptimizer method testWithOneMonthlyAndOneNonActiveAnnualUsageSectionAndEnoughUsageItems.

@Test(groups = "fast")
public void testWithOneMonthlyAndOneNonActiveAnnualUsageSectionAndEnoughUsageItems() {
    final LocalDate firstEventStartDate = new LocalDate(2014, 03, 15);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    for (int i = 0; i < 5; i++) {
        invoiceItems.add(createUsageItem(firstEventStartDate.plusMonths(i)));
    }
    final LocalDate targetDate = invoiceItems.get(invoiceItems.size() - 1).getEndDate();
    final Map<String, Usage> knownUsage = new HashMap<String, Usage>();
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    knownUsage.put(usageName, usage);
    final DefaultTieredBlock block2 = createDefaultTieredBlock("unit2", 10, 10000, BigDecimal.TEN);
    final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
    final DefaultUsage usage2 = createConsumableInArrearUsage("usageName2", BillingPeriod.ANNUAL, tier2);
    knownUsage.put("usageName2", usage2);
    final LocalDate result = rawUsageOptimizer.getOptimizedRawUsageStartDate(firstEventStartDate, targetDate, invoiceItems, knownUsage, internalCallContext);
    // The same reasoning applies as previously because there is no usage items against the annual and
    // so, the largest endDate for ii is 2014-08-15, and by default org.killbill.invoice.readMaxRawUsagePreviousPeriod == 2 => targetDate =>  2014-06-15
    Assert.assertEquals(result.compareTo(new LocalDate(2014, 06, 15)), 0, "142 got " + result);
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 19 with Usage

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

the class TestRawUsageOptimizer method testWithNoItems.

@Test(groups = "fast")
public void testWithNoItems() {
    final LocalDate firstEventStartDate = new LocalDate(2014, 03, 15);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final Map<String, Usage> knownUsage = new HashMap<String, Usage>();
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    knownUsage.put(usageName, usage);
    final LocalDate result = rawUsageOptimizer.getOptimizedRawUsageStartDate(firstEventStartDate, firstEventStartDate.plusDays(1), invoiceItems, knownUsage, internalCallContext);
    Assert.assertEquals(result.compareTo(firstEventStartDate), 0);
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 20 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() {
    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, 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, 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());
    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());
    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());
    billingEvents.add(evt3);
    LocalDate targetDate = new LocalDate(2013, 6, 23);
    final SubscriptionUsageInArrear foo = new SubscriptionUsageInArrear(accountId, invoiceId, billingEvents, ImmutableList.<RawUsage>of(), targetDate, new LocalDate(dt1, DateTimeZone.UTC), internalCallContext);
    final List<ContiguousIntervalUsageInArrear> result = foo.computeInArrearUsageInterval();
    assertEquals(result.size(), 3);
    assertEquals(result.get(0).getUsage().getName(), usageName2);
    assertEquals(result.get(0).getTransitionTimes().size(), 3);
    assertTrue(result.get(0).getTransitionTimes().get(0).compareTo(new LocalDate(2013, 3, 23)) == 0);
    assertTrue(result.get(0).getTransitionTimes().get(1).compareTo(new LocalDate(2013, 4, 15)) == 0);
    assertTrue(result.get(0).getTransitionTimes().get(2).compareTo(new LocalDate(2013, 4, 23)) == 0);
    assertEquals(result.get(1).getUsage().getName(), usageName1);
    assertEquals(result.get(1).getTransitionTimes().size(), 4);
    assertTrue(result.get(1).getTransitionTimes().get(0).compareTo(new LocalDate(2013, 3, 23)) == 0);
    assertTrue(result.get(1).getTransitionTimes().get(1).compareTo(new LocalDate(2013, 4, 15)) == 0);
    assertTrue(result.get(1).getTransitionTimes().get(2).compareTo(new LocalDate(2013, 5, 15)) == 0);
    assertTrue(result.get(1).getTransitionTimes().get(3).compareTo(new LocalDate(2013, 6, 15)) == 0);
    assertEquals(result.get(2).getUsage().getName(), usageName2);
    assertEquals(result.get(2).getTransitionTimes().size(), 2);
    assertTrue(result.get(2).getTransitionTimes().get(0).compareTo(new LocalDate(2013, 5, 23)) == 0);
    assertTrue(result.get(2).getTransitionTimes().get(1).compareTo(new LocalDate(2013, 6, 15)) == 0);
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) RawUsage(org.killbill.billing.usage.RawUsage) 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) Test(org.testng.annotations.Test)

Aggregations

Usage (org.killbill.billing.catalog.api.Usage)21 Test (org.testng.annotations.Test)14 LocalDate (org.joda.time.LocalDate)11 BigDecimal (java.math.BigDecimal)9 DefaultTier (org.killbill.billing.catalog.DefaultTier)9 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)9 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)8 HashMap (java.util.HashMap)7 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)7 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)7 RawUsage (org.killbill.billing.usage.RawUsage)7 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)6 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)5 BillingEvent (org.killbill.billing.junction.BillingEvent)5 Function (com.google.common.base.Function)4 ArrayList (java.util.ArrayList)4 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)4 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2