Search in sources :

Example 36 with DefaultEntitlement

use of org.killbill.billing.entitlement.api.DefaultEntitlement in project killbill by killbill.

the class TestWithInvoicePlugin method testRescheduledViaNotification.

@Test(groups = "slow")
public void testRescheduledViaNotification() throws Exception {
    testInvoicePluginApi.shouldAddTaxItem = false;
    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 0);
    // Create original subscription (Trial PHASE) -> $0 invoice
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 1);
    Assert.assertFalse(testInvoicePluginApi.wasRescheduled);
    // Reschedule invoice generation
    final DateTime utcNow = clock.getUTCNow();
    testInvoicePluginApi.rescheduleDate = new DateTime(2012, 5, 2, utcNow.getHourOfDay(), utcNow.getMinuteOfHour(), utcNow.getSecondOfMinute(), DateTimeZone.UTC);
    // Move to Evergreen PHASE
    busHandler.pushExpectedEvents(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 2);
    Assert.assertFalse(testInvoicePluginApi.wasRescheduled);
    // PHASE invoice has been rescheduled, reset rescheduleDate
    testInvoicePluginApi.rescheduleDate = null;
    // Move one day
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(1);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 3);
    Assert.assertTrue(testInvoicePluginApi.wasRescheduled);
    // Invoicing resumes as expected
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 6, 1), new LocalDate(2012, 7, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 4);
    Assert.assertFalse(testInvoicePluginApi.wasRescheduled);
}
Also used : Account(org.killbill.billing.account.api.Account) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 37 with DefaultEntitlement

use of org.killbill.billing.entitlement.api.DefaultEntitlement in project killbill by killbill.

the class TestWithInvoicePlugin method testBasicAdditionalExternalChargeItem.

@Test(groups = "slow")
public void testBasicAdditionalExternalChargeItem() throws Exception {
    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final UUID pluginInvoiceItemId = UUID.randomUUID();
    final UUID pluginLinkedItemId = UUID.randomUUID();
    testInvoicePluginApi.additionalInvoiceItem = new ExternalChargeInvoiceItem(pluginInvoiceItemId, clock.getUTCNow(), null, account.getId(), null, null, null, null, null, null, null, null, "My charge", clock.getUTCToday(), null, BigDecimal.TEN, null, Currency.USD, pluginLinkedItemId, null);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 0);
    // Create original subscription (Trial PHASE) -> $0 invoice but plugin added one item
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.EXTERNAL_CHARGE, BigDecimal.TEN));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 1);
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(invoices.size(), 1);
    final List<InvoiceItem> invoiceItems = invoices.get(0).getInvoiceItems();
    final InvoiceItem externalCharge = Iterables.tryFind(invoiceItems, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getInvoiceItemType() == InvoiceItemType.EXTERNAL_CHARGE;
        }
    }).orNull();
    assertNotNull(externalCharge);
    // verify the ID is the one passed by the plugin #818
    assertEquals(externalCharge.getId(), pluginInvoiceItemId);
    // verify the ID is the one passed by the plugin #887
    assertEquals(externalCharge.getLinkedItemId(), pluginLinkedItemId);
    // On next invoice we will update the amount and the description of the previously inserted EXTERNAL_CHARGE item
    testInvoicePluginApi.additionalInvoiceItem = new ExternalChargeInvoiceItem(pluginInvoiceItemId, clock.getUTCNow(), invoices.get(0).getId(), account.getId(), null, null, null, null, null, null, null, null, "Update Description", clock.getUTCToday(), null, BigDecimal.ONE, null, Currency.USD, pluginLinkedItemId, null);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    final List<Invoice> invoices2 = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    final List<InvoiceItem> invoiceItems2 = invoices2.get(0).getInvoiceItems();
    final InvoiceItem externalCharge2 = Iterables.tryFind(invoiceItems2, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getInvoiceItemType() == InvoiceItemType.EXTERNAL_CHARGE;
        }
    }).orNull();
    assertNotNull(externalCharge2);
    assertEquals(externalCharge2.getAmount().compareTo(BigDecimal.ONE), 0);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 38 with DefaultEntitlement

use of org.killbill.billing.entitlement.api.DefaultEntitlement in project killbill by killbill.

the class TestWithInvoicePlugin method testRescheduledViaAPI.

@Test(groups = "slow")
public void testRescheduledViaAPI() throws Exception {
    testInvoicePluginApi.shouldAddTaxItem = false;
    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 0);
    // Create original subscription (Trial PHASE) -> $0 invoice
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 1);
    // Reschedule invoice generation at the time of the PHASE event
    testInvoicePluginApi.rescheduleDate = new DateTime(clock.getUTCNow()).plusDays(30);
    try {
        invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext);
        Assert.fail();
    } catch (final InvoiceApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_NOTHING_TO_DO.getCode());
    }
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 2);
    Assert.assertFalse(testInvoicePluginApi.wasRescheduled);
    // Let the next invoice go through
    testInvoicePluginApi.rescheduleDate = null;
    // Move to Evergreen PHASE: two invoice runs will be triggers, one by SubscriptionNotificationKey (PHASE event) and one by NextBillingDateNotificationKey (reschedule)
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 4);
    // Cannot check wasRescheduled flag, as it would be true only for one of the runs
    // Reschedule next invoice one month in the future
    testInvoicePluginApi.rescheduleDate = clock.getUTCNow().plusMonths(1);
    try {
        invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext);
        Assert.fail();
    } catch (final InvoiceApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_NOTHING_TO_DO.getCode());
    }
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 5);
    Assert.assertFalse(testInvoicePluginApi.wasRescheduled);
    // Let the next invoice go through
    testInvoicePluginApi.rescheduleDate = null;
    // Move one month ahead: no NULL_INVOICE this time: since there is already a notification for that date, the reschedule is a no-op (and we keep the isRescheduled flag to false)
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 6, 1), new LocalDate(2012, 7, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    Assert.assertEquals(testInvoicePluginApi.invocationCount, 6);
    Assert.assertFalse(testInvoicePluginApi.wasRescheduled);
}
Also used : Account(org.killbill.billing.account.api.Account) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 39 with DefaultEntitlement

use of org.killbill.billing.entitlement.api.DefaultEntitlement in project killbill by killbill.

the class TestWithTaxItems method testBasicTaxItems.

@Test(groups = "slow")
public void testBasicTaxItems() throws Exception {
    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // 
    // Create original subscription (Trial PHASE) -> $0 invoice.
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    // Move to Evergreen PHASE, but add AUTO_INVOICING_OFF => No invoice
    add_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    busHandler.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    // Add Cleaning ADD_ON => No Invoice
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Cleaning", ProductCategory.ADD_ON, BillingPeriod.MONTHLY);
    assertListenerStatus();
    // Make sure TestInvoicePluginApi will return an additional TAX item
    final UUID pluginInvoiceItemId = UUID.randomUUID();
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(pluginInvoiceItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));
    // Remove AUTO_INVOICING_OFF => Invoice + Payment
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("2.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(invoices.size(), 2);
    final List<InvoiceItem> invoiceItems = invoices.get(1).getInvoiceItems();
    final InvoiceItem taxItem = Iterables.tryFind(invoiceItems, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getInvoiceItemType() == InvoiceItemType.TAX;
        }
    }).orNull();
    assertNotNull(taxItem);
    // verify the ID is the one passed by the plugin #818
    assertEquals(taxItem.getId(), pluginInvoiceItemId);
    // Add AUTO_INVOICING_OFF and change to a higher plan on the same day that already include the 'Cleaning' ADD_ON, so it gets cancelled
    add_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.CANCEL, NextEvent.BLOCK);
    changeEntitlementAndCheckForCompletion(bpSubscription, "Shotgun", BillingPeriod.MONTHLY, BillingActionPolicy.IMMEDIATE);
    assertListenerStatus();
    // Make sure TestInvoicePluginApi will return an additional TAX item
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));
    // Remove AUTO_INVOICING_OFF => Invoice + Payment
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("2.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("249.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-2.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));
    // Add AUTO_INVOICING_OFF and change to a higher plan on the same day
    add_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    busHandler.pushExpectedEvent(NextEvent.CHANGE);
    changeEntitlementAndCheckForCompletion(bpSubscription, "Assault-Rifle", BillingPeriod.MONTHLY, BillingActionPolicy.IMMEDIATE);
    assertListenerStatus();
    // Make sure TestInvoicePluginApi will return an additional TAX item
    // Verify we support passing extra items for a null invoiceItemId #1182 (as advertised in our doc)
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(null, null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));
    // Remove AUTO_INVOICING_OFF => Invoice + Payment
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("249.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-2.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));
    invoiceChecker.checkInvoice(account.getId(), 4, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("599.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-249.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")));
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 40 with DefaultEntitlement

use of org.killbill.billing.entitlement.api.DefaultEntitlement in project killbill by killbill.

the class TestOverdueIntegration method testOverdueStagesFollowedWithImmediateChange2.

@Test(groups = "slow", description = "Test overdue stages and follow with an immediate change of plan and use of credit", retryAnalyzer = FlakyRetryAnalyzer.class)
public void testOverdueStagesFollowedWithImmediateChange2() throws Exception {
    // 2012-05-01T00:03:42.000Z
    clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
    setupAccount();
    // Set next invoice to fail and create subscription
    paymentPlugin.makeAllInvoicesFailWithError(true);
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, BillingPeriod.ANNUAL, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    bundle = subscriptionApi.getSubscriptionBundle(baseEntitlement.getBundleId(), callContext);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 5, 1), callContext);
    // 2012-05-31 => DAY 30 have to get out of trial {I0, P0}
    addDaysAndCheckForCompletion(30, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 31), new LocalDate(2013, 5, 31), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2013, 5, 31), callContext);
    // 2012-06-08 => Retry P0
    addDaysAndCheckForCompletion(8, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    checkODState(OverdueWrapper.CLEAR_STATE_NAME);
    // 2012-06-16 => Retry P0
    addDaysAndCheckForCompletion(8, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    checkODState(OverdueWrapper.CLEAR_STATE_NAME);
    // 2012-06-24 => Retry P0
    addDaysAndCheckForCompletion(8, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    checkODState(OverdueWrapper.CLEAR_STATE_NAME);
    // 2012-06-30 => OD1
    addDaysAndCheckForCompletion(6, NextEvent.BLOCK);
    checkODState("OD1");
    // 2012-07-02 => Retry P0
    addDaysAndCheckForCompletion(2, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    checkODState("OD1");
    // 2012-07-10 => Retry P0 & transition to OD2
    addDaysAndCheckForCompletion(8, NextEvent.BLOCK, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.TAG);
    checkODState("OD2");
    // 2012-07-18 => Retry P0
    addDaysAndCheckForCompletion(8, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    checkODState("OD2");
    // 2012-07-20 => OD3
    addDaysAndCheckForCompletion(2, NextEvent.BLOCK);
    checkODState("OD3");
    allowPaymentsAndResetOverdueToClearByPayingAllUnpaidInvoices(false);
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 31), new LocalDate(2013, 5, 31), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 10), new LocalDate(2012, 7, 20), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-65.75")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 31), new LocalDate(2013, 5, 31), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-1998.86")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 20), new LocalDate(2012, 7, 20), InvoiceItemType.CBA_ADJ, new BigDecimal("2064.61")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2013, 5, 31), callContext);
    // Move to 2012-07-31 and make a change of plan
    addDaysAndCheckForCompletion(11);
    checkChangePlanWithOverdueState(baseEntitlement, false, false);
    invoiceChecker.checkInvoice(account.getId(), 4, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 31), new LocalDate(2012, 8, 31), InvoiceItemType.RECURRING, new BigDecimal("599.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 31), new LocalDate(2012, 7, 31), InvoiceItemType.CBA_ADJ, new BigDecimal("-599.95")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 8, 31), callContext);
    assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(new BigDecimal("-1464.66")), 0);
}
Also used : DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)202 Test (org.testng.annotations.Test)186 LocalDate (org.joda.time.LocalDate)167 BigDecimal (java.math.BigDecimal)148 Account (org.killbill.billing.account.api.Account)144 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)138 Invoice (org.killbill.billing.invoice.api.Invoice)129 DateTime (org.joda.time.DateTime)98 AccountData (org.killbill.billing.account.api.AccountData)72 ArrayList (java.util.ArrayList)42 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)42 UUID (java.util.UUID)28 Payment (org.killbill.billing.payment.api.Payment)27 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)23 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)20 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)20 ExpectedPaymentCheck (org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck)16 Entitlement (org.killbill.billing.entitlement.api.Entitlement)16 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)13 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)13