Search in sources :

Example 6 with DefaultEntitlement

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

the class TestIntegration method testForMultipleRecurringPhases.

@Test(groups = "slow")
public void testForMultipleRecurringPhases() throws Exception {
    final DateTime initialCreationDate = new DateTime(2012, 2, 1, 0, 3, 42, 0, testTimeZone);
    clock.setDeltaFromReality(initialCreationDate.getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(2));
    final UUID accountId = account.getId();
    final String productName = "Blowdart";
    final String planSetName = "DEFAULT";
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", productName, ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, false, callContext);
    assertNotNull(invoices);
    assertTrue(invoices.size() == 1);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
    assertListenerStatus();
    invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, false, callContext);
    assertNotNull(invoices);
    assertEquals(invoices.size(), 2);
    for (int i = 0; i < 5; i++) {
        log.info("============== loop number " + i + "=======================");
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
        assertListenerStatus();
    }
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
    assertListenerStatus();
    invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, false, callContext);
    assertNotNull(invoices);
    assertEquals(invoices.size(), 8);
    for (int i = 0; i <= 5; i++) {
        log.info("============== second loop number " + i + "=======================");
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
        assertListenerStatus();
    }
    invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, false, callContext);
    assertNotNull(invoices);
    assertEquals(invoices.size(), 14);
    assertListenerStatus();
    checkNoMoreInvoiceToGenerate(account);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 7 with DefaultEntitlement

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

the class TestIntegration method testBasePlanCompleteWithBillingDayAlignedWithTrial.

@Test(groups = "slow")
public void testBasePlanCompleteWithBillingDayAlignedWithTrial() throws Exception {
    final int billingDay = 2;
    final DateTime initialCreationDate = new DateTime(2012, 2, 1, 0, 3, 42, 0, testTimeZone);
    // set clock to the initial start date
    clock.setTime(initialCreationDate);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    int invoiceItemCount = 1;
    // 
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    // 
    DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    DefaultSubscriptionBase subscription = subscriptionDataFromSubscription(baseEntitlement.getSubscriptionBase());
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(initialCreationDate.toLocalDate(), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    // No end date for the trial item (fixed price of zero), and CTD should be today (i.e. when the trial started)
    invoiceChecker.checkChargedThroughDate(subscription.getId(), clock.getUTCToday(), callContext);
    // 
    // CHANGE PLAN IMMEDIATELY AND EXPECT BOTH EVENTS: NextEvent.CHANGE NextEvent.INVOICE
    // 
    // Make sure CHANGE does not exactly align with CREATE
    clock.addDeltaFromReality(1000);
    changeEntitlementAndCheckForCompletion(baseEntitlement, "Assault-Rifle", BillingPeriod.MONTHLY, null, NextEvent.CHANGE, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(initialCreationDate.toLocalDate(), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkChargedThroughDate(subscription.getId(), clock.getUTCToday(), callContext);
    // 
    // MOVE 4 * TIME THE CLOCK
    // 
    setDateAndCheckForCompletion(new DateTime(2012, 2, 28, 23, 59, 59, 0, testTimeZone));
    setDateAndCheckForCompletion(new DateTime(2012, 2, 29, 23, 59, 59, 0, testTimeZone));
    setDateAndCheckForCompletion(new DateTime(2012, 3, 1, 23, 59, 59, 0, testTimeZone));
    setDateAndCheckForCompletion(new DateTime(2012, 3, 2, 23, 59, 59, 0, testTimeZone), NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("599.95")));
    invoiceChecker.checkChargedThroughDate(subscription.getId(), new LocalDate(2012, 4, 2), callContext);
    // 
    // CHANGE PLAN EOT AND EXPECT NOTHING
    // 
    final TestDryRunArguments dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, null, null, SubscriptionEventType.CHANGE, subscription.getId(), subscription.getBundleId(), null, null);
    try {
        invoiceUserApi.triggerDryRunInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
        Assert.fail("Call should return no invoices");
    } catch (final InvoiceApiException e) {
        assertEquals(e.getCode(), INVOICE_NOTHING_TO_DO.getCode());
    }
    baseEntitlement = changeEntitlementAndCheckForCompletion(baseEntitlement, "Pistol", BillingPeriod.MONTHLY, null);
    subscription = subscriptionDataFromSubscription(baseEntitlement.getSubscriptionBase());
    // 
    // MOVE TIME AFTER CTD AND EXPECT BOTH EVENTS : NextEvent.CHANGE NextEvent.INVOICE
    // 
    final LocalDate firstRecurringPistolDate = subscription.getChargedThroughDate().toLocalDate();
    final LocalDate secondRecurringPistolDate = firstRecurringPistolDate.plusMonths(1);
    addDaysAndCheckForCompletion(31, NextEvent.CHANGE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 2), new LocalDate(2012, 5, 2), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkChargedThroughDate(subscription.getId(), secondRecurringPistolDate, callContext);
    // 
    // MOVE 3 * TIME AFTER NEXT BILL CYCLE DAY AND EXPECT EVENT : NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT
    // 
    addDaysAndCheckForCompletion(31, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 2), new LocalDate(2012, 6, 2), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkChargedThroughDate(subscription.getId(), new LocalDate(2012, 6, 2), callContext);
    addDaysAndCheckForCompletion(31, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 6, 2), new LocalDate(2012, 7, 2), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkChargedThroughDate(subscription.getId(), new LocalDate(2012, 7, 2), callContext);
    addDaysAndCheckForCompletion(31, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceChecker.checkInvoice(account.getId(), invoiceItemCount++, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 2), new LocalDate(2012, 8, 2), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkChargedThroughDate(subscription.getId(), new LocalDate(2012, 8, 2), callContext);
    // 
    // FINALLY CANCEL SUBSCRIPTION EOT
    // 
    cancelEntitlementAndCheckForCompletion(baseEntitlement, NextEvent.BLOCK);
    // MOVE AFTER CANCEL DATE
    addDaysAndCheckForCompletion(31, NextEvent.CANCEL, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE);
    invoiceChecker.checkChargedThroughDate(subscription.getId(), new LocalDate(2012, 8, 2), callContext);
    checkNoMoreInvoiceToGenerate(account);
}
Also used : Account(org.killbill.billing.account.api.Account) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) 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)

Example 8 with DefaultEntitlement

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

the class TestIntegration method testCancelBPWithAOTheSameDay.

@Test(groups = "slow")
public void testCancelBPWithAOTheSameDay() 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 List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    // 
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    // 
    TestDryRunArguments dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, null, null, SubscriptionEventType.START_BILLING, null, null, null, null);
    Invoice dryRunInvoice = invoiceUserApi.triggerDryRunInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, expectedInvoices);
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // Check bundle after BP got created otherwise we get an error from auditApi.
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, expectedInvoices);
    expectedInvoices.clear();
    // 
    // ADD ADD_ON ON THE SAME DAY
    // 
    dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Telescopic-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, null, null, SubscriptionEventType.START_BILLING, null, bpSubscription.getBundleId(), null, null);
    dryRunInvoice = invoiceUserApi.triggerDryRunInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("399.95")));
    invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, expectedInvoices);
    addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Telescopic-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final Invoice invoice = invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    paymentChecker.checkPayment(account.getId(), 1, callContext, new ExpectedPaymentCheck(new LocalDate(2012, 4, 1), new BigDecimal("399.95"), TransactionStatus.SUCCESS, invoice.getId(), Currency.USD));
    expectedInvoices.clear();
    // 
    // CANCEL BP ON THE SAME DAY (we should have two cancellations, BP and AO)
    // There is no invoice created as we only adjust the previous invoice.
    // 
    dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, null, null, null, null, null, SubscriptionEventType.STOP_BILLING, bpSubscription.getId(), bpSubscription.getBundleId(), null, null);
    dryRunInvoice = invoiceUserApi.triggerDryRunInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-399.95")));
    // The second invoice should be adjusted for the AO (we paid for the full period) and since we paid we should also see a CBA
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("399.95"), false));
    invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, expectedInvoices);
    cancelEntitlementAndCheckForCompletion(bpSubscription, NextEvent.BLOCK, NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.CANCEL, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, expectedInvoices);
    checkNoMoreInvoiceToGenerate(account);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) ExpectedPaymentCheck(org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck) Test(org.testng.annotations.Test)

Example 9 with DefaultEntitlement

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

the class TestIntegration method testWithPauseResume.

@Test(groups = "slow")
public void testWithPauseResume() throws Exception {
    final DateTime initialDate = new DateTime(2012, 2, 1, 0, 3, 42, 0, testTimeZone);
    final int billingDay = 2;
    // set clock to the initial start date
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    final UUID accountId = account.getId();
    assertNotNull(account);
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
    // 
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    // 
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    assertNotNull(baseEntitlement);
    // 
    // VERIFY CTD HAS BEEN SET
    // 
    final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) baseEntitlement.getSubscriptionBase();
    final DateTime startDate = subscription.getCurrentPhaseStart();
    final BigDecimal rate = subscription.getCurrentPhase().getFixed().getPrice().getPrice(Currency.USD);
    verifyTestResult(accountId, subscription.getId(), startDate, null, rate, clock.getUTCNow(), 1);
    // 
    // MOVE TIME TO AFTER TRIAL AND EXPECT BOTH EVENTS :  NextEvent.PHASE NextEvent.INVOICE
    // 
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // 2012-3-4
    clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    // PAUSE THE ENTITLEMENT
    DefaultEntitlement entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.INVOICE);
    entitlementApi.pause(entitlement.getBundleId(), clock.getUTCNow().toLocalDate(), ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 4), new LocalDate(2012, 4, 2), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-233.82")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 4), new LocalDate(2012, 3, 4), InvoiceItemType.CBA_ADJ, new BigDecimal("233.82")));
    entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    Assert.assertEquals(entitlement.getState(), EntitlementState.BLOCKED);
    // MOVE CLOCK FORWARD ADN CHECK THERE IS NO NEW INVOICE
    // 2012-4-5
    clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.NULL_INVOICE, NextEvent.INVOICE);
    entitlementApi.resume(entitlement.getBundleId(), clock.getUTCNow().toLocalDate(), ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 4, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 5), new LocalDate(2012, 5, 2), InvoiceItemType.RECURRING, new BigDecimal("224.96")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 5), new LocalDate(2012, 4, 5), InvoiceItemType.CBA_ADJ, new BigDecimal("-224.96")));
    // Verify next invoice
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // 2012-5-2
    clock.addDays(27);
    assertListenerStatus();
    checkNoMoreInvoiceToGenerate(account);
}
Also used : Account(org.killbill.billing.account.api.Account) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) UUID(java.util.UUID) 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)

Example 10 with DefaultEntitlement

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

the class TestIntegration method testWithPauseResumeAfterENT_CANCELLEDBlockingState.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/715")
public void testWithPauseResumeAfterENT_CANCELLEDBlockingState() throws Exception {
    final DateTime initialDate = new DateTime(2012, 2, 1, 0, 3, 42, 0, testTimeZone);
    final int billingDay = 2;
    // set clock to the initial start date
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    final UUID accountId = account.getId();
    assertNotNull(account);
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    // 
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    // 
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    assertNotNull(baseEntitlement);
    // 
    // VERIFY CTD HAS BEEN SET
    // 
    final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) baseEntitlement.getSubscriptionBase();
    final DateTime startDate = subscription.getCurrentPhaseStart();
    final BigDecimal rate = subscription.getCurrentPhase().getFixed().getPrice().getPrice(Currency.USD);
    verifyTestResult(accountId, subscription.getId(), startDate, null, rate, clock.getUTCNow(), 1);
    // 
    // MOVE TIME TO AFTER TRIAL (2012-03-04)
    // 
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
    assertListenerStatus();
    invoiceChecker.checkInvoice(accountId, 1, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 2, 1), null, InvoiceItemType.FIXED, BigDecimal.ZERO)));
    invoiceChecker.checkInvoice(accountId, 2, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("249.95"))));
    // Pause the entitlement between 2012-03-05 and 2012-03-15
    DefaultEntitlement entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    entitlementApi.pause(entitlement.getBundleId(), new LocalDate(2012, 3, 5), ImmutableList.<PluginProperty>of(), callContext);
    entitlementApi.resume(entitlement.getBundleId(), new LocalDate(2012, 3, 15), ImmutableList.<PluginProperty>of(), callContext);
    // Advance clock to 2012-03-07
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.INVOICE);
    clock.addDays(3);
    assertListenerStatus();
    invoiceChecker.checkInvoice(accountId, 2, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("249.95"))));
    invoiceChecker.checkInvoice(accountId, 3, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 5), new LocalDate(2012, 4, 2), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-225.76")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 7), new LocalDate(2012, 3, 7), InvoiceItemType.CBA_ADJ, new BigDecimal("225.76"))));
    // Entitlement should be blocked
    entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    Assert.assertEquals(entitlement.getState(), EntitlementState.BLOCKED);
    // Advance clock to 2012-03-12, nothing should happen
    clock.addDays(5);
    assertListenerStatus();
    // Entitlement is still blocked
    entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    Assert.assertEquals(entitlement.getState(), EntitlementState.BLOCKED);
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, false, callContext);
    assertEquals(invoices.size(), 3);
    // Cancel entitlement start of term but with billing policy immediate (ENT_BLOCKED must be after ENT_CANCELLED to trigger the bug)
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.NULL_INVOICE);
    baseEntitlement.cancelEntitlementWithDateOverrideBillingPolicy(new LocalDate(2012, 3, 2), BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // 2012-03-16
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    clock.addDays(4);
    assertListenerStatus();
    // 2012-04-02
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE);
    clock.addMonths(17);
    assertListenerStatus();
    // No new invoices
    invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, false, callContext);
    assertEquals(invoices.size(), 3);
    checkNoMoreInvoiceToGenerate(account);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) UUID(java.util.UUID) 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