use of org.killbill.billing.catalog.api.Usage 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"));
}
use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeToBeBilledUsage.
@Test(groups = "fast")
public void testComputeToBeBilledUsage() throws Exception {
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, TierBlockPolicy.ALL_TIERS, tier);
final LocalDate targetDate = startDate.plusDays(1);
final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
final List<InvoiceItem> existingUsage = Lists.newArrayList();
final UsageInvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, startDate, endDate, BigDecimal.TEN, currency);
existingUsage.add(ii1);
final UsageInvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, 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, productName, planName, phaseName, usage.getName(), null, 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, productName, planName, phaseName, "other", null, startDate, endDate, BigDecimal.TEN, currency);
existingUsage.add(ii4);
// Will be ignored because non usage item
final FixedPriceInvoiceItem ii5 = new FixedPriceInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, 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);
}
use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class DefaultBillingEventSet method getUsages.
@Override
public Map<String, Usage> getUsages() {
final Iterable<Usage> allUsages = Iterables.concat(Iterables.transform(this, new Function<BillingEvent, List<Usage>>() {
@Override
public List<Usage> apply(final BillingEvent input) {
try {
return input.getUsages();
} catch (final CatalogApiException e) {
throw new IllegalStateException(String.format("Failed to retrieve usage section for billing event %s", input), e);
}
}
}));
if (!allUsages.iterator().hasNext()) {
return Collections.emptyMap();
}
final Map<String, Usage> result = new HashMap<String, Usage>();
for (Usage cur : Sets.<Usage>newHashSet(allUsages)) {
result.put(cur.getName(), cur);
}
return result;
}
Aggregations