killbill-aplcache

Initial code review for #94

10/14/2013 3:56:28 PM

Details

diff --git a/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueListener.java b/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueListener.java
index 99be37c..8e37747 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueListener.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueListener.java
@@ -24,6 +24,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.ning.billing.ObjectType;
+import com.ning.billing.bus.api.BusEvent;
 import com.ning.billing.clock.Clock;
 import com.ning.billing.overdue.notification.OverdueAsyncBusNotificationKey;
 import com.ning.billing.overdue.notification.OverdueAsyncBusNotificationKey.OverdueAsyncBusNotificationAction;
@@ -67,18 +68,14 @@ public class OverdueListener {
     @Subscribe
     public void handle_OVERDUE_ENFORCEMENT_OFF_Insert(final ControlTagCreationInternalEvent event) {
         if (event.getTagDefinition().getName().equals(ControlTagType.OVERDUE_ENFORCEMENT_OFF.toString()) && event.getObjectType() == ObjectType.ACCOUNT) {
-            final UUID accountId = event.getObjectId();
-            final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(accountId, OverdueAsyncBusNotificationAction.CLEAR);
-            asyncPoster.insertOverdueNotification(accountId, clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()));
+            insertBusEventIntoNotificationQueue(event.getObjectId(), event, OverdueAsyncBusNotificationAction.CLEAR);
         }
     }
 
     @Subscribe
     public void handle_OVERDUE_ENFORCEMENT_OFF_Removal(final ControlTagDeletionInternalEvent event) {
         if (event.getTagDefinition().getName().equals(ControlTagType.OVERDUE_ENFORCEMENT_OFF.toString()) && event.getObjectType() == ObjectType.ACCOUNT) {
-            final UUID accountId = event.getObjectId();
-            final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(accountId, OverdueAsyncBusNotificationAction.REFRESH);
-            asyncPoster.insertOverdueNotification(accountId, clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()));
+            insertBusEventIntoNotificationQueue(event.getObjectId(), event, OverdueAsyncBusNotificationAction.REFRESH);
         }
     }
 
@@ -86,32 +83,25 @@ public class OverdueListener {
     @Subscribe
     public void handlePaymentInfoEvent(final PaymentInfoInternalEvent event) {
         log.debug("Received PaymentInfo event {}", event);
-        final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(event.getAccountId(), OverdueAsyncBusNotificationAction.REFRESH);
-        asyncPoster.insertOverdueNotification(event.getAccountId(), clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()));
+        insertBusEventIntoNotificationQueue(event.getAccountId(), event, OverdueAsyncBusNotificationAction.REFRESH);
     }
 
     @Subscribe
     public void handlePaymentErrorEvent(final PaymentErrorInternalEvent event) {
         log.debug("Received PaymentError event {}", event);
-        final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(event.getAccountId(), OverdueAsyncBusNotificationAction.REFRESH);
-        asyncPoster.insertOverdueNotification(event.getAccountId(), clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()));
+        insertBusEventIntoNotificationQueue(event.getAccountId(), event, OverdueAsyncBusNotificationAction.REFRESH);
     }
 
     @Subscribe
     public void handleInvoiceAdjustmentEvent(final InvoiceAdjustmentInternalEvent event) {
         log.debug("Received InvoiceAdjustment event {}", event);
-        final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(event.getAccountId(), OverdueAsyncBusNotificationAction.REFRESH);
-        asyncPoster.insertOverdueNotification(event.getAccountId(), clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()));
+        insertBusEventIntoNotificationQueue(event.getAccountId(), event, OverdueAsyncBusNotificationAction.REFRESH);
     }
 
-    public void handleProcessOverdueForAccount(final UUID accountId, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
-        log.info(String.format("Handle overdue notification processing for id = %s", accountId));
-        dispatcher.processOverdueForAccount(accountId, createCallContext(userToken, accountRecordId, tenantRecordId));
-    }
+    private void insertBusEventIntoNotificationQueue(final UUID accountId, final BusEvent event, final OverdueAsyncBusNotificationAction action) {
+        final OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(accountId, action);
+        asyncPoster.insertOverdueNotification(accountId, clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, createCallContext(event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()));
 
-    public void handleClearOverdueForAccount(final UUID accountId, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
-        log.info(String.format("Handle overdue notification clear for id = %s", accountId));
-        dispatcher.clearOverdueForAccount(accountId, createCallContext(userToken, accountRecordId, tenantRecordId));
     }
 
     private InternalCallContext createCallContext(final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
diff --git a/overdue/src/main/java/com/ning/billing/overdue/notification/DefaultOverdueNotifierBase.java b/overdue/src/main/java/com/ning/billing/overdue/notification/DefaultOverdueNotifierBase.java
index 71655ef..c28b37a 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/notification/DefaultOverdueNotifierBase.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/notification/DefaultOverdueNotifierBase.java
@@ -20,7 +20,9 @@ import java.util.UUID;
 
 import org.joda.time.DateTime;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import com.ning.billing.callcontext.InternalCallContext;
 import com.ning.billing.notificationq.api.NotificationEvent;
 import com.ning.billing.notificationq.api.NotificationQueue;
 import com.ning.billing.notificationq.api.NotificationQueueService;
@@ -28,30 +30,35 @@ import com.ning.billing.notificationq.api.NotificationQueueService.NoSuchNotific
 import com.ning.billing.notificationq.api.NotificationQueueService.NotificationQueueAlreadyExists;
 import com.ning.billing.notificationq.api.NotificationQueueService.NotificationQueueHandler;
 import com.ning.billing.overdue.OverdueProperties;
-import com.ning.billing.overdue.listener.OverdueListener;
+import com.ning.billing.overdue.listener.OverdueDispatcher;
 import com.ning.billing.overdue.service.DefaultOverdueService;
+import com.ning.billing.util.callcontext.CallOrigin;
+import com.ning.billing.util.callcontext.InternalCallContextFactory;
+import com.ning.billing.util.callcontext.UserType;
 
 public abstract class DefaultOverdueNotifierBase implements OverdueNotifier {
 
 
+    private static final Logger log = LoggerFactory.getLogger(DefaultOverdueNotifierBase.class);
+
+    private final InternalCallContextFactory internalCallContextFactory;
     protected final NotificationQueueService notificationQueueService;
     protected final OverdueProperties config;
-    protected final OverdueListener listener;
-
+    protected final OverdueDispatcher dispatcher;
     protected NotificationQueue overdueQueue;
 
-
-    public abstract Logger getLogger();
-
     public abstract String getQueueName();
 
     public abstract void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId);
 
-    public DefaultOverdueNotifierBase(final NotificationQueueService notificationQueueService, final OverdueProperties config,
-                                      final OverdueListener listener) {
+    public DefaultOverdueNotifierBase(final NotificationQueueService notificationQueueService,
+                                      final OverdueProperties config,
+                                      final InternalCallContextFactory internalCallContextFactory,
+                                      final OverdueDispatcher dispatcher) {
         this.notificationQueueService = notificationQueueService;
         this.config = config;
-        this.listener = listener;
+        this.dispatcher = dispatcher;
+        this.internalCallContextFactory = internalCallContextFactory;
     }
 
     @Override
@@ -87,8 +94,13 @@ public abstract class DefaultOverdueNotifierBase implements OverdueNotifier {
             try {
                 notificationQueueService.deleteNotificationQueue(overdueQueue.getServiceName(), overdueQueue.getQueueName());
             } catch (NoSuchNotificationQueue e) {
-                getLogger().error("Error deleting a queue by its own name - this should never happen", e);
+                log.error("Error deleting a queue by its own name - this should never happen", e);
             }
         }
     }
+
+    protected InternalCallContext createCallContext(final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
+        return internalCallContextFactory.createInternalCallContext(tenantRecordId, accountRecordId, "OverdueService", CallOrigin.INTERNAL, UserType.SYSTEM, userToken);
+    }
+
 }
diff --git a/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueAsyncBusNotifier.java b/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueAsyncBusNotifier.java
index e31a3cd..1135aa8 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueAsyncBusNotifier.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueAsyncBusNotifier.java
@@ -22,10 +22,15 @@ import org.joda.time.DateTime;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.ning.billing.callcontext.InternalCallContext;
 import com.ning.billing.notificationq.api.NotificationEvent;
 import com.ning.billing.notificationq.api.NotificationQueueService;
 import com.ning.billing.overdue.OverdueProperties;
+import com.ning.billing.overdue.listener.OverdueDispatcher;
 import com.ning.billing.overdue.listener.OverdueListener;
+import com.ning.billing.util.callcontext.CallOrigin;
+import com.ning.billing.util.callcontext.InternalCallContextFactory;
+import com.ning.billing.util.callcontext.UserType;
 
 import com.google.inject.Inject;
 
@@ -38,13 +43,9 @@ public class OverdueAsyncBusNotifier extends DefaultOverdueNotifierBase implemen
 
     @Inject
     public OverdueAsyncBusNotifier(final NotificationQueueService notificationQueueService, final OverdueProperties config,
-                                   final OverdueListener listener) {
-        super(notificationQueueService, config, listener);
-    }
-
-    @Override
-    public Logger getLogger() {
-        return log;
+                                   final InternalCallContextFactory internalCallContextFactory,
+                                   final OverdueDispatcher dispatcher) {
+        super(notificationQueueService, config, internalCallContextFactory, dispatcher);
     }
 
     @Override
@@ -56,17 +57,17 @@ public class OverdueAsyncBusNotifier extends DefaultOverdueNotifierBase implemen
     public void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
         try {
             if (!(notificationKey instanceof OverdueAsyncBusNotificationKey)) {
-                getLogger().error("Overdue service received Unexpected notificationKey {}", notificationKey.getClass().getName());
+                log.error("Overdue service received Unexpected notificationKey {}", notificationKey.getClass().getName());
                 return;
             }
 
             final OverdueAsyncBusNotificationKey key = (OverdueAsyncBusNotificationKey) notificationKey;
             switch (key.getAction()) {
                 case CLEAR:
-                    listener.handleClearOverdueForAccount(key.getUuidKey(), userToken, accountRecordId, tenantRecordId);
+                    dispatcher.clearOverdueForAccount(key.getUuidKey(), createCallContext(userToken, accountRecordId, tenantRecordId));
                     break;
                 case REFRESH:
-                    listener.handleProcessOverdueForAccount(key.getUuidKey(), userToken, accountRecordId, tenantRecordId);
+                    dispatcher.processOverdueForAccount(key.getUuidKey(), createCallContext(userToken, accountRecordId, tenantRecordId));
                     break;
                 default:
                     throw new RuntimeException("Unexpected action " + key.getAction() + " for account " + key.getUuidKey());
@@ -76,4 +77,5 @@ public class OverdueAsyncBusNotifier extends DefaultOverdueNotifierBase implemen
         }
     }
 
+
 }
diff --git a/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueCheckNotifier.java b/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueCheckNotifier.java
index 036a1eb..b3a78f1 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueCheckNotifier.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/notification/OverdueCheckNotifier.java
@@ -25,7 +25,9 @@ import org.slf4j.LoggerFactory;
 import com.ning.billing.notificationq.api.NotificationEvent;
 import com.ning.billing.notificationq.api.NotificationQueueService;
 import com.ning.billing.overdue.OverdueProperties;
+import com.ning.billing.overdue.listener.OverdueDispatcher;
 import com.ning.billing.overdue.listener.OverdueListener;
+import com.ning.billing.util.callcontext.InternalCallContextFactory;
 
 import com.google.inject.Inject;
 
@@ -38,13 +40,9 @@ public class OverdueCheckNotifier extends DefaultOverdueNotifierBase implements 
 
     @Inject
     public OverdueCheckNotifier(final NotificationQueueService notificationQueueService, final OverdueProperties config,
-                                final OverdueListener listener) {
-        super(notificationQueueService, config, listener);
-    }
-
-    @Override
-    public Logger getLogger() {
-        return log;
+                                final InternalCallContextFactory internalCallContextFactory,
+                                final OverdueDispatcher dispatcher) {
+        super(notificationQueueService, config, internalCallContextFactory, dispatcher);
     }
 
     @Override
@@ -56,12 +54,12 @@ public class OverdueCheckNotifier extends DefaultOverdueNotifierBase implements 
     public void handleReadyNotification(final NotificationEvent notificationKey, final DateTime eventDate, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
         try {
             if (!(notificationKey instanceof OverdueCheckNotificationKey)) {
-                getLogger().error("Overdue service received Unexpected notificationKey {}", notificationKey.getClass().getName());
+                log.error("Overdue service received Unexpected notificationKey {}", notificationKey.getClass().getName());
                 return;
             }
 
             final OverdueCheckNotificationKey key = (OverdueCheckNotificationKey) notificationKey;
-            listener.handleProcessOverdueForAccount(key.getUuidKey(), userToken, accountRecordId, tenantRecordId);
+            dispatcher.processOverdueForAccount(key.getUuidKey(), createCallContext(userToken, accountRecordId, tenantRecordId));
         } catch (IllegalArgumentException e) {
             log.error("The key returned from the NextBillingNotificationQueue is not a valid UUID", e);
         }
diff --git a/overdue/src/test/java/com/ning/billing/overdue/notification/TestOverdueCheckNotifier.java b/overdue/src/test/java/com/ning/billing/overdue/notification/TestOverdueCheckNotifier.java
index 9cd34ff..82fb6b9 100644
--- a/overdue/src/test/java/com/ning/billing/overdue/notification/TestOverdueCheckNotifier.java
+++ b/overdue/src/test/java/com/ning/billing/overdue/notification/TestOverdueCheckNotifier.java
@@ -27,8 +27,9 @@ import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.ning.billing.account.api.Account;
+import com.ning.billing.callcontext.InternalCallContext;
 import com.ning.billing.overdue.OverdueTestSuiteWithEmbeddedDB;
-import com.ning.billing.overdue.listener.OverdueListener;
+import com.ning.billing.overdue.listener.OverdueDispatcher;
 import com.ning.billing.util.callcontext.InternalCallContextFactory;
 import com.ning.billing.callcontext.InternalTenantContext;
 
@@ -37,20 +38,22 @@ import static java.util.concurrent.TimeUnit.SECONDS;
 
 public class TestOverdueCheckNotifier extends OverdueTestSuiteWithEmbeddedDB {
 
-    private OverdueListenerMock mockListener;
+    private OverdueDispatcherMock mockDispatcher;
     private OverdueNotifier notifierForMock;
 
-    private static final class OverdueListenerMock extends OverdueListener {
+
+
+    private static final class  OverdueDispatcherMock extends OverdueDispatcher {
 
         int eventCount = 0;
         UUID latestAccountId = null;
 
-        public OverdueListenerMock(final InternalCallContextFactory internalCallContextFactory) {
-            super(null, getClock(), null,internalCallContextFactory);
+        public OverdueDispatcherMock(final InternalCallContextFactory internalCallContextFactory) {
+            super(null);
         }
 
         @Override
-        public void handleProcessOverdueForAccount(final UUID accountId, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) {
+        public void processOverdueForAccount(final UUID accountId, final InternalCallContext context) {
             eventCount++;
             latestAccountId = accountId;
         }
@@ -73,8 +76,8 @@ public class TestOverdueCheckNotifier extends OverdueTestSuiteWithEmbeddedDB {
         final Account account = Mockito.mock(Account.class);
         Mockito.when(accountApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account);
 
-        mockListener = new OverdueListenerMock(internalCallContextFactory);
-        notifierForMock = new OverdueCheckNotifier(notificationQueueService, overdueProperties, mockListener);
+        mockDispatcher = new OverdueDispatcherMock(internalCallContextFactory);
+        notifierForMock = new OverdueCheckNotifier(notificationQueueService, overdueProperties, internalCallContextFactory, mockDispatcher);
 
         notifierForMock.initialize();
         notifierForMock.start();
@@ -104,11 +107,11 @@ public class TestOverdueCheckNotifier extends OverdueTestSuiteWithEmbeddedDB {
         await().atMost(5, SECONDS).until(new Callable<Boolean>() {
             @Override
             public Boolean call() throws Exception {
-                return mockListener.getEventCount() == 1;
+                return mockDispatcher.getEventCount() == 1;
             }
         });
 
-        Assert.assertEquals(mockListener.getEventCount(), 1);
-        Assert.assertEquals(mockListener.getLatestAccountId(), accountId);
+        Assert.assertEquals(mockDispatcher.getEventCount(), 1);
+        Assert.assertEquals(mockDispatcher.getLatestAccountId(), accountId);
     }
 }