use of org.killbill.billing.catalog.DefaultTieredBlock 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));
}
use of org.killbill.billing.catalog.DefaultTieredBlock 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);
}
use of org.killbill.billing.catalog.DefaultTieredBlock 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);
}
use of org.killbill.billing.catalog.DefaultTieredBlock 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);
}
use of org.killbill.billing.catalog.DefaultTieredBlock in project killbill by killbill.
the class TestUsageInArrearBase method createDefaultTieredBlock.
protected DefaultTieredBlock createDefaultTieredBlock(final String unit, final int size, final int max, final BigDecimal btcPrice) {
final DefaultTieredBlock block = new DefaultTieredBlock();
block.setUnit(new DefaultUnit().setName(unit));
block.setSize(new Double(size));
final DefaultPrice[] prices = new DefaultPrice[1];
prices[0] = new DefaultPrice();
prices[0].setCurrency(Currency.BTC).setValue(btcPrice);
block.setPrice(new DefaultInternationalPrice().setPrices(prices));
block.setMax(new Double(max));
return block;
}
Aggregations