use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testBilledDetailsForUnitType.
@Test(groups = "fast")
public void testBilledDetailsForUnitType() throws Exception {
final LocalDate startDate = new LocalDate(2014, 03, 20);
final LocalDate targetDate = startDate.plusDays(1);
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 ContiguousIntervalConsumableUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
final UsageConsumableInArrearTierUnitAggregate detail1 = new UsageConsumableInArrearTierUnitAggregate(3, "FOO", new BigDecimal("0.50"), 1L, 700L);
final UsageConsumableInArrearTierUnitAggregate detail2 = new UsageConsumableInArrearTierUnitAggregate(2, "FOO", BigDecimal.ONE, 1L, 500L);
final UsageConsumableInArrearTierUnitAggregate detail3 = new UsageConsumableInArrearTierUnitAggregate(1, "FOO", BigDecimal.TEN, 1L, 10L);
final UsageConsumableInArrearTierUnitAggregate detail4 = new UsageConsumableInArrearTierUnitAggregate(2, "FOO", BigDecimal.ONE, 1L, 50L);
final UsageConsumableInArrearTierUnitAggregate detail5 = new UsageConsumableInArrearTierUnitAggregate(1, "FOO", BigDecimal.TEN, 1L, 100L);
final List<UsageConsumableInArrearTierUnitAggregate> existingUsage = ImmutableList.of(detail1, detail2, detail3, detail4, detail5);
final UsageConsumableInArrearAggregate usageConsumableInArrearDetail = new UsageConsumableInArrearAggregate(existingUsage);
final String existingUsageJson = objectMapper.writeValueAsString(usageConsumableInArrearDetail);
final List<InvoiceItem> existingItems = new ArrayList<InvoiceItem>();
final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, new LocalDate(2014, 03, 20), new LocalDate(2014, 04, 15), new BigDecimal("570.00"), null, currency, null, existingUsageJson);
existingItems.add(ii1);
final List<UsageConsumableInArrearTierUnitAggregate> aggregateDetails = intervalConsumableInArrear.getBilledDetailsForUnitType(existingItems, "FOO");
assertEquals(aggregateDetails.size(), 3);
assertEquals(aggregateDetails.get(0).getTier(), 1);
assertEquals(aggregateDetails.get(0).getQuantity().intValue(), 110);
assertEquals(aggregateDetails.get(1).getTier(), 2);
assertEquals(aggregateDetails.get(1).getQuantity().intValue(), 550);
assertEquals(aggregateDetails.get(2).getTier(), 3);
assertEquals(aggregateDetails.get(2).getQuantity().intValue(), 700);
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsageWithUnlimitedMaxWith_ALL_TIERS.
@Test(groups = "fast")
public void testComputeBilledUsageWithUnlimitedMaxWith_ALL_TIERS() throws Exception {
final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 1000, -1, BigDecimal.ONE);
final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, 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));
List<UsageConsumableInArrearTierUnitAggregate> result = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 5325L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
assertEquals(result.size(), 2);
// 5000 = 1000 (tier1) + 4325 (tier2) => 10 + 5 = 15
assertEquals(result.get(0).getAmount(), new BigDecimal("10"));
assertEquals(result.get(1).getAmount(), new BigDecimal("5"));
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeMissingItemsAggregateModeAllTier_AGGREGATE.
@Test(groups = "fast")
public void testComputeMissingItemsAggregateModeAllTier_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.ALL_TIERS, 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.ALL_TIERS, UsageDetailMode.AGGREGATE, ImmutableList.<InvoiceItem>of());
assertEquals(result.size(), 1);
assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("225")), 0);
usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
});
itemDetails = usageDetail.getTierDetails();
// BAR: 100 * 2 = 200
assertEquals(itemDetails.get(0).getTierUnit(), "BAR");
assertEquals(itemDetails.get(0).getTier(), 1);
assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("200.0")), 0);
assertEquals(itemDetails.get(0).getQuantity().intValue(), 100);
assertEquals(itemDetails.get(0).getTierPrice().compareTo(new BigDecimal("2.0")), 0);
// BAR: 1 * 20 = 20
assertEquals(itemDetails.get(1).getTierUnit(), "BAR");
assertEquals(itemDetails.get(1).getTier(), 2);
assertEquals(itemDetails.get(1).getAmount().compareTo(new BigDecimal("20.0")), 0);
assertEquals(itemDetails.get(1).getQuantity().intValue(), 1);
assertEquals(itemDetails.get(1).getTierPrice().compareTo(new BigDecimal("20.0")), 0);
// FOO: 5 * 1 = 5
assertEquals(itemDetails.get(2).getTierUnit(), "FOO");
assertEquals(itemDetails.get(2).getTier(), 1);
assertEquals(itemDetails.get(2).getAmount().compareTo(new BigDecimal("5")), 0);
assertEquals(itemDetails.get(2).getQuantity().intValue(), 5);
assertEquals(itemDetails.get(2).getTierPrice().compareTo(BigDecimal.ONE), 0);
// Case 3
rawUsageRecords = new ArrayList<RawUsageRecord>();
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 75L, "tracking-3"));
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-4"));
result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.ALL_TIERS, UsageDetailMode.AGGREGATE, ImmutableList.<InvoiceItem>of());
assertEquals(result.size(), 1);
assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("2230")), 0);
usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
});
itemDetails = usageDetail.getTierDetails();
// BAR: 100 * 2 = 200
assertEquals(itemDetails.get(0).getTierUnit(), "BAR");
assertEquals(itemDetails.get(0).getTier(), 1);
assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("200.0")), 0);
assertEquals(itemDetails.get(0).getQuantity().intValue(), 100);
assertEquals(itemDetails.get(0).getTierPrice().compareTo(new BigDecimal("2.0")), 0);
// BAR: 1 * 20 = 20
assertEquals(itemDetails.get(1).getTierUnit(), "BAR");
assertEquals(itemDetails.get(1).getTier(), 2);
assertEquals(itemDetails.get(1).getAmount().compareTo(new BigDecimal("20.0")), 0);
assertEquals(itemDetails.get(1).getQuantity().intValue(), 1);
assertEquals(itemDetails.get(1).getTierPrice().compareTo(new BigDecimal("20.0")), 0);
// FOO: 10 * 1 = 10
assertEquals(itemDetails.get(2).getTierUnit(), "FOO");
assertEquals(itemDetails.get(2).getTier(), 1);
assertEquals(itemDetails.get(2).getAmount().compareTo(BigDecimal.TEN), 0);
assertEquals(itemDetails.get(2).getQuantity().intValue(), 10);
assertEquals(itemDetails.get(2).getTierPrice().compareTo(BigDecimal.ONE), 0);
// FOO: 50 * 10 = 500
assertEquals(itemDetails.get(3).getTierUnit(), "FOO");
assertEquals(itemDetails.get(3).getTier(), 2);
assertEquals(itemDetails.get(3).getAmount().compareTo(new BigDecimal("500")), 0);
assertEquals(itemDetails.get(3).getQuantity().intValue(), 50);
assertEquals(itemDetails.get(3).getTierPrice().compareTo(BigDecimal.TEN), 0);
// FOO: 15 * 100 = 1500
assertEquals(itemDetails.get(4).getTierUnit(), "FOO");
assertEquals(itemDetails.get(4).getTier(), 3);
assertEquals(itemDetails.get(4).getAmount().compareTo(new BigDecimal("1500")), 0);
assertEquals(itemDetails.get(4).getQuantity().intValue(), 15);
assertEquals(itemDetails.get(4).getTierPrice().compareTo(new BigDecimal("100.0")), 0);
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeMissingItems.
@Test(groups = "fast")
public void testComputeMissingItems() throws Exception {
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<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "unit", 130L, "tracking-1"));
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "unit", 271L, "tracking-1"));
// 1 items for firstBCDDate - endDate
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit", 199L, "tracking-2"));
final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
final DefaultTier tier = createDefaultTierWithBlocks(block);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
final LocalDate targetDate = endDate;
final BillingEvent event1 = createMockBillingEvent(startDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
final BillingEvent event2 = createMockBillingEvent(endDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsageRecords, targetDate, true, event1, event2);
final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, startDate, firstBCDDate, BigDecimal.ONE, currency);
invoiceItems.add(ii1);
final InvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, firstBCDDate, endDate, BigDecimal.ONE, currency);
invoiceItems.add(ii2);
final UsageInArrearItemsAndNextNotificationDate usageResult = intervalConsumableInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
checkTrackingIds(rawUsageRecords, usageResult.getTrackingIds());
final List<InvoiceItem> result = usageResult.getInvoiceItems();
assertEquals(result.size(), 2);
// Invoiced for 1 USD and used 130 + 271 = 401 => 5 blocks => 5 USD so remaining piece should be 4 USD
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.USD);
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);
assertNotNull(result.get(0).getItemDetails());
UsageConsumableInArrearAggregate usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
});
List<UsageConsumableInArrearTierUnitAggregate> itemDetails = usageDetail.getTierDetails();
assertEquals(itemDetails.size(), 1);
// Because we did not have the details before, the new details don't take into account the
assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("5.0")), 0);
// Invoiced for 1 USD and used 199 => 2 blocks => 2 USD so remaining piece should be 1 USD
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.USD);
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);
assertNotNull(result.get(1).getItemDetails());
usageDetail = objectMapper.readValue(result.get(1).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
});
itemDetails = usageDetail.getTierDetails();
assertEquals(itemDetails.size(), 1);
assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("2.0")), 0);
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate 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"));
}
Aggregations