Search in sources :

Example 6 with RawUsage

use of org.killbill.billing.usage.RawUsage 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 7 with RawUsage

use of org.killbill.billing.usage.RawUsage 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 8 with RawUsage

use of org.killbill.billing.usage.RawUsage in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testComputeMissingItems.

@Test(groups = "fast")
public void testComputeMissingItems() throws CatalogApiException {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate firstBCDDate = new LocalDate(2014, 04, 15);
    final LocalDate endDate = new LocalDate(2014, 05, 15);
    // 2 items for startDate - firstBCDDate
    final List<RawUsage> rawUsages = new ArrayList<RawUsage>();
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "unit", 130L));
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "unit", 271L));
    // 1 items for firstBCDDate - endDate
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit", 199L));
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = endDate;
    final BillingEvent event1 = createMockBillingEvent(startDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final BillingEvent event2 = createMockBillingEvent(endDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsages, targetDate, true, event1, event2);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, firstBCDDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii1);
    final InvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), firstBCDDate, endDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii2);
    final UsageInArrearItemsAndNextNotificationDate usageResult = intervalConsumableInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
    final List<InvoiceItem> rawResults = usageResult.getInvoiceItems();
    assertEquals(rawResults.size(), 4);
    final List<InvoiceItem> result = ImmutableList.copyOf(Iterables.filter(rawResults, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getAmount().compareTo(BigDecimal.ZERO) > 0;
        }
    }));
    // Invoiced for 1 BTC and used 130 + 271 = 401 => 5 blocks => 5 BTC so remaining piece should be 4 BTC
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("4.0")), 0, String.format("%s != 4.0", result.get(0).getAmount()));
    assertEquals(result.get(0).getCurrency(), Currency.BTC);
    assertEquals(result.get(0).getAccountId(), accountId);
    assertEquals(result.get(0).getBundleId(), bundleId);
    assertEquals(result.get(0).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(0).getPlanName(), planName);
    assertEquals(result.get(0).getPhaseName(), phaseName);
    assertEquals(result.get(0).getUsageName(), usage.getName());
    assertTrue(result.get(0).getStartDate().compareTo(startDate) == 0);
    assertTrue(result.get(0).getEndDate().compareTo(firstBCDDate) == 0);
    // Invoiced for 1 BTC and used 199  => 2 blocks => 2 BTC so remaining piece should be 1 BTC
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("1.0")), 0, String.format("%s != 1.0", result.get(0).getAmount()));
    assertEquals(result.get(1).getCurrency(), Currency.BTC);
    assertEquals(result.get(1).getAccountId(), accountId);
    assertEquals(result.get(1).getBundleId(), bundleId);
    assertEquals(result.get(1).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(1).getPlanName(), planName);
    assertEquals(result.get(1).getPhaseName(), phaseName);
    assertEquals(result.get(1).getUsageName(), usage.getName());
    assertTrue(result.get(1).getStartDate().compareTo(firstBCDDate) == 0);
    assertTrue(result.get(1).getEndDate().compareTo(endDate) == 0);
}
Also used : 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) ArrayList(java.util.ArrayList) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RawUsage(org.killbill.billing.usage.RawUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate) UsageInArrearItemsAndNextNotificationDate(org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 9 with RawUsage

use of org.killbill.billing.usage.RawUsage in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testGetRolledUpUsage.

@Test(groups = "fast")
public void testGetRolledUpUsage() {
    final DefaultTieredBlock tieredBlock1 = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTieredBlock tieredBlock2 = createDefaultTieredBlock("unit2", 10, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(tieredBlock1, tieredBlock2);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate t0 = new LocalDate(2015, 03, BCD);
    final BillingEvent eventT0 = createMockBillingEvent(t0.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final LocalDate t1 = new LocalDate(2015, 04, BCD);
    final BillingEvent eventT1 = createMockBillingEvent(t1.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final LocalDate t2 = new LocalDate(2015, 05, BCD);
    final BillingEvent eventT2 = createMockBillingEvent(t2.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final LocalDate t3 = new LocalDate(2015, 06, BCD);
    final BillingEvent eventT3 = createMockBillingEvent(t3.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final LocalDate targetDate = t3;
    // Prev t0
    final RawUsage raw1 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 03, 01), "unit", 12L);
    // t0 - t1
    final RawUsage raw2 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 03, 15), "unit", 6L);
    final RawUsage raw3 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 03, 25), "unit", 4L);
    // t1 - t2 nothing
    // t2 - t3
    final RawUsage raw4 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, 15), "unit", 13L);
    final RawUsage oraw1 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, 21), "unit2", 21L);
    final RawUsage raw5 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, 31), "unit", 7L);
    // after t3
    final RawUsage raw6 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 06, 15), "unit", 100L);
    final List<RawUsage> rawUsage = ImmutableList.of(raw1, raw2, raw3, raw4, oraw1, raw5, raw6);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsage, targetDate, true, eventT0, eventT1, eventT2, eventT3);
    final List<RolledUpUsage> unsortedRolledUpUsage = intervalConsumableInArrear.getRolledUpUsage();
    Assert.assertEquals(unsortedRolledUpUsage.size(), 2);
    final List<RolledUpUsage> rolledUpUsage = TEST_ROLLED_UP_FIRST_USAGE_ORDERING.sortedCopy(unsortedRolledUpUsage);
    Assert.assertEquals(rolledUpUsage.get(0).getStart().compareTo(t0), 0);
    Assert.assertEquals(rolledUpUsage.get(0).getEnd().compareTo(t1), 0);
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().size(), 1);
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(0).getUnitType(), "unit");
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(0).getAmount(), new Long(10L));
    Assert.assertEquals(rolledUpUsage.get(1).getStart().compareTo(t2), 0);
    Assert.assertEquals(rolledUpUsage.get(1).getEnd().compareTo(t3), 0);
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().size(), 2);
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(0).getUnitType(), "unit");
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(0).getAmount(), new Long(20L));
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(1).getUnitType(), "unit2");
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(1).getAmount(), new Long(21L));
}
Also used : RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RawUsage(org.killbill.billing.usage.RawUsage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 10 with RawUsage

use of org.killbill.billing.usage.RawUsage 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

LocalDate (org.joda.time.LocalDate)10 RawUsage (org.killbill.billing.usage.RawUsage)10 DefaultTier (org.killbill.billing.catalog.DefaultTier)8 Test (org.testng.annotations.Test)8 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)7 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)7 BigDecimal (java.math.BigDecimal)6 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)6 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)5 Usage (org.killbill.billing.catalog.api.Usage)5 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)4 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)4 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)4 BillingEvent (org.killbill.billing.junction.BillingEvent)4 ArrayList (java.util.ArrayList)3 DefaultLimit (org.killbill.billing.catalog.DefaultLimit)3 DefaultUnit (org.killbill.billing.catalog.DefaultUnit)3 Predicate (com.google.common.base.Predicate)2 UsageInArrearItemsAndNextNotificationDate (org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate)2 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)2