Search in sources :

Example 6 with BillingPeriod

use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.

the class TestCatalogRetireElements method testRetirePlanWithUncancel.

@Test(groups = "slow")
public void testRetirePlanWithUncancel() throws Exception {
    // Catalog v1 starts in 2011-01-01
    // Catalog v2 starts in 2015-12-01
    final LocalDate today = new LocalDate(2015, 10, 5);
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(today);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    final String productName = "Pistol";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    Entitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    assertNotNull(bpEntitlement);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
    assertEquals(bpEntitlement.getState(), EntitlementState.ACTIVE);
    // Move out a month.
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    // Cancel entitlement
    bpEntitlement = bpEntitlement.cancelEntitlementWithDate(new LocalDate("2016-05-01"), true, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(bpEntitlement.getState(), EntitlementState.ACTIVE);
    assertListenerStatus();
    // Move out a month.
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    // Catalog v2 should start now.
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(productName, term, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    try {
        entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "externalKey2", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
        // force to fail is there is not an exception
        fail();
    } catch (final EntitlementApiException e) {
        assertEquals(e.getCode(), ErrorCode.CAT_PLAN_NOT_FOUND.getCode());
    }
    // Uncancel entitlement
    busHandler.pushExpectedEvents(NextEvent.UNCANCEL);
    bpEntitlement.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Move out a month and verify 'Pistol' plan continue working as expected.
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(invoices.size(), 4);
    for (final Invoice invoice : invoices) {
        assertEquals(invoice.getInvoiceItems().get(0).getPlanName(), "pistol-monthly");
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 7 with BillingPeriod

use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.

the class TestCatalogRetireElements method testRetirePriceList.

@Test(groups = "slow")
public void testRetirePriceList() throws Exception {
    // Catalog v1 starts in 2011-01-01
    // Catalog v2 starts in 2015-12-01
    // Catalog v3 starts in 2016-01-01
    final LocalDate today = new LocalDate(2015, 11, 1);
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(today);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    // accountUserApi.createAccount(getAccountData(1), callContext);
    final String productName = "Pistol";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(productName, term, "SpecialDiscount", null);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final UUID bpEntitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "externalKey", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement bpEntitlement = entitlementApi.getEntitlementForId(bpEntitlementId, callContext);
    assertNotNull(bpEntitlement);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
    // Move out a month.
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    // Move out a month.
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    try {
        entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "externalKey2", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
        // force to fail is there is not an exception
        fail();
    } catch (final EntitlementApiException e) {
        assertTrue(e.getLocalizedMessage().startsWith("Could not find any product named 'Pistol'"));
    }
    // Move out a month and verify 'Pistol' plan continue working as expected.
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    // Move out a month.
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(invoices.size(), 5);
    assertTrue(invoices.get(0).getInvoiceItems().get(0).getPhaseName().equals("discount-pistol-monthly-trial"));
    assertTrue(invoices.get(1).getInvoiceItems().get(0).getPhaseName().equals("discount-pistol-monthly-discount"));
    assertTrue(invoices.get(2).getInvoiceItems().get(0).getPhaseName().equals("discount-pistol-monthly-discount"));
    assertTrue(invoices.get(3).getInvoiceItems().get(0).getPhaseName().equals("discount-pistol-monthly-discount"));
    assertTrue(invoices.get(4).getInvoiceItems().get(0).getPhaseName().equals("discount-pistol-monthly-evergreen"));
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) 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)

Example 8 with BillingPeriod

use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.

the class TestCatalogRetireElements method testRetirePlanWithUncancelAfterChange.

@Test(groups = "slow")
public void testRetirePlanWithUncancelAfterChange() throws Exception {
    // Catalog v1 starts in 2011-01-01
    // Catalog v3 starts in 2016-01-01
    final LocalDate today = new LocalDate(2015, 7, 5);
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(today);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(null));
    final String productName = "Pistol";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final PlanPhaseSpecifier spec1 = new PlanPhaseSpecifier(productName, term, "DEFAULT", null);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final UUID bpEntitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec1), "externalKey", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Entitlement bpEntitlement = entitlementApi.getEntitlementForId(bpEntitlementId, callContext);
    assertNotNull(bpEntitlement);
    assertEquals(bpEntitlement.getLastActivePhase().getPhaseType(), PhaseType.TRIAL);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
    // Move out after trial (2015-08-04)
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    assertEquals(entitlementApi.getEntitlementForId(bpEntitlement.getId(), callContext).getLastActivePhase().getPhaseType(), PhaseType.EVERGREEN);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 2);
    // 2015-08-05
    clock.addDays(1);
    assertListenerStatus();
    // Change to discount phase in SpecialDiscount pricelist (CBA generated, no payment)
    // Note that we need to trigger a CHANGE outside a TRIAL phase to generate a CHANGE event (otherwise, a CREATE is generated)
    final PlanPhaseSpecifier spec2 = new PlanPhaseSpecifier(productName, term, "SpecialDiscount", null);
    busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE);
    bpEntitlement = bpEntitlement.changePlanWithDate(new DefaultEntitlementSpecifier(spec2), clock.getUTCToday(), ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    assertEquals(entitlementApi.getEntitlementForId(bpEntitlement.getId(), callContext).getLastActivePhase().getPhaseType(), PhaseType.DISCOUNT);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 3);
    // Cancel entitlement
    bpEntitlement = bpEntitlement.cancelEntitlementWithDate(new LocalDate("2016-05-01"), true, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(bpEntitlement.getState(), EntitlementState.ACTIVE);
    assertListenerStatus();
    // Move out after discount phase (happens on 2015-11-04)
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // 2015-09-05
    clock.addMonths(1);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // 2015-10-05
    clock.addMonths(1);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // 2015-11-05
    clock.addMonths(1);
    // This verifies the PlanAligner.getNextTimedPhase codepath with a CHANGE transition type
    assertListenerStatus();
    assertEquals(entitlementApi.getEntitlementForId(bpEntitlement.getId(), callContext).getLastActivePhase().getPhaseType(), PhaseType.EVERGREEN);
    // Move out a month (2015-12-05)
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    // Uncancel entitlement
    busHandler.pushExpectedEvents(NextEvent.UNCANCEL);
    bpEntitlement.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Move out a month (2016-01-01)
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    try {
        entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec2), "externalKey2", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
        // force to fail is there is not an exception
        fail();
    } catch (final EntitlementApiException e) {
        assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PRODUCT.getCode());
    }
    // Move out a month and verify 'Pistol' discounted plan continues working as expected.
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(invoices.size(), 9);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) 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)

Example 9 with BillingPeriod

use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.

the class TestCustomFieldApi method testCustomFieldForInvoice.

@Test(groups = "slow")
public void testCustomFieldForInvoice() throws CustomFieldApiException, SubscriptionBaseApiException {
    // 
    // Create necessary logic to end up with an Invoice object on that account.
    // 
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.ANNUAL;
    final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
    final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    assertNotNull(bpEntitlement);
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    Assert.assertEquals(invoices.size(), 1);
    final Invoice invoice = invoices.get(0);
    Assert.assertEquals(invoice.getAccountId(), account.getId());
    addCustomField("name1", "value1", invoice.getId(), ObjectType.INVOICE, clock.getUTCNow());
    addCustomField("name2", "value2", invoice.getId(), ObjectType.INVOICE, clock.getUTCNow());
    List<CustomField> fields = customFieldApi.getCustomFieldsForAccount(invoice.getId(), callContext);
    Assert.assertEquals(fields.size(), 2);
    fields = customFieldApi.getCustomFieldsForAccountType(invoice.getId(), ObjectType.INVOICE, callContext);
    Assert.assertEquals(fields.size(), 2);
    fields = customFieldApi.getCustomFieldsForObject(invoice.getId(), ObjectType.INVOICE, callContext);
    Assert.assertEquals(fields.size(), 2);
    // 
    // Add custom field on account and retry
    // 
    addCustomField("foo", "bar", account.getId(), ObjectType.ACCOUNT, clock.getUTCNow());
    fields = customFieldApi.getCustomFieldsForAccount(invoice.getId(), callContext);
    Assert.assertEquals(fields.size(), 3);
    fields = customFieldApi.getCustomFieldsForAccountType(invoice.getId(), ObjectType.INVOICE, callContext);
    Assert.assertEquals(fields.size(), 2);
    fields = customFieldApi.getCustomFieldsForObject(invoice.getId(), ObjectType.INVOICE, callContext);
    Assert.assertEquals(fields.size(), 2);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) Test(org.testng.annotations.Test)

Example 10 with BillingPeriod

use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.

the class TestCatalogNewEntries method testNewPlan.

@Test(groups = "slow")
public void testNewPlan() throws Exception {
    // We start on the first version of the catalog where 'shotgun-monthly' exists but bazooka-monthly does not yet exist
    final LocalDate today = new LocalDate(2015, 11, 1);
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(today);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    assertNotNull(bpEntitlement);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
    assertEquals(bpEntitlement.getSubscriptionBase().getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.MONTHLY);
    final PlanPhaseSpecifier bazookaSpec = new PlanPhaseSpecifier("bazooka-monthly");
    try {
        bpEntitlement.changePlanWithDate(new DefaultEntitlementSpecifier(bazookaSpec), null, null, callContext);
        Assert.fail("Change plan should fail because plan does not yet exist");
    } catch (EntitlementApiException ignore) {
    }
    // Move to 2015-12-1
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    // We will use the version 2 of the catalog which contains the new plan bazooka-monthly
    busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    bpEntitlement.changePlanWithDate(new DefaultEntitlementSpecifier(bazookaSpec), new LocalDate(2015, 12, 1), null, callContext);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)111 Test (org.testng.annotations.Test)91 LocalDate (org.joda.time.LocalDate)61 DateTime (org.joda.time.DateTime)59 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)45 BigDecimal (java.math.BigDecimal)44 Account (org.killbill.billing.account.api.Account)44 Invoice (org.killbill.billing.invoice.api.Invoice)40 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)31 Plan (org.killbill.billing.catalog.api.Plan)20 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)20 Interval (org.joda.time.Interval)17 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)17 UUID (java.util.UUID)15 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)15 ArrayList (java.util.ArrayList)14 Duration (org.killbill.billing.catalog.api.Duration)10 Entitlement (org.killbill.billing.entitlement.api.Entitlement)10 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)10 HashMap (java.util.HashMap)9