use of org.killbill.billing.client.model.Subscriptions 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);
callbackServlet.pushExpectedEvents(ExtBusEventType.ACCOUNT_CHANGE, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.INVOICE_CREATION);
final Subscriptions body = new Subscriptions();
body.add(base);
body.add(addOn);
final Bundle bundle = subscriptionApi.createSubscriptionWithAddOns(body, null, null, NULL_PLUGIN_PROPERTIES, requestOptions);
callbackServlet.assertListenerStatus();
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));
usageApi.recordUsage(usage, requestOptions);
callbackServlet.assertListenerStatus();
final RolledUpUsage retrievedUsage1 = usageApi.getUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday(), requestOptions);
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 = usageApi.getUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday().plusDays(1), requestOptions);
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 = usageApi.getUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday(), clock.getUTCToday().plusDays(1), requestOptions);
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 = usageApi.getAllUsage(addOnSubscriptionId, clock.getUTCToday(), clock.getUTCToday().plusDays(1), requestOptions);
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);
callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
clock.addMonths(1);
callbackServlet.assertListenerStatus();
final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.MINIMAL, requestOptions);
Assert.assertEquals(invoices.size(), 2);
// Verify system assigned one tracking ID and this is correctly returned
Assert.assertEquals(invoices.get(1).getTrackingIds().size(), 1);
final InvoiceItem usageItem = Iterables.tryFind(invoices.get(1).getItems(), new Predicate<InvoiceItem>() {
@Override
public boolean apply(final InvoiceItem input) {
return input.getItemType() == InvoiceItemType.USAGE;
}
}).orNull();
Assert.assertNotNull(usageItem);
Assert.assertEquals(usageItem.getPrettyPlanName(), "Bullet Monthly Plan");
Assert.assertEquals(usageItem.getPrettyPhaseName(), "Bullet Monthly Plan Evergreen");
Assert.assertEquals(usageItem.getPrettyUsageName(), "Bullet Usage In Arrear");
}
use of org.killbill.billing.client.model.Subscriptions 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 Subscriptions body = new Subscriptions();
body.add(base);
body.add(addOn);
final Bundle bundle = subscriptionApi.createSubscriptionWithAddOns(body, null, null, NULL_PLUGIN_PROPERTIES, requestOptions);
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);
final String trackingId = UUID.randomUUID().toString();
usage.setTrackingId(trackingId);
usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
usageApi.recordUsage(usage, requestOptions);
try {
usageApi.recordUsage(usage, requestOptions);
Assert.fail();
} catch (final KillBillClientException e) {
Assert.assertEquals(e.getBillingException().getCode(), (Integer) ErrorCode.USAGE_RECORD_TRACKING_ID_ALREADY_EXISTS.getCode());
}
}
use of org.killbill.billing.client.model.Subscriptions in project killbill by killbill.
the class TestEntitlement method testEntitlementWithAddOnsWithWRITTEN_OFF.
@Test(groups = "slow", description = "Create a base entitlement and also addOns entitlements under the same bundle")
public void testEntitlementWithAddOnsWithWRITTEN_OFF() throws Exception {
final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final String bundleExternalKey = "bundleKey12346542";
final Account accountJson = createAccount();
final Subscription base = new Subscription();
base.setAccountId(accountJson.getAccountId());
base.setBundleExternalKey(bundleExternalKey);
base.setExternalKey("Base");
base.setProductName("Shotgun");
base.setProductCategory(ProductCategory.BASE);
base.setBillingPeriod(BillingPeriod.MONTHLY);
base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Subscription addOn1 = new Subscription();
addOn1.setAccountId(accountJson.getAccountId());
addOn1.setExternalKey("addOn1");
addOn1.setProductName("Telescopic-Scope");
addOn1.setProductCategory(ProductCategory.ADD_ON);
addOn1.setBillingPeriod(BillingPeriod.MONTHLY);
addOn1.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Subscription addOn2 = new Subscription();
addOn2.setAccountId(accountJson.getAccountId());
addOn2.setExternalKey("addOn2");
addOn2.setProductName("Laser-Scope");
addOn2.setProductCategory(ProductCategory.ADD_ON);
addOn2.setBillingPeriod(BillingPeriod.MONTHLY);
addOn2.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Subscriptions subscriptions = new Subscriptions();
subscriptions.add(base);
subscriptions.add(addOn1);
subscriptions.add(addOn2);
final Bundle bundle = subscriptionApi.createSubscriptionWithAddOns(subscriptions, null, null, null, null, null, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, NULL_PLUGIN_PROPERTIES, requestOptions);
assertNotNull(bundle);
assertEquals(bundle.getExternalKey(), bundleExternalKey);
assertEquals(bundle.getSubscriptions().size(), 3);
int found = 0;
for (int i = 0; i < 3; i++) {
final Subscription cur = bundle.getSubscriptions().get(i);
assertEquals(cur.getBundleExternalKey(), bundleExternalKey);
if ("Shotgun".equals(cur.getProductName())) {
assertEquals(cur.getExternalKey(), "Base");
found++;
} else if ("Telescopic-Scope".equals(cur.getProductName())) {
assertEquals(cur.getExternalKey(), "addOn1");
found++;
} else if ("Laser-Scope".equals(cur.getProductName())) {
assertEquals(cur.getExternalKey(), "addOn2");
found++;
}
}
assertEquals(found, 3);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
assertEquals(invoices.size(), 1);
assertEquals(invoices.get(0).getBalance().compareTo(BigDecimal.ZERO), 1);
assertEquals(invoiceApi.getInvoiceTags(invoices.get(0).getInvoiceId(), requestOptions).size(), 0);
final Bundles accountBundles = accountApi.getAccountBundles(accountJson.getAccountId(), null, null, requestOptions);
assertEquals(accountBundles.size(), 1);
for (final Subscription subscription : accountBundles.get(0).getSubscriptions()) {
assertEquals(subscription.getState(), EntitlementState.ACTIVE);
}
accountApi.closeAccount(accountJson.getAccountId(), true, true, false, true, requestOptions);
final Bundles accountBundlesAfterClose = accountApi.getAccountBundles(accountJson.getAccountId(), null, null, requestOptions);
assertEquals(accountBundlesAfterClose.size(), 1);
for (final Subscription subscription : accountBundlesAfterClose.get(0).getSubscriptions()) {
assertEquals(subscription.getState(), EntitlementState.CANCELLED);
}
final List<Invoice> invoicesAfterClose = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
assertEquals(invoicesAfterClose.size(), 1);
assertEquals(invoicesAfterClose.get(0).getBalance().compareTo(BigDecimal.ZERO), 0);
assertEquals(invoiceApi.getInvoiceTags(invoicesAfterClose.get(0).getInvoiceId(), requestOptions).size(), 1);
}
Aggregations