Search in sources :

Example 81 with DefaultEntitlementSpecifier

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

the class TestMigrationSubscriptions method testMigrationWithMultipleBundlesAndDifferentDates.

@Test(groups = "slow")
public void testMigrationWithMultipleBundlesAndDifferentDates() throws Exception {
    clock.setDay(new LocalDate(2016, 1, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDateBundle1 = new LocalDate(2015, 12, 20);
    final LocalDate billingMigrationDateBundle1 = new LocalDate(2016, 2, 1);
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDateBundle2 = new LocalDate(2015, 12, 20);
    final LocalDate billingMigrationDateBundle2 = new LocalDate(2016, 3, 1);
    final PlanPhaseSpecifier baseSpec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final PlanPhaseSpecifier addOnSpec1 = new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final String externalKey = "baseExternalKey";
    EntitlementSpecifier baseEntitlementSpecifier = new DefaultEntitlementSpecifier(baseSpec);
    EntitlementSpecifier addOnEntitlementSpecifier1 = new DefaultEntitlementSpecifier(addOnSpec1);
    final List<EntitlementSpecifier> specifierList = new ArrayList<EntitlementSpecifier>();
    specifierList.add(baseEntitlementSpecifier);
    specifierList.add(addOnEntitlementSpecifier1);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.BLOCK, NextEvent.BLOCK, NextEvent.BLOCK);
    BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifierBundle1 = buildBaseEntitlementWithAddOnsSpecifier(entitlementMigrationDateBundle1, billingMigrationDateBundle1, externalKey, specifierList);
    List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifierBundle2 = buildBaseEntitlementWithAddOnsSpecifier(entitlementMigrationDateBundle2, billingMigrationDateBundle2, externalKey, specifierList);
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifierBundle1);
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifierBundle2);
    final List<UUID> baseEntitlements = entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), baseEntitlementWithAddOnsSpecifierList, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Assert.assertEquals(entitlementApi.getEntitlementForId(baseEntitlements.get(0), callContext).getState(), EntitlementState.ACTIVE);
    Assert.assertEquals(entitlementApi.getEntitlementForId(baseEntitlements.get(1), callContext).getState(), EntitlementState.ACTIVE);
    // Billing starts straight on EVERGREEN for Bundle 1 after 1 month
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
    // Billing starts straight on EVERGREEN for Bundle 2 after 2 months
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
    // Next month we should still get one single invoice and payment / invoice payment
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) AccountData(org.killbill.billing.account.api.AccountData) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 82 with DefaultEntitlementSpecifier

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

the class TestMigrationSubscriptions method testSimpleMigrationBP.

// 
// Scenario: On 2016-1-1, we decide to migrate a subscription with a cutOverDate of 2016-1-10 (10 days in the future) and a billing date of 2016-1-31
// (note that 2016-1-31 + 30 days trial = 2016-03-01, which aligns well with the BCD=1 we set on our test account)
// 
// 
@Test(groups = "slow")
public void testSimpleMigrationBP() throws Exception {
    clock.setDay(new LocalDate(2016, 1, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDate = new LocalDate(2016, 1, 10);
    final LocalDate billingMigrationDate = new LocalDate(2016, 1, 31);
    // Entitlement wil be created in PENDING state
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "bundleKey", entitlementMigrationDate, billingMigrationDate, false, true, ImmutableList.<PluginProperty>of(), callContext);
    final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
    Assert.assertEquals(entitlement.getState(), EntitlementState.PENDING);
    // Move clock to entitlementMigrationDate (migration cutOverDate), and expect the associated event
    busHandler.pushExpectedEvents(NextEvent.BLOCK);
    clock.addDays(10);
    assertListenerStatus();
    final Entitlement activeEntitlement = entitlementApi.getEntitlementForId(entitlement.getId(), callContext);
    Assert.assertEquals(activeEntitlement.getState(), EntitlementState.ACTIVE);
    // Move clock to billingMigrationDate and expect the CREATE event along a $0 invoice for the trial
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.INVOICE);
    clock.addDays(21);
    assertListenerStatus();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(billingMigrationDate, null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, expectedInvoices);
    expectedInvoices.clear();
    // Move clock next month for first RECURRING invoice
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    final LocalDate startDate = billingMigrationDate.plusDays(30);
    final LocalDate endDate = startDate.plusMonths(1);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(startDate, endDate, InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    expectedInvoices.clear();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) AccountData(org.killbill.billing.account.api.AccountData) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 83 with DefaultEntitlementSpecifier

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

the class TestMigrationSubscriptions method testSimpleMigrationBundle.

// 
// Scenario: On 2016-1-1, we decide to migrate a bundle (one BP and one AO). We migrate straight to EVERGREEN phase. The scenario is very similar to previous one
// but with an additional AO (by using the createBaseEntitlementWithAddOns api).
// 
// Note that while convenient to migrate a bundle at once (BP + AOS), one could do several calls for each subscription. The advantage of the later approach is that
// the granularity in terms of alignments for when things start and which phase are skipped can be better controlled.
// 
@Test(groups = "slow")
public void testSimpleMigrationBundle() throws Exception {
    clock.setDay(new LocalDate(2016, 1, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDate = new LocalDate(2015, 12, 20);
    final LocalDate billingMigrationDate = new LocalDate(2016, 2, 1);
    final PlanPhaseSpecifier baseSpec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final PlanPhaseSpecifier addOnSpec1 = new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final String externalKey = "baseExternalKey";
    EntitlementSpecifier baseEntitlementSpecifier = new DefaultEntitlementSpecifier(baseSpec);
    EntitlementSpecifier addOnEntitlementSpecifier1 = new DefaultEntitlementSpecifier(addOnSpec1);
    final List<EntitlementSpecifier> specifierList = new ArrayList<EntitlementSpecifier>();
    specifierList.add(baseEntitlementSpecifier);
    specifierList.add(addOnEntitlementSpecifier1);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.BLOCK);
    BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = buildBaseEntitlementWithAddOnsSpecifier(entitlementMigrationDate, billingMigrationDate, externalKey, specifierList);
    List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final List<UUID> baseEntitlements = entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), baseEntitlementWithAddOnsSpecifierList, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement entitlement = entitlementApi.getEntitlementForId(baseEntitlements.get(0), callContext);
    Assert.assertEquals(entitlement.getState(), EntitlementState.ACTIVE);
    // Billing starts straight on EVERGREEN
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) AccountData(org.killbill.billing.account.api.AccountData) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) UUID(java.util.UUID) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Test(org.testng.annotations.Test)

Example 84 with DefaultEntitlementSpecifier

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

the class TestIntegrationParentInvoice method testParentInvoiceGenerationMultipleActionsSameDay.

@Test(groups = "slow")
public void testParentInvoiceGenerationMultipleActionsSameDay() throws Exception {
    final int billingDay = 14;
    final DateTime initialCreationDate = new DateTime(2015, 5, 15, 0, 0, 0, 0, testTimeZone);
    // set clock to the initial start date
    clock.setTime(initialCreationDate);
    final Account parentAccount = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    final Account childAccount = createAccountWithNonOsgiPaymentMethod(getChildAccountData(billingDay, parentAccount.getId(), true));
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE NextEvent.INVOICE
    DefaultEntitlement baseEntitlementChild = createBaseEntitlementAndCheckForCompletion(childAccount.getId(), "bundleKey1", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // Moving a day the NotificationQ calls the commitInvoice. No payment is expected.
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    clock.addDays(1);
    assertListenerStatus();
    // Move through time and verify new parent Invoice. No payments are expected yet.
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE);
    clock.addDays(29);
    assertListenerStatus();
    // check parent Invoice with child plan amount
    List<Invoice> parentInvoices = invoiceUserApi.getInvoicesByAccount(parentAccount.getId(), false, false, callContext);
    assertEquals(parentInvoices.size(), 2);
    Invoice parentInvoice = parentInvoices.get(1);
    assertEquals(parentInvoice.getNumberOfItems(), 1);
    assertEquals(parentInvoice.getStatus(), InvoiceStatus.DRAFT);
    assertTrue(parentInvoice.isParentInvoice());
    // balance is 0 because parent invoice status is DRAFT
    assertEquals(parentInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
    assertEquals(parentInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.valueOf(29.95)), 0);
    // upgrade plan
    busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, baseEntitlementChild.getLastActivePriceList().getName());
    baseEntitlementChild.changePlanWithDate(new DefaultEntitlementSpecifier(spec), null, null, callContext);
    assertListenerStatus();
    // check parent invoice. Expected to have the same invoice item with the amount updated
    final List<Invoice> childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, false, callContext);
    parentInvoices = invoiceUserApi.getInvoicesByAccount(parentAccount.getId(), false, false, callContext);
    assertEquals(parentInvoices.size(), 2);
    parentInvoice = parentInvoices.get(1);
    assertEquals(parentInvoice.getNumberOfItems(), 1);
    assertEquals(parentInvoice.getStatus(), InvoiceStatus.DRAFT);
    assertTrue(parentInvoice.isParentInvoice());
    // balance is 0 because parent invoice status is DRAFT
    assertEquals(parentInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
    // Moving a day the NotificationQ calls the commitInvoice. Now payment is expected
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(1);
    assertListenerStatus();
    parentInvoice = invoiceUserApi.getInvoice(parentInvoice.getId(), callContext);
    assertEquals(parentInvoice.getStatus(), InvoiceStatus.COMMITTED);
    assertEquals(parentInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 85 with DefaultEntitlementSpecifier

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

the class TestWithInvoiceOptimization method testBillRunInArrearLeadingProration.

@Test(groups = "slow")
public void testBillRunInArrearLeadingProration() throws Exception {
    invoiceConfig.setMaxInvoiceLimit(new Period("P1m"));
    clock.setTime(new DateTime("2021-01-15T3:56:02"));
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    assertNotNull(account);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("blowdart-in-arrear-monthly-notrial");
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "Something", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // 2021-02-01
    clock.addDays(16);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    invoiceUserApi.triggerInvoiceGeneration(account.getId(), new LocalDate(2021, 2, 28), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2021, 1, 15), new LocalDate(2021, 2, 1), InvoiceItemType.RECURRING, new BigDecimal("54.84")));
    // 2021-03-01
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    invoiceUserApi.triggerInvoiceGeneration(account.getId(), new LocalDate(2021, 3, 31), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2021, 2, 1), new LocalDate(2021, 3, 1), InvoiceItemType.RECURRING, new BigDecimal("100.00")));
    // 2021-04-01
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    invoiceUserApi.triggerInvoiceGeneration(account.getId(), new LocalDate(2021, 4, 30), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2021, 3, 1), new LocalDate(2021, 4, 1), InvoiceItemType.RECURRING, new BigDecimal("100.00")));
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) Period(org.joda.time.Period) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)141 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)140 Test (org.testng.annotations.Test)129 LocalDate (org.joda.time.LocalDate)105 Account (org.killbill.billing.account.api.Account)98 UUID (java.util.UUID)87 Entitlement (org.killbill.billing.entitlement.api.Entitlement)64 BigDecimal (java.math.BigDecimal)62 DateTime (org.joda.time.DateTime)61 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)59 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)56 Invoice (org.killbill.billing.invoice.api.Invoice)40 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)27 ArrayList (java.util.ArrayList)24 AccountData (org.killbill.billing.account.api.AccountData)23 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)20 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)14 BlockingState (org.killbill.billing.entitlement.api.BlockingState)13 Interval (org.joda.time.Interval)12 Period (org.joda.time.Period)12