use of org.killbill.billing.invoice.api.DryRunArguments in project killbill by killbill.
the class TestIntegrationDryRunInvoice method testDryRunWithPendingSubscription.
@Test(groups = "slow")
public void testDryRunWithPendingSubscription() throws Exception {
final LocalDate initialDate = new LocalDate(2017, 4, 1);
clock.setDay(initialDate);
// Create account with no BCD
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(null));
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME, null);
final LocalDate futureDate = new LocalDate(2017, 5, 1);
// No CREATE event as this is set in the future
final UUID createdEntitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), futureDate, futureDate, false, true, ImmutableList.<PluginProperty>of(), callContext);
final Entitlement createdEntitlement = entitlementApi.getEntitlementForId(createdEntitlementId, callContext);
assertEquals(createdEntitlement.getState(), Entitlement.EntitlementState.PENDING);
assertEquals(createdEntitlement.getEffectiveStartDate().compareTo(futureDate), 0);
assertEquals(createdEntitlement.getEffectiveEndDate(), null);
assertListenerStatus();
// Generate a dryRun invoice on the billing startDate
final Invoice dryRunInvoice1 = invoiceUserApi.triggerDryRunInvoiceGeneration(createdEntitlement.getAccountId(), futureDate, DRY_RUN_TARGET_DATE_ARG, callContext);
assertEquals(dryRunInvoice1.getInvoiceItems().size(), 1);
assertEquals(dryRunInvoice1.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
assertEquals(dryRunInvoice1.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(dryRunInvoice1.getInvoiceItems().get(0).getStartDate(), futureDate);
assertEquals(dryRunInvoice1.getInvoiceItems().get(0).getPlanName(), "shotgun-annual");
// Generate a dryRun invoice with a plan change
final DryRunArguments dryRunSubscriptionActionArg = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null, SubscriptionEventType.CHANGE, createdEntitlement.getId(), createdEntitlement.getBundleId(), futureDate, BillingActionPolicy.IMMEDIATE);
// First one day prior subscription starts
try {
invoiceUserApi.triggerDryRunInvoiceGeneration(createdEntitlement.getAccountId(), futureDate.minusDays(1), dryRunSubscriptionActionArg, callContext);
fail("Should fail to trigger dryRun invoice prior subscription starts");
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), INVOICE_NOTHING_TO_DO.getCode());
}
// Second, on the startDate
final Invoice dryRunInvoice2 = invoiceUserApi.triggerDryRunInvoiceGeneration(createdEntitlement.getAccountId(), futureDate, dryRunSubscriptionActionArg, callContext);
assertEquals(dryRunInvoice2.getInvoiceItems().size(), 1);
assertEquals(dryRunInvoice2.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
assertEquals(dryRunInvoice2.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(dryRunInvoice2.getInvoiceItems().get(0).getStartDate(), futureDate);
assertEquals(dryRunInvoice2.getInvoiceItems().get(0).getPlanName(), "pistol-monthly");
// Check BCD is not yet set
final Account refreshedAccount1 = accountUserApi.getAccountById(account.getId(), callContext);
assertEquals(refreshedAccount1.getBillCycleDayLocal(), new Integer(0));
busHandler.pushExpectedEvents(NextEvent.INVOICE);
final Invoice realInvoice = invoiceUserApi.triggerInvoiceGeneration(createdEntitlement.getAccountId(), futureDate, callContext);
assertListenerStatus();
assertEquals(realInvoice.getInvoiceItems().size(), 1);
assertEquals(realInvoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
assertEquals(realInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(realInvoice.getInvoiceItems().get(0).getStartDate(), futureDate);
assertEquals(realInvoice.getInvoiceItems().get(0).getPlanName(), "shotgun-annual");
// Check BCD is still not set (SUBSCRIPTION alignment)
final Account refreshedAccount2 = accountUserApi.getAccountById(account.getId(), callContext);
assertEquals(refreshedAccount2.getBillCycleDayLocal(), new Integer(0));
// Move clock past startDate to check nothing happens
busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
clock.addDays(31);
assertListenerStatus();
// Move clock after PHASE event
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
clock.addMonths(12);
assertListenerStatus();
}
Aggregations