Search in sources :

Example 71 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestNextBillingDateNotifier method testInvoiceNotifier.

@Test(groups = "slow")
public void testInvoiceNotifier() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final Long accountRecordId = nonEntityDao.retrieveAccountRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, null);
    final SubscriptionBase subscription = invoiceUtil.createSubscription();
    final UUID subscriptionId = subscription.getId();
    final DateTime now = clock.getUTCNow();
    final NotificationQueue nextBillingQueue = notificationQueueService.getNotificationQueue(KILLBILL_SERVICES.INVOICE_SERVICE.getServiceName(), DefaultNextBillingDateNotifier.NEXT_BILLING_DATE_NOTIFIER_QUEUE);
    nextBillingQueue.recordFutureNotification(now, new NextBillingDateNotificationKey(null, ImmutableList.<UUID>of(subscriptionId), now, Boolean.FALSE, Boolean.FALSE), internalCallContext.getUserToken(), accountRecordId, internalCallContext.getTenantRecordId());
    // Move time in the future after the notification effectiveDate
    clock.setDeltaFromReality(3000);
    await().atMost(1, MINUTES).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return testInvoiceNotificationQListener.getEventCount() == 1;
        }
    });
    Assert.assertEquals(testInvoiceNotificationQListener.getEventCount(), 1);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) Account(org.killbill.billing.account.api.Account) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 72 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestNextBillingDatePoster method testDryRunWithSameSubscriptionLater.

@Test(groups = "slow")
public void testDryRunWithSameSubscriptionLater() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final Long accountRecordId = nonEntityDao.retrieveAccountRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, null);
    final LocalDate notificationDate1 = clock.getUTCToday().plusDays(30);
    final SubscriptionBase subscription = invoiceUtil.createSubscription();
    final UUID subscriptionId = subscription.getId();
    final FutureAccountNotifications futureAccountNotifications1 = createFutureAccountNotifications(subscriptionId, notificationDate1);
    // Add 3 seconds to make it more interesting
    clock.addDeltaFromReality(3000);
    invoiceDao.setFutureAccountNotificationsForEmptyInvoice(account.getId(), futureAccountNotifications1, internalCallContext);
    clock.addDays(1);
    final LocalDate notificationDate2 = clock.getUTCToday().plusDays(30);
    final FutureAccountNotifications futureAccountNotifications2 = createFutureAccountNotifications(subscriptionId, notificationDate2);
    invoiceDao.setFutureAccountNotificationsForEmptyInvoice(account.getId(), futureAccountNotifications2, internalCallContext);
    final NotificationQueue nextBillingQueue = notificationQueueService.getNotificationQueue(KILLBILL_SERVICES.INVOICE_SERVICE.getServiceName(), DefaultNextBillingDateNotifier.NEXT_BILLING_DATE_NOTIFIER_QUEUE);
    final Iterable<NotificationEventWithMetadata<NextBillingDateNotificationKey>> futureNotifications = nextBillingQueue.getFutureNotificationForSearchKeys(accountRecordId, internalCallContext.getTenantRecordId());
    final ImmutableList<NotificationEventWithMetadata<NextBillingDateNotificationKey>> futureNotificationsList = ImmutableList.copyOf(futureNotifications);
    // We expect only two notifications, one for each date
    Assert.assertEquals(futureNotificationsList.size(), 2);
    final NotificationEventWithMetadata<NextBillingDateNotificationKey> notification1 = futureNotificationsList.get(0);
    Assert.assertEquals(notification1.getEffectiveDate(), internalCallContext.toUTCDateTime(notificationDate1));
    final Iterable<UUID> uuidKeys1 = notification1.getEvent().getUuidKeys();
    Assert.assertFalse(Iterables.isEmpty(uuidKeys1));
    final List<UUID> uuidKeysList1 = ImmutableList.copyOf(uuidKeys1);
    Assert.assertEquals(uuidKeysList1.size(), 1);
    Assert.assertEquals(uuidKeysList1.get(0), subscriptionId);
    final NotificationEventWithMetadata<NextBillingDateNotificationKey> notification2 = futureNotificationsList.get(1);
    Assert.assertEquals(notification2.getEffectiveDate(), internalCallContext.toUTCDateTime(notificationDate2));
    final Iterable<UUID> uuidKeys2 = notification2.getEvent().getUuidKeys();
    Assert.assertFalse(Iterables.isEmpty(uuidKeys2));
    final List<UUID> uuidKeysList2 = ImmutableList.copyOf(uuidKeys2);
    Assert.assertEquals(uuidKeysList2.size(), 1);
    Assert.assertEquals(uuidKeysList2.get(0), subscriptionId);
}
Also used : Account(org.killbill.billing.account.api.Account) FutureAccountNotifications(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) LocalDate(org.joda.time.LocalDate) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 73 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestNextBillingDatePoster method testDryRunUpdateWithNewSubscription.

@Test(groups = "slow")
public void testDryRunUpdateWithNewSubscription() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final Long accountRecordId = nonEntityDao.retrieveAccountRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, null);
    final LocalDate notificationDate = clock.getUTCToday().plusDays(30);
    final SubscriptionBase subscription1 = invoiceUtil.createSubscription();
    final UUID subscriptionId1 = subscription1.getId();
    final FutureAccountNotifications futureAccountNotifications1 = createFutureAccountNotifications(subscriptionId1, notificationDate);
    invoiceDao.setFutureAccountNotificationsForEmptyInvoice(account.getId(), futureAccountNotifications1, internalCallContext);
    final SubscriptionBase subscription2 = invoiceUtil.createSubscription();
    final UUID subscriptionId2 = subscription2.getId();
    final FutureAccountNotifications futureAccountNotifications2 = createFutureAccountNotifications(subscriptionId2, notificationDate);
    invoiceDao.setFutureAccountNotificationsForEmptyInvoice(account.getId(), futureAccountNotifications2, internalCallContext);
    final NotificationQueue nextBillingQueue = notificationQueueService.getNotificationQueue(KILLBILL_SERVICES.INVOICE_SERVICE.getServiceName(), DefaultNextBillingDateNotifier.NEXT_BILLING_DATE_NOTIFIER_QUEUE);
    final Iterable<NotificationEventWithMetadata<NextBillingDateNotificationKey>> futureNotifications = nextBillingQueue.getFutureNotificationForSearchKeys(accountRecordId, internalCallContext.getTenantRecordId());
    final ImmutableList<NotificationEventWithMetadata<NextBillingDateNotificationKey>> futureNotificationsList = ImmutableList.copyOf(futureNotifications);
    Assert.assertEquals(futureNotificationsList.size(), 1);
    // We expect only one notification but this time we should see a list with both subscriptionIds.
    final NotificationEventWithMetadata<NextBillingDateNotificationKey> notification = futureNotificationsList.get(0);
    Assert.assertEquals(notification.getEffectiveDate(), internalCallContext.toUTCDateTime(notificationDate));
    final Iterable<UUID> uuidKeys = notification.getEvent().getUuidKeys();
    Assert.assertFalse(Iterables.isEmpty(uuidKeys));
    final List<UUID> uuidKeysList = ImmutableList.copyOf(uuidKeys);
    Assert.assertEquals(uuidKeysList.size(), 2);
    Assert.assertEquals(uuidKeysList.get(0), subscriptionId1);
    Assert.assertEquals(uuidKeysList.get(1), subscriptionId2);
}
Also used : Account(org.killbill.billing.account.api.Account) FutureAccountNotifications(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) LocalDate(org.joda.time.LocalDate) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 74 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class DefaultEntitlementApiBase method pause.

public void pause(final UUID bundleId, @Nullable final LocalDate localEffectiveDate, final Iterable<PluginProperty> properties, final InternalCallContext internalCallContext) throws EntitlementApiException {
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(bundleId, null, null, localEffectiveDate, localEffectiveDate, false);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.PAUSE_BUNDLE, null, null, null, null, properties, internalCallContextFactory.createCallContext(internalCallContext));
    final WithEntitlementPlugin<Void> pauseWithPlugin = new WithEntitlementPlugin<Void>() {

        @Override
        public Void doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
            try {
                final SubscriptionBase baseSubscription = subscriptionInternalApi.getBaseSubscription(bundleId, internalCallContext);
                blockUnblockBundle(bundleId, DefaultEntitlementApi.ENT_STATE_BLOCKED, KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName(), localEffectiveDate, true, true, true, baseSubscription, internalCallContext);
            } catch (SubscriptionBaseApiException e) {
                throw new EntitlementApiException(e);
            }
            return null;
        }
    };
    pluginExecution.executeWithPlugin(pauseWithPlugin, pluginContext);
}
Also used : WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) ArrayList(java.util.ArrayList) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) DefaultEntitlementApi(org.killbill.billing.entitlement.api.DefaultEntitlementApi) EntitlementApi(org.killbill.billing.entitlement.api.EntitlementApi) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) DefaultEntitlementContext(org.killbill.billing.entitlement.api.DefaultEntitlementContext) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) DefaultEntitlementContext(org.killbill.billing.entitlement.api.DefaultEntitlementContext) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 75 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class DefaultEntitlementApi method populateEventsStreamForBaseSubscriptionPerBundleCache.

private void populateEventsStreamForBaseSubscriptionPerBundleCache(final UUID bundleId, final Map<UUID, Optional<EventsStream>> eventsStreamForBaseSubscriptionPerBundle, final TenantContext callContext, final InternalCallContext contextWithValidAccountRecordId) throws EntitlementApiException {
    if (eventsStreamForBaseSubscriptionPerBundle.get(bundleId) == null) {
        final List<SubscriptionBase> subscriptionsByBundle;
        try {
            subscriptionsByBundle = subscriptionBaseInternalApi.getSubscriptionsForBundle(bundleId, null, contextWithValidAccountRecordId);
            if (subscriptionsByBundle == null || subscriptionsByBundle.isEmpty()) {
                throw new EntitlementApiException(ErrorCode.SUB_NO_ACTIVE_SUBSCRIPTIONS, bundleId);
            }
        } catch (final SubscriptionBaseApiException e) {
            throw new EntitlementApiException(e);
        }
        final boolean isStandalone = Iterables.any(subscriptionsByBundle, new Predicate<SubscriptionBase>() {

            @Override
            public boolean apply(final SubscriptionBase input) {
                return ProductCategory.STANDALONE.equals(input.getCategory());
            }
        });
        if (!isStandalone) {
            final EventsStream eventsStreamForBaseSubscription = eventsStreamBuilder.buildForBaseSubscription(bundleId, callContext);
            eventsStreamForBaseSubscriptionPerBundle.put(bundleId, Optional.<EventsStream>of(eventsStreamForBaseSubscription));
        } else {
            eventsStreamForBaseSubscriptionPerBundle.put(bundleId, Optional.<EventsStream>absent());
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) EventsStream(org.killbill.billing.entitlement.EventsStream) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Aggregations

SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)120 Test (org.testng.annotations.Test)47 UUID (java.util.UUID)46 DateTime (org.joda.time.DateTime)40 Plan (org.killbill.billing.catalog.api.Plan)37 ArrayList (java.util.ArrayList)35 LocalDate (org.joda.time.LocalDate)33 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)32 BillingEvent (org.killbill.billing.junction.BillingEvent)30 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)26 MockPlan (org.killbill.billing.catalog.MockPlan)24 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)24 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)23 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)23 BigDecimal (java.math.BigDecimal)22 LinkedList (java.util.LinkedList)21 Invoice (org.killbill.billing.invoice.api.Invoice)21 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)21 BillingEventSet (org.killbill.billing.junction.BillingEventSet)21 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)20