Search in sources :

Example 71 with DefaultEntitlement

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

the class TestConsumableInArrear method testWithNoUsageInPeriodAndOldUsage.

@Test(groups = "slow")
public void testWithNoUsageInPeriodAndOldUsage() 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 SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    //
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.ANNUAL, 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, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    //
    // ADD ADD_ON ON THE SAME DAY
    //
    final DefaultEntitlement aoSubscription = addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Bullets", ProductCategory.ADD_ON, BillingPeriod.NO_BILLING_PERIOD, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 4, 1), 99L, callContext);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 4, 15), 100L, callContext);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2013, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("2399.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.USAGE, new BigDecimal("5.90")));
    // We don't expect any invoice
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE);
    clock.addMonths(1);
    assertListenerStatus();
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 6, 1), 50L, callContext);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 6, 16), 300L, callContext);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 6, 1), new LocalDate(2012, 7, 1), InvoiceItemType.USAGE, new BigDecimal("11.80")));
    // Should be ignored because this is outside of optimization range (org.killbill.invoice.readMaxRawUsagePreviousPeriod = 2) => we will only look for items > 2012-7-1 - 2 months = 2012-5-1
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 4, 30), 100L, callContext);
    // Should be invoiced from past period
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 5, 1), 199L, callContext);
    // New usage for this past period
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 7, 1), 50L, callContext);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 7, 16), 300L, callContext);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 4, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.USAGE, new BigDecimal("5.90")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 7, 1), new LocalDate(2012, 8, 1), InvoiceItemType.USAGE, new BigDecimal("11.80")));
}
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) Test(org.testng.annotations.Test)

Example 72 with DefaultEntitlement

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

the class TestConsumableInArrear method testWithCancellation.

@Test(groups = "slow")
public void testWithCancellation() 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 SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    //
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.ANNUAL, 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, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    //
    // ADD ADD_ON ON THE SAME DAY
    //
    final DefaultEntitlement aoSubscription = addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Bullets", ProductCategory.ADD_ON, BillingPeriod.NO_BILLING_PERIOD, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 4, 1), 99L, callContext);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 4, 15), 100L, callContext);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2013, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("2399.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.USAGE, new BigDecimal("5.90")));
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 5, 3), 99L, callContext);
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 5, 5), 100L, callContext);
    // This one should be ignored
    setUsage(aoSubscription.getId(), "bullets", new LocalDate(2012, 5, 29), 100L, callContext);
    clock.addDays(27);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    aoSubscription.cancelEntitlementWithDateOverrideBillingPolicy(new LocalDate(2012, 5, 28), BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 5, 28), InvoiceItemType.USAGE, new BigDecimal("5.90")));
    busHandler.pushExpectedEvent(NextEvent.NULL_INVOICE);
    clock.addDays(4);
    assertListenerStatus();
}
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) Test(org.testng.annotations.Test)

Example 73 with DefaultEntitlement

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

the class InvoiceChecker method checkChargedThroughDate.

public void checkChargedThroughDate(final UUID entitlementId, final LocalDate expectedLocalCTD, final CallContext context) {
    try {
        final DefaultEntitlement entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(entitlementId, context);
        final SubscriptionBase subscription = entitlement.getSubscriptionBase();
        if (expectedLocalCTD == null) {
            assertNull(subscription.getChargedThroughDate());
        } else {
            final String msg = String.format("Checking CTD for entitlement %s : expectedLocalCTD = %s, got %s", entitlementId, expectedLocalCTD, subscription.getChargedThroughDate().toLocalDate());
            assertTrue(expectedLocalCTD.compareTo(subscription.getChargedThroughDate().toLocalDate()) == 0, msg);
        }
    } catch (final EntitlementApiException e) {
        fail("Failed to retrieve entitlement for " + entitlementId);
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException)

Example 74 with DefaultEntitlement

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

the class DefaultEntitlementService method processEntitlementNotification.

private void processEntitlementNotification(final EntitlementNotificationKey key, final InternalCallContext internalCallContext, final CallContext callContext) {
    final Entitlement entitlement;
    try {
        entitlement = entitlementInternalApi.getEntitlementForId(key.getEntitlementId(), internalCallContext);
    } catch (final EntitlementApiException e) {
        log.error("Error retrieving entitlementId='{}'", key.getEntitlementId(), e);
        return;
    }
    if (!(entitlement instanceof DefaultEntitlement)) {
        log.error("Error retrieving entitlementId='{}', unexpected entitlement className='{}'", key.getEntitlementId(), entitlement.getClass().getName());
        return;
    }
    final EntitlementNotificationKeyAction entitlementNotificationKeyAction = key.getEntitlementNotificationKeyAction();
    try {
        if (EntitlementNotificationKeyAction.CHANGE.equals(entitlementNotificationKeyAction) || EntitlementNotificationKeyAction.CANCEL.equals(entitlementNotificationKeyAction)) {
            blockAddOnsIfRequired(key, (DefaultEntitlement) entitlement, callContext, internalCallContext);
        } else if (EntitlementNotificationKeyAction.PAUSE.equals(entitlementNotificationKeyAction)) {
            entitlementInternalApi.pause(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
        } else if (EntitlementNotificationKeyAction.RESUME.equals(entitlementNotificationKeyAction)) {
            entitlementInternalApi.resume(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
        }
    } catch (final EntitlementApiException e) {
        log.error("Error processing event for entitlementId='{}'", entitlement.getId(), e);
    }
}
Also used : EntitlementNotificationKeyAction(org.killbill.billing.entitlement.engine.core.EntitlementNotificationKeyAction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement)

Example 75 with DefaultEntitlement

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

the class DefaultEntitlementApiBase method getAllEntitlementsForAccount.

public AccountEntitlements getAllEntitlementsForAccount(final InternalTenantContext tenantContext) throws EntitlementApiException {
    final AccountEventsStreams accountEventsStreams = eventsStreamBuilder.buildForAccount(tenantContext);
    final Map<UUID, Collection<Entitlement>> entitlementsPerBundle = new HashMap<UUID, Collection<Entitlement>>();
    for (final UUID bundleId : accountEventsStreams.getEventsStreams().keySet()) {
        if (entitlementsPerBundle.get(bundleId) == null) {
            entitlementsPerBundle.put(bundleId, new LinkedList<Entitlement>());
        }
        for (final EventsStream eventsStream : accountEventsStreams.getEventsStreams().get(bundleId)) {
            final Entitlement entitlement = new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, tenantContext, internalCallContextFactory);
            entitlementsPerBundle.get(bundleId).add(entitlement);
        }
    }
    return new DefaultAccountEntitlements(accountEventsStreams, entitlementsPerBundle);
}
Also used : HashMap(java.util.HashMap) EventsStream(org.killbill.billing.entitlement.EventsStream) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Collection(java.util.Collection) AccountEventsStreams(org.killbill.billing.entitlement.AccountEventsStreams) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) UUID(java.util.UUID)

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