use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsageSizeOneWith_TOP_TIER.
@Test(groups = "fast")
public void testComputeBilledUsageSizeOneWith_TOP_TIER() throws Exception {
final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 1, 10, new BigDecimal("1.5"));
final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 1, 100, new BigDecimal("1.0"));
final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
final DefaultTieredBlock block3 = createDefaultTieredBlock("unit", 1, 1000, 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));
List<UsageConsumableInArrearTierUnitAggregate> result = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 111L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
assertEquals(result.size(), 1);
// 111 = 111 * 0.5 =
assertEquals(result.get(0).getAmount(), new BigDecimal("55.5"));
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testMultipleItemsAndTiersWithExistingItemsAllTiers_DETAIL.
@Test(groups = "fast")
public void testMultipleItemsAndTiersWithExistingItemsAllTiers_DETAIL() throws Exception {
//
// Create usage data points (will include already billed + add new usage data)
//
List<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
// tier 3
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 50L + /* already built */
20L, "tracking-1"));
// tier 2
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 80L + /* already built */
120L, "tracking-1"));
// FOO : 10 (tier 1) + 40 (tier 2) = 50
final UsageConsumableInArrearTierUnitAggregate existingFooUsageTier1 = new UsageConsumableInArrearTierUnitAggregate(1, "FOO", BigDecimal.ONE, 1L, 10L, new BigDecimal("10.00"));
final String usageInArrearDetail1 = objectMapper.writeValueAsString(existingFooUsageTier1);
final UsageConsumableInArrearTierUnitAggregate existingFooUsageTier2 = new UsageConsumableInArrearTierUnitAggregate(2, "FOO", BigDecimal.TEN, 1L, 40L, new BigDecimal("400.00"));
final String usageInArrearDetail2 = objectMapper.writeValueAsString(existingFooUsageTier2);
// BAR : 10 (tier 1) + 40 (tier 2)
final UsageConsumableInArrearTierUnitAggregate existingBarUsageTier1 = new UsageConsumableInArrearTierUnitAggregate(1, "BAR", new BigDecimal("2.00"), 1L, 80L, new BigDecimal("160.00"));
final String usageInArrearDetail3 = objectMapper.writeValueAsString(existingBarUsageTier1);
// Same as previous example bu instead of creating JSON we create one item per type/tier
final List<InvoiceItem> existingItems = new ArrayList<InvoiceItem>();
final InvoiceItem i1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, new LocalDate(2014, 03, 20), new LocalDate(2014, 04, 15), new BigDecimal("10.00"), /* amount */
new BigDecimal("1.00"), /* rate = tierPrice*/
currency, 10, /* # units*/
usageInArrearDetail1);
final InvoiceItem i2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, new LocalDate(2014, 03, 20), new LocalDate(2014, 04, 15), new BigDecimal("400.00"), new BigDecimal("10.00"), currency, 40, usageInArrearDetail2);
final InvoiceItem i3 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, new LocalDate(2014, 03, 20), new LocalDate(2014, 04, 15), new BigDecimal("160.00"), new BigDecimal("2.00"), currency, 80, usageInArrearDetail3);
existingItems.addAll(ImmutableList.<InvoiceItem>of(i1, i2, i3));
List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.ALL_TIERS, UsageDetailMode.DETAIL, existingItems);
assertEquals(result.size(), 4);
final UsageConsumableInArrearTierUnitAggregate resultUsageInArrearDetail0 = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearTierUnitAggregate>() {
});
assertEquals(resultUsageInArrearDetail0.getTierUnit(), "BAR");
assertEquals(resultUsageInArrearDetail0.getTier(), 1);
assertEquals(resultUsageInArrearDetail0.getQuantity().intValue(), 20);
assertEquals(resultUsageInArrearDetail0.getTierPrice().compareTo(new BigDecimal("2.00")), 0);
assertEquals(result.get(0).getRate().compareTo(new BigDecimal("2.00")), 0);
assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("40.00")), 0);
assertEquals(result.get(0).getQuantity().intValue(), 20);
final UsageConsumableInArrearTierUnitAggregate resultUsageInArrearDetail1 = objectMapper.readValue(result.get(1).getItemDetails(), new TypeReference<UsageConsumableInArrearTierUnitAggregate>() {
});
assertEquals(resultUsageInArrearDetail1.getTierUnit(), "BAR");
assertEquals(resultUsageInArrearDetail1.getTier(), 2);
assertEquals(resultUsageInArrearDetail1.getQuantity().intValue(), 100);
assertEquals(resultUsageInArrearDetail1.getTierPrice().compareTo(new BigDecimal("20.00")), 0);
assertEquals(result.get(1).getRate().compareTo(new BigDecimal("20.00")), 0);
assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("2000.00")), 0);
assertEquals(result.get(1).getQuantity().intValue(), 100);
final UsageConsumableInArrearTierUnitAggregate resultUsageInArrearDetail2 = objectMapper.readValue(result.get(2).getItemDetails(), new TypeReference<UsageConsumableInArrearTierUnitAggregate>() {
});
assertEquals(resultUsageInArrearDetail2.getTierUnit(), "FOO");
assertEquals(resultUsageInArrearDetail2.getTier(), 2);
assertEquals(resultUsageInArrearDetail2.getQuantity().intValue(), 10);
assertEquals(resultUsageInArrearDetail2.getTierPrice().compareTo(new BigDecimal("10.00")), 0);
assertEquals(result.get(2).getRate().compareTo(new BigDecimal("10.00")), 0);
assertEquals(result.get(2).getAmount().compareTo(new BigDecimal("100.00")), 0);
assertEquals(result.get(2).getQuantity().intValue(), 10);
final UsageConsumableInArrearTierUnitAggregate resultUsageInArrearDetail3 = objectMapper.readValue(result.get(3).getItemDetails(), new TypeReference<UsageConsumableInArrearTierUnitAggregate>() {
});
assertEquals(resultUsageInArrearDetail3.getTierUnit(), "FOO");
assertEquals(resultUsageInArrearDetail3.getTier(), 3);
assertEquals(resultUsageInArrearDetail3.getQuantity().intValue(), 10);
assertEquals(resultUsageInArrearDetail3.getTierPrice().compareTo(new BigDecimal("100.00")), 0);
assertEquals(result.get(3).getRate().compareTo(new BigDecimal("100.00")), 0);
assertEquals(result.get(3).getAmount().compareTo(new BigDecimal("1000.00")), 0);
assertEquals(result.get(3).getQuantity().intValue(), 10);
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsageWith_ALL_TIERS.
@Test(groups = "fast")
public void testComputeBilledUsageWith_ALL_TIERS() 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 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 testComputeBilledUsageSizeOneWith_ALL_TIERS.
@Test(groups = "fast")
public void testComputeBilledUsageSizeOneWith_ALL_TIERS() throws Exception {
final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 1, 10, new BigDecimal("1.5"));
final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 1, 100, new BigDecimal("1.0"));
final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
final DefaultTieredBlock block3 = createDefaultTieredBlock("unit", 1, 1000, new BigDecimal("0.5"));
final DefaultTier tier3 = createDefaultTierWithBlocks(block3);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, 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));
List<UsageConsumableInArrearTierUnitAggregate> result = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 111L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
assertEquals(result.size(), 3);
// 111 = 10 (tier1) + 100 (tier2) + 1 (tier3) => 10 * 1.5 + 100 * 1 + 1 * 0.5 = 115.5
assertEquals(result.get(0).getAmount(), new BigDecimal("15.0"));
assertEquals(result.get(1).getAmount(), new BigDecimal("100.0"));
assertEquals(result.get(2).getAmount(), new BigDecimal("0.5"));
}
use of org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testMultipleItemsAndTiersWithExistingItemsAllTiers_AGGREGATE.
@Test(groups = "fast")
public void testMultipleItemsAndTiersWithExistingItemsAllTiers_AGGREGATE() throws Exception {
//
// Let's assume we were already billed on the previous period
//
// FOO : 10 (tier 1) + 40 (tier 2) = 50
final UsageConsumableInArrearTierUnitAggregate existingFooUsageTier1 = new UsageConsumableInArrearTierUnitAggregate(1, "FOO", BigDecimal.ONE, 1L, 10L, new BigDecimal("10.00"));
final UsageConsumableInArrearTierUnitAggregate existingFooUsageTier2 = new UsageConsumableInArrearTierUnitAggregate(2, "FOO", BigDecimal.TEN, 1L, 40L, new BigDecimal("400.00"));
// BAR : 10 (tier 1) + 40 (tier 2)
final UsageConsumableInArrearTierUnitAggregate existingBarUsageTier1 = new UsageConsumableInArrearTierUnitAggregate(1, "BAR", new BigDecimal("2.00"), 1L, 80L, new BigDecimal("160.00"));
final List<UsageConsumableInArrearTierUnitAggregate> existingUsage = ImmutableList.of(existingFooUsageTier1, existingFooUsageTier2, existingBarUsageTier1);
final UsageConsumableInArrearAggregate usageConsumableInArrearDetail = new UsageConsumableInArrearAggregate(existingUsage);
final String existingUsageJson = objectMapper.writeValueAsString(usageConsumableInArrearDetail);
//
// Create usage data points (will include already billed + add new usage data)
//
List<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
// tier 3
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 50L + /* already built */
20L, "tracking-1"));
// tier 2
rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 80L + /* already built */
120L, "tracking-1"));
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);
List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.ALL_TIERS, UsageDetailMode.AGGREGATE, existingItems);
assertEquals(result.size(), 1);
assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("3140.00")), 0, String.format("%s != 3140.0", result.get(0).getAmount()));
UsageConsumableInArrearAggregate usageDetail = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {
});
List<UsageConsumableInArrearTierUnitAggregate> itemDetails = usageDetail.getTierDetails();
// We get same total than AGGREGATE : 3140
// BAR item detail
assertEquals(itemDetails.get(0).getTierUnit(), "BAR");
assertEquals(itemDetails.get(0).getTier(), 1);
assertEquals(itemDetails.get(0).getTierBlockSize(), 1);
assertEquals(itemDetails.get(0).getQuantity().intValue(), 20);
assertEquals(itemDetails.get(0).getTierPrice().compareTo(new BigDecimal("2.00")), 0);
assertEquals(itemDetails.get(0).getAmount().compareTo(new BigDecimal("40.00")), 0);
assertEquals(itemDetails.get(1).getTierUnit(), "BAR");
assertEquals(itemDetails.get(1).getTier(), 2);
assertEquals(itemDetails.get(1).getTierBlockSize(), 1);
assertEquals(itemDetails.get(1).getQuantity().intValue(), 100);
assertEquals(itemDetails.get(1).getTierPrice().compareTo(new BigDecimal("20.00")), 0);
assertEquals(itemDetails.get(1).getAmount().compareTo(new BigDecimal("2000.00")), 0);
// FOO item detail
assertEquals(itemDetails.get(2).getTierUnit(), "FOO");
assertEquals(itemDetails.get(2).getTier(), 1);
assertEquals(itemDetails.get(2).getTierBlockSize(), 1);
assertEquals(itemDetails.get(2).getQuantity().intValue(), 0);
assertEquals(itemDetails.get(2).getTierPrice().compareTo(new BigDecimal("1.00")), 0);
assertEquals(itemDetails.get(2).getAmount().compareTo(new BigDecimal("0.00")), 0);
assertEquals(itemDetails.get(3).getTierUnit(), "FOO");
assertEquals(itemDetails.get(3).getTier(), 2);
assertEquals(itemDetails.get(3).getTierBlockSize(), 1);
assertEquals(itemDetails.get(3).getQuantity().intValue(), 10);
assertEquals(itemDetails.get(3).getTierPrice().compareTo(new BigDecimal("10.00")), 0);
assertEquals(itemDetails.get(3).getAmount().compareTo(new BigDecimal("100.00")), 0);
assertEquals(itemDetails.get(4).getTierUnit(), "FOO");
assertEquals(itemDetails.get(4).getTier(), 3);
assertEquals(itemDetails.get(4).getTierBlockSize(), 1);
assertEquals(itemDetails.get(4).getQuantity().intValue(), 10);
assertEquals(itemDetails.get(4).getTierPrice().compareTo(new BigDecimal("100.00")), 0);
assertEquals(itemDetails.get(4).getAmount().compareTo(new BigDecimal("1000.00")), 0);
}
Aggregations