Search in sources :

Example 16 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear 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);
    final List<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
    // 
    // First period: startDate - firstBCDDate
    // 
    // 2 items for unit1
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "unit1", 130L, "tracking-1"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "unit1", 271L, "tracking-2"));
    // 1 items for unit2
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 24), "unit2", 10L, "tracking-1"));
    // 
    // Second period: firstBCDDate - endDate
    // 
    // 1 items unit1
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit1", 199L, "tracking-4"));
    // 1 items unit2
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit2", 20L, "tracking-5"));
    final DefaultUnit unit1 = new DefaultUnit().setName("unit1");
    final DefaultLimit limit1 = new DefaultLimit().setUnit(unit1).setMax((double) -1);
    final DefaultUnit unit2 = new DefaultUnit().setName("unit2");
    final DefaultLimit limit2 = new DefaultLimit().setUnit(unit2).setMax((double) -1);
    final DefaultTier tier = createDefaultTierWithLimits(BigDecimal.TEN, limit1, limit2);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, 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 ContiguousIntervalCapacityUsageInArrear intervalCapacityInArrear = createContiguousIntervalCapacityInArrear(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 = intervalCapacityInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
    final List<InvoiceItem> result = usageResult.getInvoiceItems();
    assertEquals(result.size(), 2);
    final Set<TrackingRecordId> trackingIds = usageResult.getTrackingIds();
    checkTrackingIds(rawUsageRecords, usageResult.getTrackingIds());
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("9.0")), 0, String.format("%s != 9.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);
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("9.0")), 0, String.format("%s != 9.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);
}
Also used : TrackingRecordId(org.killbill.billing.invoice.generator.InvoiceWithMetadata.TrackingRecordId) DefaultLimit(org.killbill.billing.catalog.DefaultLimit) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) UsageInArrearItemsAndNextNotificationDate(org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 17 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testMultipleItemsAndTiers.

private void testMultipleItemsAndTiers(UsageDetailMode usageDetailMode) 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-2"));
    List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, usageDetailMode, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(BigDecimal.ONE), 0, String.format("%s != 1.0", result.get(0).getAmount()));
    UsageCapacityInArrearAggregate itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(BigDecimal.ONE), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    List<UsageInArrearTierUnitDetail> itemUnitDetails = itemDetails.getTierDetails();
    // BAR item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(0).getTier(), 1);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 99);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // FOO item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(1).getTier(), 1);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 5);
    assertEquals(itemUnitDetails.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-1"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-2"));
    result = produceInvoiceItems(rawUsageRecords, usageDetailMode, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(BigDecimal.TEN), 0, String.format("%s != 10.0", result.get(0).getAmount()));
    itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(BigDecimal.TEN), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    itemUnitDetails = itemDetails.getTierDetails();
    // FOO item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(0).getTier(), 1);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 5);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // BAR item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(1).getTier(), 2);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 101);
    assertEquals(itemUnitDetails.get(1).getTierPrice().compareTo(BigDecimal.TEN), 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-3"));
    result = produceInvoiceItems(rawUsageRecords, usageDetailMode, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("100.0")), 0, String.format("%s != 100.0", result.get(0).getAmount()));
    itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(new BigDecimal("100.0")), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    itemUnitDetails = itemDetails.getTierDetails();
    // BAR item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(0).getTier(), 2);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 101);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(new BigDecimal("10.0")), 0);
    // FOO item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(1).getTier(), 3);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 75);
    assertEquals(itemUnitDetails.get(1).getTierPrice().compareTo(new BigDecimal("100.0")), 0);
}
Also used : 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) UsageInArrearTierUnitDetail(org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) TypeReference(com.fasterxml.jackson.core.type.TypeReference) UsageCapacityInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageCapacityInArrearAggregate)

Example 18 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord 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 19 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testGetRolledUpUsageUsageAtTransitionTimesCancelAtBCD.

@Test(groups = "fast")
public void testGetRolledUpUsageUsageAtTransitionTimesCancelAtBCD() throws Exception {
    final DefaultTieredBlock tieredBlock1 = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(tieredBlock1);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
    // 2015-03-15T18:10:17Z
    final LocalDate t0 = new LocalDate(2015, 03, BCD);
    final BillingEvent eventT0 = createMockBillingEvent(t0.toDateTime(new LocalTime(18, 10, 17), DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    Mockito.when(eventT0.getTransitionType()).thenReturn(SubscriptionBaseTransitionType.CREATE);
    // 2015-04-15T11:22:30Z
    final LocalDate t1 = new LocalDate(2015, 04, BCD);
    final BillingEvent eventT1 = createMockBillingEvent(t1.toDateTime(new LocalTime(11, 22, 30), DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    Mockito.when(eventT1.getTransitionType()).thenReturn(SubscriptionBaseTransitionType.CANCEL);
    final LocalDate targetDate = t1;
    // At t0
    final RawUsageRecord raw1 = new DefaultRawUsage(subscriptionId, t0, "unit", 12L, "tracking-1");
    // At t1
    final RawUsageRecord raw2 = new DefaultRawUsage(subscriptionId, t1, "unit", 10L, "tracking-2");
    // Should be ignored
    final RawUsageRecord raw3 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, BCD), "unit", 100L, "tracking-3");
    final List<RawUsageRecord> rawUsageRecord = ImmutableList.of(raw1, raw2, raw3);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsageRecord, targetDate, true, eventT0, eventT1);
    // Verify transition times
    assertEquals(intervalConsumableInArrear.transitionTimes.size(), 2);
    assertEquals(intervalConsumableInArrear.transitionTimes.get(0).getTargetBillingEvent().getTransitionType(), SubscriptionBaseTransitionType.CREATE);
    assertEquals(intervalConsumableInArrear.transitionTimes.get(1).getTargetBillingEvent().getTransitionType(), SubscriptionBaseTransitionType.CANCEL);
    final List<RolledUpUsageWithMetadata> rolledUpUsage = intervalConsumableInArrear.getRolledUpUsage().getUsage();
    assertEquals(rolledUpUsage.size(), 1);
    assertEquals(rolledUpUsage.get(0).getSubscriptionId(), subscriptionId);
    assertEquals(rolledUpUsage.get(0).getStart().compareTo(t0), 0);
    assertEquals(rolledUpUsage.get(0).getEnd().compareTo(t1), 0);
    assertEquals(rolledUpUsage.get(0).getRolledUpUnits().size(), 1);
    assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(0).getUnitType(), "unit");
    assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(0).getAmount().longValue(), 22L);
}
Also used : RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) LocalTime(org.joda.time.LocalTime) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 20 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testComputeMissingItemsDetailModeTopTier_DETAIL.

@Test(groups = "fast")
public void testComputeMissingItemsDetailModeTopTier_DETAIL() 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-2"));
    List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.TOP_TIER, UsageDetailMode.DETAIL, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 2);
    // BAR: 99 * 2 = 198
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("198")), 0);
    assertEquals(result.get(0).getQuantity().intValue(), 99);
    assertEquals(result.get(0).getRate().compareTo(new BigDecimal("2.0")), 0);
    // FOO: 5 * 1 = 5
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("5")), 0);
    assertEquals(result.get(1).getQuantity().intValue(), 5);
    assertEquals(result.get(1).getRate().compareTo(BigDecimal.ONE), 0);
    // Case 2
    rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 5L, "tracking-3"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-4"));
    result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.TOP_TIER, UsageDetailMode.DETAIL, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 2);
    // BAR: 101 * 20 = 2020
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("2020.0")), 0);
    assertEquals(result.get(0).getQuantity().intValue(), 101);
    assertEquals(result.get(0).getRate().compareTo(new BigDecimal("20.0")), 0);
    // FOO: 5 * 1 = 5
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("5")), 0);
    assertEquals(result.get(1).getQuantity().intValue(), 5);
    assertEquals(result.get(1).getRate().compareTo(BigDecimal.ONE), 0);
    // Case 3
    rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 76L, "tracking-5"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-6"));
    result = produceInvoiceItems(rawUsageRecords, TierBlockPolicy.TOP_TIER, UsageDetailMode.DETAIL, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 2);
    // BAR: 101 * 20 = 2020
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("2020.0")), 0);
    assertEquals(result.get(0).getQuantity().intValue(), 101);
    assertEquals(result.get(0).getRate().compareTo(new BigDecimal("20.0")), 0);
    // FOO: 76 * 100 = 7500
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("7600")), 0);
    assertEquals(result.get(1).getQuantity().intValue(), 76);
    assertEquals(result.get(1).getRate().compareTo(new BigDecimal("100.0")), 0);
}
Also used : RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) 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) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

RawUsageRecord (org.killbill.billing.usage.api.RawUsageRecord)23 LocalDate (org.joda.time.LocalDate)19 ArrayList (java.util.ArrayList)16 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)16 Test (org.testng.annotations.Test)15 BigDecimal (java.math.BigDecimal)11 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)11 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)11 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)11 DefaultTier (org.killbill.billing.catalog.DefaultTier)8 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)8 BillingEvent (org.killbill.billing.junction.BillingEvent)8 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)7 UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 TrackingRecordId (org.killbill.billing.invoice.generator.InvoiceWithMetadata.TrackingRecordId)4 UsageConsumableInArrearAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate)4 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)4 HashMap (java.util.HashMap)3 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)3