Search in sources :

Example 16 with DryRunArguments

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();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Invoice(org.killbill.billing.invoice.api.Invoice) DryRunArguments(org.killbill.billing.invoice.api.DryRunArguments) UUID(java.util.UUID) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

DryRunArguments (org.killbill.billing.invoice.api.DryRunArguments)16 LocalDate (org.joda.time.LocalDate)12 Account (org.killbill.billing.account.api.Account)11 Invoice (org.killbill.billing.invoice.api.Invoice)11 Test (org.testng.annotations.Test)10 BigDecimal (java.math.BigDecimal)8 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)8 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)8 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)7 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)6 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)5 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)5 Entitlement (org.killbill.billing.entitlement.api.Entitlement)4 DateTime (org.joda.time.DateTime)3 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)3 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)3 CallContext (org.killbill.billing.util.callcontext.CallContext)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2