Search in sources :

Example 16 with UsageConsumableInArrearTierUnitAggregate

use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testComputeMissingItemsAggregateModeTopTier_AGGREGATE.

@Test(groups = "fast")
public void testComputeMissingItemsAggregateModeTopTier_AGGREGATE() throws Exception {
    // Case 1
    List<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 5L, "tracking-1"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 99L, "tracking-1"));
    List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.TOP_TIER, UsageDetailMode.AGGREGATE, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("203")), 0);
    UsageConsumableInArrearAggregate usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
    });
    List<UsageConsumableInArrearTierUnitAggregate> itemDetails = usageDetail.getTierDetails();
    // BAR: 99 * 2 = 198
    assertEquals(itemDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemDetails.get(0).getTier(), 1);
    assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("198")), 0);
    assertEquals(itemDetails.get(0).getQuantity().intValue(), 99);
    assertEquals(itemDetails.get(0).getTierPrice().compareTo(new BigDecimal("2.0")), 0);
    // FOO: 5 * 1 = 5
    assertEquals(itemDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemDetails.get(1).getTier(), 1);
    assertEquals(itemDetails.get(1).getAmount().compareTo(new BigDecimal("5")), 0);
    assertEquals(itemDetails.get(1).getQuantity().intValue(), 5);
    assertEquals(itemDetails.get(1).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // Case 2
    rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 5L, "tracking-2"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-2"));
    result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.TOP_TIER, UsageDetailMode.AGGREGATE, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("2025")), 0);
    usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
    });
    itemDetails = usageDetail.getTierDetails();
    // BAR: 101 * 20 = 2020
    assertEquals(itemDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemDetails.get(0).getTier(), 2);
    assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("2020.0")), 0);
    assertEquals(itemDetails.get(0).getQuantity().intValue(), 101);
    assertEquals(itemDetails.get(0).getTierPrice().compareTo(new BigDecimal("20.0")), 0);
    // FOO: 5 * 1 = 5
    assertEquals(itemDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemDetails.get(1).getTier(), 1);
    assertEquals(itemDetails.get(1).getAmount().compareTo(new BigDecimal("5")), 0);
    assertEquals(itemDetails.get(1).getQuantity().intValue(), 5);
    assertEquals(itemDetails.get(1).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // Case 3
    rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 76L, "tracking-3"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-3"));
    result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.TOP_TIER, UsageDetailMode.AGGREGATE, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("9620")), 0);
    usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
    });
    itemDetails = usageDetail.getTierDetails();
    // BAR: 101 * 20 = 2020
    assertEquals(itemDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemDetails.get(0).getTier(), 2);
    assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("2020.0")), 0);
    assertEquals(itemDetails.get(0).getQuantity().intValue(), 101);
    assertEquals(itemDetails.get(0).getTierPrice().compareTo(new BigDecimal("20.0")), 0);
    // FOO: 76 * 100 = 7500
    assertEquals(itemDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemDetails.get(1).getTier(), 3);
    assertEquals(itemDetails.get(1).getAmount().compareTo(new BigDecimal("7600")), 0);
    assertEquals(itemDetails.get(1).getQuantity().intValue(), 76);
    assertEquals(itemDetails.get(1).getTierPrice().compareTo(new BigDecimal("100.0")), 0);
}
Also used : UsageConsumableInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) UsageConsumableInArrearTierUnitAggregate(org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.testng.annotations.Test)

Example 17 with UsageConsumableInArrearTierUnitAggregate

use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate 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)

Example 18 with UsageConsumableInArrearTierUnitAggregate

use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate 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)

Aggregations

UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)18 BigDecimal (java.math.BigDecimal)14 LocalDate (org.joda.time.LocalDate)13 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)13 Test (org.testng.annotations.Test)13 DefaultTier (org.killbill.billing.catalog.DefaultTier)9 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)9 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)9 UsageConsumableInArrearAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate)9 ArrayList (java.util.ArrayList)8 Usage (org.killbill.billing.catalog.api.Usage)8 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)8 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)8 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)6 RawUsageRecord (org.killbill.billing.usage.api.RawUsageRecord)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 TieredBlock (org.killbill.billing.catalog.api.TieredBlock)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1