Search in sources :

Example 11 with Usage

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) {
            return input.getUsages();
        }
    }));
    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;
}
Also used : Function(com.google.common.base.Function) Usage(org.killbill.billing.catalog.api.Usage) HashMap(java.util.HashMap) BillingEvent(org.killbill.billing.junction.BillingEvent)

Example 12 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestXMLReader method testUsageConsumableInAdvanceTopUp.

@Test(groups = "fast")
public void testUsageConsumableInAdvanceTopUp() {
    try {
        final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("UsageExperimental.xml").toExternalForm(), StandaloneCatalog.class);
        final Usage[] usages = getUsages(catalog, "consumable-in-advance-topup");
        assertEquals(usages.length, 1);
        final Usage usage = usages[0];
        assertEquals(usage.getName(), "consumable-in-advance-topup-usage1");
        assertEquals(usage.getBillingPeriod(), BillingPeriod.NO_BILLING_PERIOD);
        assertEquals(usage.getUsageType(), UsageType.CONSUMABLE);
        assertEquals(usage.getBillingMode(), BillingMode.IN_ADVANCE);
        assertEquals(usage.getLimits().length, 0);
        assertEquals(usage.getTiers().length, 0);
        assertEquals(usage.getBlocks().length, 1);
        assertEquals(usage.getBlocks()[0].getUnit().getName(), "fastrack-tokens");
        assertEquals(usage.getBlocks()[0].getSize(), new Double("10"));
        assertEquals(usage.getBlocks()[0].getPrice().getPrices().length, 1);
        assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getValue(), new BigDecimal("0.10"));
        assertEquals(usage.getBlocks()[0].getMinTopUpCredit(), new Double("5"));
    } catch (Exception e) {
        Assert.fail(e.toString());
    }
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) BigDecimal(java.math.BigDecimal) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Test(org.testng.annotations.Test)

Example 13 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestXMLReader method testUsageCapacityInArrear.

@Test(groups = "fast")
public void testUsageCapacityInArrear() {
    try {
        final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("UsageExperimental.xml").toExternalForm(), StandaloneCatalog.class);
        final Usage[] usages = getUsages(catalog, "capacity-in-arrear");
        assertEquals(usages.length, 1);
        final Usage usage = usages[0];
        assertEquals(usage.getName(), "capacity-in-arrear-usage1");
        assertEquals(usage.getBillingPeriod(), BillingPeriod.MONTHLY);
        assertEquals(usage.getUsageType(), UsageType.CAPACITY);
        assertEquals(usage.getBillingMode(), BillingMode.IN_ARREAR);
        assertEquals(usage.getLimits().length, 0);
        assertEquals(usage.getBlocks().length, 0);
        assertEquals(usage.getTiers().length, 2);
        assertEquals(usage.getTiers()[0].getLimits().length, 2);
        assertEquals(usage.getTiers()[0].getLimits()[0].getUnit().getName(), "bandwith-meg-sec");
        assertEquals(usage.getTiers()[0].getLimits()[0].getMax(), new Double("100"));
        assertEquals(usage.getTiers()[0].getLimits()[1].getUnit().getName(), "members");
        assertEquals(usage.getTiers()[0].getLimits()[1].getMax(), new Double("500"));
        assertEquals(usage.getTiers()[0].getFixedPrice().getPrices().length, 1);
        assertEquals(usage.getTiers()[0].getFixedPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getTiers()[0].getFixedPrice().getPrices()[0].getValue(), new BigDecimal("0.007"));
        assertEquals(usage.getTiers()[0].getRecurringPrice().getPrices().length, 1);
        assertEquals(usage.getTiers()[0].getRecurringPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getTiers()[0].getRecurringPrice().getPrices()[0].getValue(), new BigDecimal("0.8"));
        assertEquals(usage.getTiers()[1].getLimits()[0].getUnit().getName(), "bandwith-meg-sec");
        assertEquals(usage.getTiers()[1].getLimits()[0].getMax(), new Double("100"));
        assertEquals(usage.getTiers()[1].getLimits()[1].getUnit().getName(), "members");
        assertEquals(usage.getTiers()[1].getLimits()[1].getMax(), new Double("1000"));
        assertEquals(usage.getTiers()[1].getFixedPrice().getPrices().length, 1);
        assertEquals(usage.getTiers()[1].getFixedPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getTiers()[1].getFixedPrice().getPrices()[0].getValue(), new BigDecimal("0.4"));
        assertEquals(usage.getTiers()[1].getRecurringPrice().getPrices().length, 1);
        assertEquals(usage.getTiers()[1].getRecurringPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getTiers()[1].getRecurringPrice().getPrices()[0].getValue(), new BigDecimal("1.2"));
    } catch (Exception e) {
        Assert.fail(e.toString());
    }
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) BigDecimal(java.math.BigDecimal) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Test(org.testng.annotations.Test)

Example 14 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testComputeToBeBilledUsage.

@Test(groups = "fast")
public void testComputeToBeBilledUsage() {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate endDate = new LocalDate(2014, 04, 20);
    final DefaultUnit unit = new DefaultUnit().setName("unit");
    final DefaultLimit limit = new DefaultLimit().setUnit(unit).setMax((double) 100);
    final DefaultTier tier = createDefaultTierWithLimits(BigDecimal.TEN, limit);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = startDate.plusDays(1);
    final ContiguousIntervalUsageInArrear intervalCapacityInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsage>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList()));
    final List<InvoiceItem> existingUsage = Lists.newArrayList();
    final UsageInvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii1);
    final UsageInvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), 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, planName, phaseName, usage.getName(), 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, planName, phaseName, "other", startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii4);
    // Will be ignored because non usage item
    final FixedPriceInvoiceItem ii5 = new FixedPriceInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, BigDecimal.TEN, currency);
    existingUsage.add(ii5);
    final Iterable<InvoiceItem> billedItems = intervalCapacityInArrear.getBilledItems(startDate, endDate, existingUsage);
    final BigDecimal result = intervalCapacityInArrear.computeBilledUsage(billedItems);
    assertEquals(result.compareTo(BigDecimal.TEN.add(BigDecimal.TEN)), 0);
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RawUsage(org.killbill.billing.usage.RawUsage) 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) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 15 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testComputeBilledUsage.

@Test(groups = "fast")
public void testComputeBilledUsage() throws CatalogApiException {
    final DefaultUnit unit1 = new DefaultUnit().setName("unit1");
    final DefaultUnit unit2 = new DefaultUnit().setName("unit2");
    final DefaultLimit limit1_1 = new DefaultLimit().setUnit(unit1).setMax((double) 100).setMin((double) -1);
    final DefaultLimit limit1_2 = new DefaultLimit().setUnit(unit2).setMax((double) 1000).setMin((double) -1);
    final DefaultTier tier1 = createDefaultTierWithLimits(BigDecimal.TEN, limit1_1, limit1_2);
    final DefaultLimit limit2_1 = new DefaultLimit().setUnit(unit1).setMax((double) 200).setMin((double) -1);
    final DefaultLimit limit2_2 = new DefaultLimit().setUnit(unit2).setMax((double) 2000).setMin((double) -1);
    final DefaultTier tier2 = createDefaultTierWithLimits(new BigDecimal("20.0"), limit2_1, limit2_2);
    // Don't define any max for last tier to allow any number
    final DefaultLimit limit3_1 = new DefaultLimit().setUnit(unit1).setMin((double) -1).setMax((double) -1);
    final DefaultLimit limit3_2 = new DefaultLimit().setUnit(unit2).setMin((double) -1).setMax((double) -1);
    final DefaultTier tier3 = createDefaultTierWithLimits(new BigDecimal("30.0"), limit3_1, limit3_2);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier1, tier2, tier3);
    final LocalDate targetDate = new LocalDate(2014, 03, 20);
    final ContiguousIntervalUsageInArrear intervalCapacityInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsage>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList()));
    // Tier 1 (both units from tier 1)
    BigDecimal result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 100L), new DefaultRolledUpUnit("unit2", 1000L)));
    assertEquals(result, BigDecimal.TEN);
    // Tier 2 (only one unit from tier 1)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 100L), new DefaultRolledUpUnit("unit2", 1001L)));
    assertEquals(result, new BigDecimal("20.0"));
    // Tier 2 (only one unit from tier 1)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 101L), new DefaultRolledUpUnit("unit2", 1000L)));
    assertEquals(result, new BigDecimal("20.0"));
    // Tier 2 (both units from tier 2)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 101L), new DefaultRolledUpUnit("unit2", 1001L)));
    assertEquals(result, new BigDecimal("20.0"));
    // Tier 3 (only one unit from tier 3)
    result = intervalCapacityInArrear.computeToBeBilledCapacityInArrear(ImmutableList.<RolledUpUnit>of(new DefaultRolledUpUnit("unit1", 10L), new DefaultRolledUpUnit("unit2", 2001L)));
    assertEquals(result, new BigDecimal("30.0"));
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RawUsage(org.killbill.billing.usage.RawUsage) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

Usage (org.killbill.billing.catalog.api.Usage)21 Test (org.testng.annotations.Test)14 LocalDate (org.joda.time.LocalDate)11 BigDecimal (java.math.BigDecimal)9 DefaultTier (org.killbill.billing.catalog.DefaultTier)9 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)9 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)8 HashMap (java.util.HashMap)7 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)7 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)7 RawUsage (org.killbill.billing.usage.RawUsage)7 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)6 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)5 BillingEvent (org.killbill.billing.junction.BillingEvent)5 Function (com.google.common.base.Function)4 ArrayList (java.util.ArrayList)4 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)4 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2