Search in sources :

Example 46 with DefaultEntitlement

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

the class TestIntegrationBase method verifyTestResult.

protected void verifyTestResult(final UUID accountId, final UUID subscriptionId, final DateTime startDate, @Nullable final DateTime endDate, final BigDecimal amount, final DateTime chargeThroughDate, final int totalInvoiceItemCount) throws EntitlementApiException {
    final Entitlement entitlement = entitlementApi.getEntitlementForId(subscriptionId, callContext);
    final SubscriptionBase subscription = ((DefaultEntitlement) entitlement).getSubscriptionBase();
    final DateTime ctd = subscription.getChargedThroughDate();
    assertNotNull(ctd);
    log.info("Checking CTD: " + ctd.toString() + "; clock is " + clock.getUTCNow().toString());
    // Either the ctd is today (start of the trial) or the clock is strictly before the CTD
    assertTrue(clock.getUTCToday().compareTo(new LocalDate(ctd)) == 0 || clock.getUTCNow().isBefore(ctd));
    assertTrue(ctd.toDateTime(testTimeZone).toLocalDate().compareTo(new LocalDate(chargeThroughDate.getYear(), chargeThroughDate.getMonthOfYear(), chargeThroughDate.getDayOfMonth())) == 0);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime)

Example 47 with DefaultEntitlement

use of org.killbill.billing.entitlement.api.DefaultEntitlement 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, 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 48 with DefaultEntitlement

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

the class TestIntegration method testCreateMultipleBPWithSameExternalKey.

@Test(groups = "slow")
public void testCreateMultipleBPWithSameExternalKey() throws Exception {
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 13, 42, 0, testTimeZone);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(25));
    assertNotNull(account);
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final SubscriptionBundle initialBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey("bundleKey", callContext);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.NULL_INVOICE);
    baseEntitlement.cancelEntitlementWithPolicy(EntitlementActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final String newProductName = "Pistol";
    final DefaultEntitlement newBaseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", newProductName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final SubscriptionBundle newBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey("bundleKey", callContext);
    assertNotEquals(initialBundle.getId(), newBundle.getId());
    assertEquals(initialBundle.getAccountId(), newBundle.getAccountId());
    assertEquals(initialBundle.getExternalKey(), newBundle.getExternalKey());
    final Entitlement refreshedBseEntitlement = entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    assertEquals(refreshedBseEntitlement.getState(), EntitlementState.CANCELLED);
    assertEquals(newBaseEntitlement.getState(), EntitlementState.ACTIVE);
    checkNoMoreInvoiceToGenerate(account);
}
Also used : Account(org.killbill.billing.account.api.Account) SubscriptionBundle(org.killbill.billing.entitlement.api.SubscriptionBundle) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 49 with DefaultEntitlement

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

the class TestIntegration method testFixedTermPlan.

@Test(groups = "slow")
public void testFixedTermPlan() 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(2015, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // First invoice
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // Second invoice -> first recurring for Refurbish-Maintenance
    addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Refurbish-Maintenance", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("599.95")));
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 4, 1), new LocalDate(2015, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("199.95")));
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    expectedInvoices.clear();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 5, 1), new LocalDate(2015, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("199.95")));
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 5, 1), new LocalDate(2015, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    // 2015-5-1
    // Third invoice -> second recurring for Refurbish-Maintenance
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // Also = 1 month because or initial date 2015, 4, 1
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, expectedInvoices);
    expectedInvoices.clear();
    // Next 10 invoices -> third to twelve **and last** recurring for Refurbish-Maintenance
    LocalDate startDate = new LocalDate(2015, 6, 1);
    for (int i = 0; i < 10; i++) {
        final LocalDate endDate = startDate.plusMonths(1);
        expectedInvoices.add(new ExpectedInvoiceItemCheck(startDate, endDate, InvoiceItemType.RECURRING, new BigDecimal("199.95")));
        expectedInvoices.add(new ExpectedInvoiceItemCheck(startDate, endDate, InvoiceItemType.RECURRING, new BigDecimal("29.95")));
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.addMonths(1);
        assertListenerStatus();
        invoiceChecker.checkInvoice(account.getId(), 4 + i, callContext, expectedInvoices);
        expectedInvoices.clear();
        startDate = endDate;
    }
    // We check there is no more recurring for Refurbish-Maintenance
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2016, 4, 1), new LocalDate(2016, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 14, callContext, expectedInvoices);
    expectedInvoices.clear();
}
Also used : Account(org.killbill.billing.account.api.Account) 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) Test(org.testng.annotations.Test)

Example 50 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.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, callContext, 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.triggerInvoiceGeneration(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, callContext, 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.triggerInvoiceGeneration(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, callContext, 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)

Aggregations

DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)121 Test (org.testng.annotations.Test)112 LocalDate (org.joda.time.LocalDate)93 BigDecimal (java.math.BigDecimal)83 Account (org.killbill.billing.account.api.Account)77 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)77 DateTime (org.joda.time.DateTime)72 Invoice (org.killbill.billing.invoice.api.Invoice)66 AccountData (org.killbill.billing.account.api.AccountData)28 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)28 ArrayList (java.util.ArrayList)25 Payment (org.killbill.billing.payment.api.Payment)17 Entitlement (org.killbill.billing.entitlement.api.Entitlement)12 ExpectedPaymentCheck (org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck)11 PluginProperty (org.killbill.billing.payment.api.PluginProperty)11 UUID (java.util.UUID)10 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)9 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)8 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)7 HashMap (java.util.HashMap)6