use of org.killbill.billing.client.model.gen.InvoiceItem 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.gen.InvoiceItem in project killbill by killbill.
the class TestExternalRefund method testAutomaticPaymentAndExternalRefundWithAdjustments.
@Test(groups = "slow", description = "#255 - Scenario 2a - Can refund an automatic payment though an external refund over item adjustments")
public void testAutomaticPaymentAndExternalRefundWithAdjustments() throws Exception {
final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();
// delete PM
paymentMethodApi.deletePaymentMethod(accountJson.getPaymentMethodId(), true, true, NULL_PLUGIN_PROPERTIES, requestOptions);
final Payments paymentsForAccount = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);
final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, true, false, false, null, AuditLevel.NONE, requestOptions);
final List<InvoiceItem> itemsToBeAdjusted = invoices.get(1).getItems();
// external refund
final InvoicePaymentTransaction invoicePaymentTransactionRequest = new InvoicePaymentTransaction();
invoicePaymentTransactionRequest.setAmount(BigDecimal.valueOf(249.95));
invoicePaymentTransactionRequest.setCurrency(accountJson.getCurrency());
invoicePaymentTransactionRequest.setPaymentId(payment.getPaymentId());
invoicePaymentTransactionRequest.setIsAdjusted(true);
invoicePaymentTransactionRequest.setAdjustments(itemsToBeAdjusted);
final InvoicePayment invoicePaymentExternalRefund = invoicePaymentApi.createRefundWithAdjustments(payment.getPaymentId(), invoicePaymentTransactionRequest, true, null, NULL_PLUGIN_PROPERTIES, requestOptions);
assertNotNull(invoicePaymentExternalRefund);
assertInvoicePaymentsExternalRefund(accountJson.getAccountId(), invoicePaymentExternalRefund);
assertRefundInvoiceAdjustments(accountJson.getAccountId());
assertRefundAccountBalance(accountJson.getAccountId(), BigDecimal.ZERO, BigDecimal.ZERO);
}
use of org.killbill.billing.client.model.gen.InvoiceItem in project killbill by killbill.
the class TestInvoiceItem method testCustomFields.
@Test(groups = "slow", description = "Add custom fields to invoice item")
public void testCustomFields() throws Exception {
final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
final Invoices invoicesJson = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
Assert.assertNotNull(invoicesJson);
Assert.assertEquals(invoicesJson.size(), 2);
final List<InvoiceItem> invoiceItems = invoicesJson.get(0).getItems();
Assert.assertNotNull(invoiceItems);
final CustomFields customFields = new CustomFields();
customFields.add(new CustomField(null, invoiceItems.get(0).getInvoiceItemId(), ObjectType.INVOICE_ITEM, "1", "value1", null));
customFields.add(new CustomField(null, invoiceItems.get(0).getInvoiceItemId(), ObjectType.INVOICE_ITEM, "2", "value2", null));
customFields.add(new CustomField(null, invoiceItems.get(0).getInvoiceItemId(), ObjectType.INVOICE_ITEM, "3", "value3", null));
invoiceItemApi.createInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), customFields, requestOptions);
final List<CustomField> invoiceItemCustomFields = invoiceItemApi.getInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), requestOptions);
Assert.assertEquals(invoiceItemCustomFields.size(), 3);
// Delete all custom fields for account
invoiceItemApi.deleteInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), null, requestOptions);
final List<CustomField> remainingCustomFields = invoiceItemApi.getInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), requestOptions);
Assert.assertEquals(remainingCustomFields.size(), 0);
}
use of org.killbill.billing.client.model.gen.InvoiceItem in project killbill by killbill.
the class TestInvoiceItem method testTags.
@Test(groups = "slow", description = "Add tags to invoice item")
public void testTags() throws Exception {
final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
final Invoices invoicesJson = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
Assert.assertNotNull(invoicesJson);
Assert.assertEquals(invoicesJson.size(), 2);
final List<InvoiceItem> invoiceItems = invoicesJson.get(0).getItems();
Assert.assertNotNull(invoiceItems);
// Create tag definition
final TagDefinition input = new TagDefinition(null, false, "tagtest", "invoice item tag test", ImmutableList.<ObjectType>of(ObjectType.INVOICE_ITEM), null);
final TagDefinition objFromJson = tagDefinitionApi.createTagDefinition(input, requestOptions);
Assert.assertNotNull(objFromJson);
Assert.assertEquals(objFromJson.getName(), input.getName());
Assert.assertEquals(objFromJson.getDescription(), input.getDescription());
// Add a tag
final Multimap<String, String> followQueryParams = HashMultimap.create();
followQueryParams.put(JaxrsResource.QUERY_ACCOUNT_ID, accountJson.getAccountId().toString());
final RequestOptions followRequestOptions = requestOptions.extend().withQueryParamsForFollow(followQueryParams).build();
invoiceItemApi.createInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), ImmutableList.<UUID>of(objFromJson.getId()), followRequestOptions);
// Retrieves all tags
final List<Tag> tags1 = invoiceItemApi.getInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), accountJson.getAccountId(), null, AuditLevel.FULL, requestOptions);
Assert.assertEquals(tags1.size(), 1);
Assert.assertEquals(tags1.get(0).getTagDefinitionId(), objFromJson.getId());
// Verify adding the same tag a second time doesn't do anything
invoiceItemApi.createInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), ImmutableList.<UUID>of(objFromJson.getId()), followRequestOptions);
// Retrieves all tags again
final List<Tag> tags2 = invoiceItemApi.getInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), accountJson.getAccountId(), null, AuditLevel.FULL, requestOptions);
Assert.assertEquals(tags2, tags1);
// Verify audit logs
Assert.assertEquals(tags2.get(0).getAuditLogs().size(), 1);
final AuditLog auditLogJson = tags2.get(0).getAuditLogs().get(0);
Assert.assertEquals(auditLogJson.getChangeType(), "INSERT");
Assert.assertEquals(auditLogJson.getChangedBy(), createdBy);
Assert.assertEquals(auditLogJson.getReasonCode(), reason);
Assert.assertEquals(auditLogJson.getComments(), comment);
Assert.assertNotNull(auditLogJson.getChangeDate());
Assert.assertNotNull(auditLogJson.getUserToken());
// remove it
invoiceItemApi.deleteInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), ImmutableList.<UUID>of(objFromJson.getId()), requestOptions);
final List<Tag> tags3 = invoiceItemApi.getInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), accountJson.getAccountId(), null, AuditLevel.FULL, requestOptions);
Assert.assertEquals(tags3.size(), 0);
tagDefinitionApi.deleteTagDefinition(objFromJson.getId(), requestOptions);
List<TagDefinition> objsFromJson = tagDefinitionApi.getTagDefinitions(requestOptions);
Assert.assertNotNull(objsFromJson);
Boolean isFound = false;
for (TagDefinition tagDefinition : objsFromJson) {
isFound |= tagDefinition.getId().equals(objFromJson.getId());
}
Assert.assertFalse(isFound);
}
use of org.killbill.billing.client.model.gen.InvoiceItem in project killbill by killbill.
the class TestCredit method testAddCreditToCommittedInvoice.
@Test(groups = "slow", description = "Can add a credit to an existing account", expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = ".*it is already in COMMITTED status")
public void testAddCreditToCommittedInvoice() throws Exception {
final Invoice invoice = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions).get(1);
final BigDecimal creditAmount = BigDecimal.ONE;
final InvoiceItem credit = new InvoiceItem();
credit.setAccountId(accountJson.getAccountId());
credit.setInvoiceId(invoice.getInvoiceId());
credit.setAmount(creditAmount);
final InvoiceItems credits = new InvoiceItems();
credits.add(credit);
final InvoiceItems objFromJson = creditApi.createCredits(credits, true, NULL_PLUGIN_PROPERTIES, requestOptions);
Assert.assertTrue(objFromJson.get(0).getAmount().compareTo(creditAmount) == 0);
}
Aggregations