Search in sources :

Example 1 with UnitUsageRecord

use of org.killbill.billing.client.model.UnitUsageRecord in project killbill by killbill.

the class TestUsage method testRecordUsage.

@Test(groups = "slow", description = "Can record and retrieve usage data")
public void testRecordUsage() throws Exception {
    final Account accountJson = createAccountWithDefaultPaymentMethod();
    final Subscription base = new Subscription();
    base.setAccountId(accountJson.getAccountId());
    base.setProductName("Pistol");
    base.setProductCategory(ProductCategory.BASE);
    base.setBillingPeriod(BillingPeriod.MONTHLY);
    base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Subscription addOn = new Subscription();
    addOn.setAccountId(accountJson.getAccountId());
    addOn.setProductName("Bullets");
    addOn.setProductCategory(ProductCategory.ADD_ON);
    addOn.setBillingPeriod(BillingPeriod.NO_BILLING_PERIOD);
    addOn.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Bundle bundle = killBillClient.createSubscriptionWithAddOns(ImmutableList.<Subscription>of(base, addOn), null, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
    final UUID addOnSubscriptionId = Iterables.<Subscription>find(bundle.getSubscriptions(), new Predicate<Subscription>() {

        @Override
        public boolean apply(final Subscription input) {
            return ProductCategory.ADD_ON.equals(input.getProductCategory());
        }
    }).getSubscriptionId();
    clock.addDays(1);
    final UsageRecord usageRecord1 = new UsageRecord();
    usageRecord1.setAmount(10L);
    usageRecord1.setRecordDate(clock.getUTCToday().minusDays(1));
    final UsageRecord usageRecord2 = new UsageRecord();
    usageRecord2.setAmount(5L);
    usageRecord2.setRecordDate(clock.getUTCToday());
    final UnitUsageRecord unitUsageRecord = new UnitUsageRecord();
    unitUsageRecord.setUnitType("bullets");
    unitUsageRecord.setUsageRecords(ImmutableList.<UsageRecord>of(usageRecord1, usageRecord2));
    final SubscriptionUsageRecord usage = new SubscriptionUsageRecord();
    usage.setSubscriptionId(addOnSubscriptionId);
    usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
    killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
    final RolledUpUsage retrievedUsage1 = killBillClient.getRolledUpUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday());
    Assert.assertEquals(retrievedUsage1.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage1.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage1.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
    // endDate is excluded
    Assert.assertEquals((long) retrievedUsage1.getRolledUpUnits().get(0).getAmount(), 10);
    final RolledUpUsage retrievedUsage2 = killBillClient.getRolledUpUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday().plusDays(1));
    Assert.assertEquals(retrievedUsage2.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage2.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage2.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
    Assert.assertEquals((long) retrievedUsage2.getRolledUpUnits().get(0).getAmount(), 15);
    final RolledUpUsage retrievedUsage3 = killBillClient.getRolledUpUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday(), clock.getUTCToday().plusDays(1));
    Assert.assertEquals(retrievedUsage3.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage3.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage3.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
    Assert.assertEquals((long) retrievedUsage3.getRolledUpUnits().get(0).getAmount(), 5);
    final RolledUpUsage retrievedUsage4 = killBillClient.getRolledUpUsage(addOnSubscriptionId, null, clock.getUTCToday(), clock.getUTCToday().plusDays(1));
    Assert.assertEquals(retrievedUsage4.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage4.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage4.getRolledUpUnits().get(0).getUnitType(), "bullets");
    Assert.assertEquals((long) retrievedUsage4.getRolledUpUnits().get(0).getAmount(), 5);
}
Also used : Account(org.killbill.billing.client.model.Account) RolledUpUsage(org.killbill.billing.client.model.RolledUpUsage) Bundle(org.killbill.billing.client.model.Bundle) UnitUsageRecord(org.killbill.billing.client.model.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.client.model.SubscriptionUsageRecord) UsageRecord(org.killbill.billing.client.model.UsageRecord) UnitUsageRecord(org.killbill.billing.client.model.UnitUsageRecord) Subscription(org.killbill.billing.client.model.Subscription) UUID(java.util.UUID) SubscriptionUsageRecord(org.killbill.billing.client.model.SubscriptionUsageRecord) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Example 2 with UnitUsageRecord

use of org.killbill.billing.client.model.UnitUsageRecord in project killbill by killbill.

the class TestUsage method testRecordUsageTrackingIdExists.

@Test(groups = "slow", description = "Test tracking ID already exists")
public void testRecordUsageTrackingIdExists() throws Exception {
    final Account accountJson = createAccountWithDefaultPaymentMethod();
    final Subscription base = new Subscription();
    base.setAccountId(accountJson.getAccountId());
    base.setProductName("Pistol");
    base.setProductCategory(ProductCategory.BASE);
    base.setBillingPeriod(BillingPeriod.MONTHLY);
    base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Subscription addOn = new Subscription();
    addOn.setAccountId(accountJson.getAccountId());
    addOn.setProductName("Bullets");
    addOn.setProductCategory(ProductCategory.ADD_ON);
    addOn.setBillingPeriod(BillingPeriod.NO_BILLING_PERIOD);
    addOn.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Bundle bundle = killBillClient.createSubscriptionWithAddOns(ImmutableList.<Subscription>of(base, addOn), null, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
    final UUID addOnSubscriptionId = Iterables.<Subscription>find(bundle.getSubscriptions(), new Predicate<Subscription>() {

        @Override
        public boolean apply(final Subscription input) {
            return ProductCategory.ADD_ON.equals(input.getProductCategory());
        }
    }).getSubscriptionId();
    final UsageRecord usageRecord1 = new UsageRecord();
    usageRecord1.setAmount(10L);
    usageRecord1.setRecordDate(clock.getUTCToday().minusDays(1));
    final UnitUsageRecord unitUsageRecord = new UnitUsageRecord();
    unitUsageRecord.setUnitType("bullets");
    unitUsageRecord.setUsageRecords(ImmutableList.<UsageRecord>of(usageRecord1));
    final SubscriptionUsageRecord usage = new SubscriptionUsageRecord();
    usage.setSubscriptionId(addOnSubscriptionId);
    usage.setTrackingId(UUID.randomUUID().toString());
    usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
    killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
    try {
        killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
        Assert.fail();
    } catch (final KillBillClientException e) {
        Assert.assertEquals(e.getBillingException().getCode(), (Integer) ErrorCode.USAGE_RECORD_TRACKING_ID_ALREADY_EXISTS.getCode());
    }
}
Also used : Account(org.killbill.billing.client.model.Account) Bundle(org.killbill.billing.client.model.Bundle) UnitUsageRecord(org.killbill.billing.client.model.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.client.model.SubscriptionUsageRecord) UsageRecord(org.killbill.billing.client.model.UsageRecord) UnitUsageRecord(org.killbill.billing.client.model.UnitUsageRecord) KillBillClientException(org.killbill.billing.client.KillBillClientException) Subscription(org.killbill.billing.client.model.Subscription) UUID(java.util.UUID) SubscriptionUsageRecord(org.killbill.billing.client.model.SubscriptionUsageRecord) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Aggregations

Predicate (com.google.common.base.Predicate)2 UUID (java.util.UUID)2 Account (org.killbill.billing.client.model.Account)2 Bundle (org.killbill.billing.client.model.Bundle)2 Subscription (org.killbill.billing.client.model.Subscription)2 SubscriptionUsageRecord (org.killbill.billing.client.model.SubscriptionUsageRecord)2 UnitUsageRecord (org.killbill.billing.client.model.UnitUsageRecord)2 UsageRecord (org.killbill.billing.client.model.UsageRecord)2 Test (org.testng.annotations.Test)2 KillBillClientException (org.killbill.billing.client.KillBillClientException)1 RolledUpUsage (org.killbill.billing.client.model.RolledUpUsage)1