Search in sources :

Example 6 with RolledUpUsage

use of org.killbill.billing.usage.api.RolledUpUsage 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));
}
Also used : RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RawUsage(org.killbill.billing.usage.RawUsage) 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)

Aggregations

LocalDate (org.joda.time.LocalDate)6 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)6 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 RolledUpUsageJson (org.killbill.billing.jaxrs.json.RolledUpUsageJson)2 RawUsage (org.killbill.billing.usage.RawUsage)2 TenantContext (org.killbill.billing.util.callcontext.TenantContext)2 TimedResource (org.killbill.commons.metrics.TimedResource)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 DefaultTier (org.killbill.billing.catalog.DefaultTier)1 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)1 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)1