killbill-memoizeit
Changes
invoice/src/main/java/com/ning/billing/invoice/notification/DefaultNextBillingDateNotifier.java 25(+10 -15)
Details
diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java b/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
index ce70ba7..8921b33 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
@@ -105,7 +105,7 @@ public class Engine implements EventListener, EntitlementService {
NOTIFICATION_QUEUE_NAME,
new NotificationQueueHandler() {
@Override
- public void handleReadyNotification(String notificationKey) {
+ public void handleReadyNotification(String notificationKey, DateTime eventDateTime) {
EntitlementEvent event = dao.getEventById(UUID.fromString(notificationKey));
if (event == null) {
log.warn("Failed to extract event for notification key {}", notificationKey);
diff --git a/invoice/src/main/java/com/ning/billing/invoice/InvoiceListener.java b/invoice/src/main/java/com/ning/billing/invoice/InvoiceListener.java
index ea1fc12..3026871 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/InvoiceListener.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/InvoiceListener.java
@@ -41,8 +41,6 @@ import com.ning.billing.invoice.dao.InvoiceDao;
import com.ning.billing.invoice.model.BillingEventSet;
import com.ning.billing.invoice.model.InvoiceGenerator;
import com.ning.billing.invoice.model.InvoiceItemList;
-import com.ning.billing.invoice.notification.NextBillingDateEvent;
-import com.ning.billing.util.clock.Clock;
import com.ning.billing.util.globallocker.GlobalLock;
import com.ning.billing.util.globallocker.GlobalLocker;
import com.ning.billing.util.globallocker.GlobalLocker.LockerService;
@@ -58,7 +56,6 @@ public class InvoiceListener {
private final EntitlementBillingApi entitlementBillingApi;
private final AccountUserApi accountUserApi;
private final InvoiceDao invoiceDao;
- private final Clock clock;
private final GlobalLocker locker;
private final static boolean VERBOSE_OUTPUT = false;
@@ -67,14 +64,12 @@ public class InvoiceListener {
public InvoiceListener(final InvoiceGenerator generator, final AccountUserApi accountUserApi,
final EntitlementBillingApi entitlementBillingApi,
final InvoiceDao invoiceDao,
- final GlobalLocker locker,
- final Clock clock) {
+ final GlobalLocker locker) {
this.generator = generator;
this.entitlementBillingApi = entitlementBillingApi;
this.accountUserApi = accountUserApi;
this.invoiceDao = invoiceDao;
this.locker = locker;
- this.clock = clock;
}
@Subscribe
@@ -86,11 +81,9 @@ public class InvoiceListener {
}
}
- @Subscribe
- public void handleNextBillingDateEvent(final NextBillingDateEvent event) {
- // STEPH should we use the date of the event instead?
+ public void handleNextBillingDateEvent(final UUID subscriptionId, final DateTime eventDateTime) {
try {
- processSubscription(event.getSubscriptionId(), clock.getUTCNow());
+ processSubscription(subscriptionId, eventDateTime);
} catch (InvoiceApiException e) {
log.error(e.getMessage());
}
diff --git a/invoice/src/main/java/com/ning/billing/invoice/notification/DefaultNextBillingDateNotifier.java b/invoice/src/main/java/com/ning/billing/invoice/notification/DefaultNextBillingDateNotifier.java
index 3fd03b2..642dcf9 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/notification/DefaultNextBillingDateNotifier.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/notification/DefaultNextBillingDateNotifier.java
@@ -18,8 +18,6 @@ package com.ning.billing.invoice.notification;
import java.util.UUID;
-import com.ning.billing.entitlement.api.user.Subscription;
-import com.ning.billing.entitlement.engine.dao.EntitlementDao;
import org.joda.time.DateTime;
import org.skife.jdbi.v2.sqlobject.mixins.Transmogrifier;
import org.slf4j.Logger;
@@ -27,9 +25,11 @@ import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.ning.billing.config.InvoiceConfig;
+import com.ning.billing.entitlement.api.user.Subscription;
+import com.ning.billing.entitlement.engine.dao.EntitlementDao;
+import com.ning.billing.invoice.InvoiceListener;
import com.ning.billing.invoice.api.DefaultInvoiceService;
import com.ning.billing.util.bus.Bus;
-import com.ning.billing.util.bus.Bus.EventBusException;
import com.ning.billing.util.notificationq.NotificationConfig;
import com.ning.billing.util.notificationq.NotificationKey;
import com.ning.billing.util.notificationq.NotificationQueue;
@@ -43,20 +43,20 @@ public class DefaultNextBillingDateNotifier implements NextBillingDateNotifier
private static final String NEXT_BILLING_DATE_NOTIFIER_QUEUE = "next-billing-date-queue";
- private final Bus eventBus;
private final NotificationQueueService notificationQueueService;
private final InvoiceConfig config;
private final EntitlementDao entitlementDao;
private NotificationQueue nextBillingQueue;
+ private InvoiceListener listener;
@Inject
public DefaultNextBillingDateNotifier(NotificationQueueService notificationQueueService, Bus eventBus,
- InvoiceConfig config, EntitlementDao entitlementDao){
+ InvoiceConfig config, EntitlementDao entitlementDao, InvoiceListener listener){
this.notificationQueueService = notificationQueueService;
this.config = config;
- this.eventBus = eventBus;
this.entitlementDao = entitlementDao;
+ this.listener = listener;
}
@Override
@@ -66,15 +66,14 @@ public class DefaultNextBillingDateNotifier implements NextBillingDateNotifier
NEXT_BILLING_DATE_NOTIFIER_QUEUE,
new NotificationQueueHandler() {
@Override
- public void handleReadyNotification(String notificationKey) {
- UUID subscriptionId;
+ public void handleReadyNotification(String notificationKey, DateTime eventDate) {
try {
UUID key = UUID.fromString(notificationKey);
Subscription subscription = entitlementDao.getSubscriptionFromId(key);
if (subscription == null) {
log.warn("Next Billing Date Notification Queue handled spurious notification (key: " + key + ")" );
} else {
- processEvent(key);
+ processEvent(key , eventDate);
}
} catch (IllegalArgumentException e) {
log.error("The key returned from the NextBillingNotificationQueue is not a valid UUID", e);
@@ -118,12 +117,8 @@ public class DefaultNextBillingDateNotifier implements NextBillingDateNotifier
}
}
- private void processEvent(UUID subscriptionId) {
- try {
- eventBus.post(new NextBillingDateEvent(subscriptionId));
- } catch (EventBusException e) {
- log.error("Failed to post entitlement event " + subscriptionId, e);
- }
+ private void processEvent(UUID subscriptionId, DateTime eventDateTime) {
+ listener.handleNextBillingDateEvent(subscriptionId, eventDateTime);
}
@Override
diff --git a/invoice/src/test/java/com/ning/billing/invoice/notification/TestNextBillingDateNotifier.java b/invoice/src/test/java/com/ning/billing/invoice/notification/TestNextBillingDateNotifier.java
index d2fcdee..a1ed65e 100644
--- a/invoice/src/test/java/com/ning/billing/invoice/notification/TestNextBillingDateNotifier.java
+++ b/invoice/src/test/java/com/ning/billing/invoice/notification/TestNextBillingDateNotifier.java
@@ -16,17 +16,15 @@
package com.ning.billing.invoice.notification;
+import static com.jayway.awaitility.Awaitility.await;
+import static java.util.concurrent.TimeUnit.MINUTES;
+
import java.io.IOException;
import java.sql.SQLException;
+import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
-import com.ning.billing.catalog.DefaultCatalogService;
-import com.ning.billing.catalog.api.CatalogService;
-import com.ning.billing.config.CatalogConfig;
-import com.ning.billing.entitlement.engine.dao.EntitlementDao;
-import com.ning.billing.entitlement.engine.dao.EntitlementSqlDao;
-import com.ning.billing.util.bus.InMemoryBus;
import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;
import org.skife.config.ConfigurationObjectFactory;
@@ -38,25 +36,35 @@ import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
-import com.google.common.eventbus.Subscribe;
+
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Stage;
+import com.ning.billing.catalog.DefaultCatalogService;
+import com.ning.billing.catalog.api.CatalogService;
+import com.ning.billing.config.CatalogConfig;
import com.ning.billing.config.InvoiceConfig;
import com.ning.billing.dbi.MysqlTestingHelper;
+import com.ning.billing.entitlement.api.migration.AccountMigrationData;
+import com.ning.billing.entitlement.api.user.Subscription;
+import com.ning.billing.entitlement.api.user.SubscriptionBundle;
+import com.ning.billing.entitlement.api.user.SubscriptionBundleData;
+import com.ning.billing.entitlement.api.user.SubscriptionData;
+import com.ning.billing.entitlement.engine.dao.EntitlementDao;
+import com.ning.billing.entitlement.engine.dao.EntitlementSqlDao;
+import com.ning.billing.entitlement.events.EntitlementEvent;
+import com.ning.billing.invoice.InvoiceListener;
import com.ning.billing.lifecycle.KillbillService.ServiceException;
+import com.ning.billing.util.bus.Bus;
+import com.ning.billing.util.bus.InMemoryBus;
import com.ning.billing.util.clock.Clock;
import com.ning.billing.util.clock.ClockMock;
-import com.ning.billing.util.bus.Bus;
import com.ning.billing.util.notificationq.DefaultNotificationQueueService;
import com.ning.billing.util.notificationq.DummySqlTest;
import com.ning.billing.util.notificationq.NotificationQueueService;
import com.ning.billing.util.notificationq.dao.NotificationSqlDao;
-import static com.jayway.awaitility.Awaitility.await;
-import static java.util.concurrent.TimeUnit.MINUTES;
-
public class TestNextBillingDateNotifier {
private static Logger log = LoggerFactory.getLogger(TestNextBillingDateNotifier.class);
private Clock clock;
@@ -64,7 +72,158 @@ public class TestNextBillingDateNotifier {
private DummySqlTest dao;
private Bus eventBus;
private MysqlTestingHelper helper;
+ private InvoiceListenerMock listener = new InvoiceListenerMock();
+
+ private static final class InvoiceListenerMock extends InvoiceListener {
+ int eventCount = 0;
+ UUID latestSubscriptionId = null;
+
+ public InvoiceListenerMock() {
+ super(null, null, null, null, null);
+ }
+
+
+ @Override
+ public void handleNextBillingDateEvent(UUID subscriptionId,
+ DateTime eventDateTime) {
+ eventCount++;
+ latestSubscriptionId=subscriptionId;
+ }
+
+ public int getEventCount() {
+ return eventCount;
+ }
+
+ public UUID getLatestSubscriptionId(){
+ return latestSubscriptionId;
+ }
+
+ }
+
+ private class MockEntitlementDao implements EntitlementDao {
+
+ @Override
+ public List<SubscriptionBundle> getSubscriptionBundleForAccount(
+ UUID accountId) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public SubscriptionBundle getSubscriptionBundleFromKey(String bundleKey) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public SubscriptionBundle getSubscriptionBundleFromId(UUID bundleId) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public SubscriptionBundle createSubscriptionBundle(
+ SubscriptionBundleData bundle) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public Subscription getSubscriptionFromId(UUID subscriptionId) {
+ return new BrainDeadSubscription();
+
+ }
+
+ @Override
+ public UUID getAccountIdFromSubscriptionId(UUID subscriptionId) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Subscription getBaseSubscription(UUID bundleId) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public List<Subscription> getSubscriptions(UUID bundleId) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public List<Subscription> getSubscriptionsForKey(String bundleKey) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public void updateSubscription(SubscriptionData subscription) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void createNextPhaseEvent(UUID subscriptionId,
+ EntitlementEvent nextPhase) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public EntitlementEvent getEventById(UUID eventId) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public List<EntitlementEvent> getEventsForSubscription(
+ UUID subscriptionId) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public List<EntitlementEvent> getPendingEventsForSubscription(
+ UUID subscriptionId) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public void createSubscription(SubscriptionData subscription,
+ List<EntitlementEvent> initialEvents) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void cancelSubscription(UUID subscriptionId,
+ EntitlementEvent cancelEvent) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public void uncancelSubscription(UUID subscriptionId,
+ List<EntitlementEvent> uncancelEvents) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ @Override
+ public void changePlan(UUID subscriptionId,
+ List<EntitlementEvent> changeEvents) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void migrate(UUID acountId, AccountMigrationData data) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void undoMigration(UUID accountId) {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+
@BeforeClass(groups={"setup"})
public void setup() throws ServiceException, IOException, ClassNotFoundException, SQLException {
//TestApiBase.loadSystemPropertiesFromClasspath("/entitlement.properties");
@@ -91,7 +250,7 @@ public class TestNextBillingDateNotifier {
dao = dbi.onDemand(DummySqlTest.class);
eventBus = g.getInstance(Bus.class);
helper = g.getInstance(MysqlTestingHelper.class);
- notifier = new DefaultNextBillingDateNotifier(g.getInstance(NotificationQueueService.class), eventBus, g.getInstance(InvoiceConfig.class), g.getInstance(EntitlementDao.class));
+ notifier = new DefaultNextBillingDateNotifier(g.getInstance(NotificationQueueService.class), eventBus, g.getInstance(InvoiceConfig.class), new MockEntitlementDao(), listener);
startMysql();
}
@@ -105,38 +264,17 @@ public class TestNextBillingDateNotifier {
helper.initDb(entitlementDdl);
}
- public static class NextBillingEventListener {
- private int eventCount=0;
- private NextBillingDateEvent event;
-
- public int getEventCount() {
- return eventCount;
- }
-
- @Subscribe
- public synchronized void processEvent(NextBillingDateEvent event) {
- eventCount++;
- this.event = event;
- //log.debug("Got event {} {}", event.name, event.value);
- }
-
- public NextBillingDateEvent getLatestEvent() {
- return event;
- }
- }
- @Test(enabled=false, groups="slow")
+ @Test(enabled=true, groups="slow")
public void test() throws Exception {
- final UUID subscriptionId = new UUID(0L,1000L);
+ final UUID subscriptionId = new UUID(0L,1L);
final DateTime now = new DateTime();
final DateTime readyTime = now.plusMillis(2000);
- final NextBillingEventListener listener = new NextBillingEventListener();
eventBus.start();
notifier.initialize();
notifier.start();
- eventBus.register(listener);
dao.inTransaction(new Transaction<Void, DummySqlTest>() {
@Override
public Void inTransaction(DummySqlTest transactional,
@@ -159,6 +297,6 @@ public class TestNextBillingDateNotifier {
});
Assert.assertEquals(listener.getEventCount(), 1);
- Assert.assertEquals(listener.getLatestEvent().getSubscriptionId(), subscriptionId);
+ Assert.assertEquals(listener.getLatestSubscriptionId(), subscriptionId);
}
}
diff --git a/util/src/main/java/com/ning/billing/util/notificationq/DefaultNotificationQueue.java b/util/src/main/java/com/ning/billing/util/notificationq/DefaultNotificationQueue.java
index 3c19a2b..9b7b012 100644
--- a/util/src/main/java/com/ning/billing/util/notificationq/DefaultNotificationQueue.java
+++ b/util/src/main/java/com/ning/billing/util/notificationq/DefaultNotificationQueue.java
@@ -53,7 +53,7 @@ public class DefaultNotificationQueue extends NotificationQueueBase {
nbProcessedEvents.incrementAndGet();
logDebug("handling notification %s, key = %s for time %s",
cur.getUUID(), cur.getNotificationKey(), cur.getEffectiveDate());
- handler.handleReadyNotification(cur.getNotificationKey());
+ handler.handleReadyNotification(cur.getNotificationKey(), cur.getEffectiveDate());
clearNotification(cur);
logDebug("done handling notification %s, key = %s for time %s",
cur.getUUID(), cur.getNotificationKey(), cur.getEffectiveDate());
diff --git a/util/src/main/java/com/ning/billing/util/notificationq/NotificationQueueService.java b/util/src/main/java/com/ning/billing/util/notificationq/NotificationQueueService.java
index 4fb17b5..979e494 100644
--- a/util/src/main/java/com/ning/billing/util/notificationq/NotificationQueueService.java
+++ b/util/src/main/java/com/ning/billing/util/notificationq/NotificationQueueService.java
@@ -16,6 +16,8 @@
package com.ning.billing.util.notificationq;
+import org.joda.time.DateTime;
+
public interface NotificationQueueService {
@@ -25,7 +27,7 @@ public interface NotificationQueueService {
*
* @param notificationKey the notification key associated to that notification entry
*/
- public void handleReadyNotification(String notificationKey);
+ public void handleReadyNotification(String notificationKey, DateTime eventDateTime);
}
public static final class NotificationQueueAlreadyExists extends Exception {
diff --git a/util/src/test/java/com/ning/billing/util/notificationq/MockNotificationQueue.java b/util/src/test/java/com/ning/billing/util/notificationq/MockNotificationQueue.java
index e1da366..4bfe09e 100644
--- a/util/src/test/java/com/ning/billing/util/notificationq/MockNotificationQueue.java
+++ b/util/src/test/java/com/ning/billing/util/notificationq/MockNotificationQueue.java
@@ -74,7 +74,7 @@ public class MockNotificationQueue extends NotificationQueueBase implements Noti
}
}
for (Notification cur : readyNotifications) {
- handler.handleReadyNotification(cur.getNotificationKey());
+ handler.handleReadyNotification(cur.getNotificationKey(), cur.getEffectiveDate());
DefaultNotification processedNotification = new DefaultNotification(-1L, cur.getUUID(), hostname, "MockQueue", clock.getUTCNow().plus(config.getDaoClaimTimeMs()), NotificationLifecycleState.PROCESSED, cur.getNotificationKey(), cur.getEffectiveDate());
oldNotifications.add(cur);
processedNotifications.add(processedNotification);
diff --git a/util/src/test/java/com/ning/billing/util/notificationq/TestNotificationQueue.java b/util/src/test/java/com/ning/billing/util/notificationq/TestNotificationQueue.java
index eac2d78..fbcf8f9 100644
--- a/util/src/test/java/com/ning/billing/util/notificationq/TestNotificationQueue.java
+++ b/util/src/test/java/com/ning/billing/util/notificationq/TestNotificationQueue.java
@@ -48,7 +48,6 @@ import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
-import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.ning.billing.dbi.MysqlTestingHelper;
import com.ning.billing.util.clock.Clock;
@@ -119,7 +118,7 @@ public class TestNotificationQueue {
final DefaultNotificationQueue queue = new DefaultNotificationQueue(dbi, clock, "test-svc", "foo",
new NotificationQueueHandler() {
@Override
- public void handleReadyNotification(String notificationKey) {
+ public void handleReadyNotification(String notificationKey, DateTime eventDateTime) {
synchronized (expectedNotifications) {
log.info("Handler received key: " + notificationKey);
@@ -182,7 +181,7 @@ public class TestNotificationQueue {
final DefaultNotificationQueue queue = new DefaultNotificationQueue(dbi, clock, "test-svc", "many",
new NotificationQueueHandler() {
@Override
- public void handleReadyNotification(String notificationKey) {
+ public void handleReadyNotification(String notificationKey, DateTime eventDateTime) {
synchronized (expectedNotifications) {
expectedNotifications.put(notificationKey, Boolean.TRUE);
expectedNotifications.notify();
@@ -292,7 +291,7 @@ public class TestNotificationQueue {
final NotificationQueue queueFred = notificationQueueService.createNotificationQueue("UtilTest", "Fred", new NotificationQueueHandler() {
@Override
- public void handleReadyNotification(String notificationKey) {
+ public void handleReadyNotification(String notificationKey, DateTime eventDateTime) {
log.info("Fred received key: " + notificationKey);
expectedNotificationsFred.put(notificationKey, Boolean.TRUE);
eventsReceived++;
@@ -302,7 +301,7 @@ public class TestNotificationQueue {
final NotificationQueue queueBarney = notificationQueueService.createNotificationQueue("UtilTest", "Barney", new NotificationQueueHandler() {
@Override
- public void handleReadyNotification(String notificationKey) {
+ public void handleReadyNotification(String notificationKey, DateTime eventDateTime) {
log.info("Barney received key: " + notificationKey);
expectedNotificationsBarney.put(notificationKey, Boolean.TRUE);
eventsReceived++;