killbill-memoizeit

Details

diff --git a/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java b/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java
index 24b23a7..8d57212 100644
--- a/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java
+++ b/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java
@@ -17,9 +17,11 @@
 package com.ning.billing.beatrix;
 
 import javax.inject.Inject;
+import javax.inject.Named;
 
 import com.ning.billing.beatrix.bus.api.BeatrixService;
 import com.ning.billing.beatrix.extbus.BeatrixListener;
+import com.ning.billing.beatrix.glue.BeatrixModule;
 import com.ning.billing.bus.api.PersistentBus;
 import com.ning.billing.lifecycle.LifecycleHandlerType;
 import com.ning.billing.lifecycle.LifecycleHandlerType.LifecycleLevel;
@@ -34,7 +36,7 @@ public class DefaultBeatrixService implements BeatrixService {
     private final PersistentBus externalBus;
 
     @Inject
-    public DefaultBeatrixService(final PersistentBus eventBus, final PersistentBus externalBus, final BeatrixListener beatrixListener) {
+    public DefaultBeatrixService(final PersistentBus eventBus,@Named(BeatrixModule.EXTERNAL_BUS) final PersistentBus externalBus, final BeatrixListener beatrixListener) {
         this.eventBus = eventBus;
         this.externalBus = externalBus;
         this.beatrixListener = beatrixListener;
diff --git a/beatrix/src/main/java/com/ning/billing/beatrix/extbus/BeatrixListener.java b/beatrix/src/main/java/com/ning/billing/beatrix/extbus/BeatrixListener.java
index 2e0284c..619922d 100644
--- a/beatrix/src/main/java/com/ning/billing/beatrix/extbus/BeatrixListener.java
+++ b/beatrix/src/main/java/com/ning/billing/beatrix/extbus/BeatrixListener.java
@@ -19,6 +19,7 @@ package com.ning.billing.beatrix.extbus;
 import java.util.UUID;
 
 import javax.inject.Inject;
+import javax.inject.Named;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -26,13 +27,16 @@ import org.slf4j.LoggerFactory;
 import com.ning.billing.ObjectType;
 import com.ning.billing.account.api.Account;
 import com.ning.billing.account.api.AccountApiException;
+import com.ning.billing.beatrix.glue.BeatrixModule;
 import com.ning.billing.bus.api.BusEvent;
 import com.ning.billing.bus.api.PersistentBus;
 import com.ning.billing.bus.api.PersistentBus.EventBusException;
 import com.ning.billing.entitlement.api.SubscriptionTransitionType;
 import com.ning.billing.notification.plugin.api.ExtBusEventType;
+import com.ning.billing.util.callcontext.CallOrigin;
 import com.ning.billing.util.callcontext.InternalCallContext;
 import com.ning.billing.util.callcontext.InternalCallContextFactory;
+import com.ning.billing.util.callcontext.UserType;
 import com.ning.billing.util.events.AccountChangeInternalEvent;
 import com.ning.billing.util.events.AccountCreationInternalEvent;
 import com.ning.billing.util.events.BusInternalEvent;
@@ -68,7 +72,7 @@ public class BeatrixListener {
     protected final ObjectMapper objectMapper;
 
     @Inject
-    public BeatrixListener(final PersistentBus externalBus,
+    public BeatrixListener(@Named(BeatrixModule.EXTERNAL_BUS) final PersistentBus externalBus,
                            final InternalCallContextFactory internalCallContextFactory,
                            final AccountInternalApi accountApi) {
         this.externalBus = externalBus;
@@ -82,7 +86,7 @@ public class BeatrixListener {
     @Subscribe
     public void handleAllInternalKillbillEvents(final BusInternalEvent event) {
 
-        final InternalCallContext internalContext = null; // STEPH_BUS internalCallContextFactory.createInternalCallContext(eventWithMetadata.getSearchKey2(), eventWithMetadata.getSearchKey1(), "BeatrixListener", CallOrigin.INTERNAL, UserType.SYSTEM, eventWithMetadata.getUserToken());
+        final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(event.getSearchKey2(), event.getSearchKey1(), "BeatrixListener", CallOrigin.INTERNAL, UserType.SYSTEM, event.getUserToken());
         try {
             final BusEvent externalEvent = computeExtBusEventEntryFromBusInternalEvent(event, internalContext);
             if (externalEvent != null) {
@@ -216,17 +220,12 @@ public class BeatrixListener {
 
             default:
         }
-
         final UUID accountId = getAccountIdFromRecordId(event.getBusEventType(), objectId, context.getAccountRecordId(), context);
         final UUID tenantId = context.toTenantContext().getTenantId();
 
-        return null;
-        // STEPH_BUS
-        /*
         return eventBusType != null ?
-               new DefaultBusExternalEvent(objectId, objectType, eventBusType, accountId, tenantId) :
+               new DefaultBusExternalEvent(objectId, objectType, eventBusType, accountId, tenantId, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) :
                null;
-               */
     }
 
     private final UUID getAccountIdFromRecordId(final BusInternalEventType eventType, final UUID objectId, final Long recordId, final InternalCallContext context) {
diff --git a/beatrix/src/main/java/com/ning/billing/beatrix/extbus/DefaultBusExternalEvent.java b/beatrix/src/main/java/com/ning/billing/beatrix/extbus/DefaultBusExternalEvent.java
index 0c1b74b..a7c36cb 100644
--- a/beatrix/src/main/java/com/ning/billing/beatrix/extbus/DefaultBusExternalEvent.java
+++ b/beatrix/src/main/java/com/ning/billing/beatrix/extbus/DefaultBusExternalEvent.java
@@ -8,29 +8,38 @@ import com.ning.billing.notification.plugin.api.ExtBusEvent;
 import com.ning.billing.notification.plugin.api.ExtBusEventType;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
 
-public class DefaultBusExternalEvent implements ExtBusEvent {
+public class DefaultBusExternalEvent implements ExtBusEvent, BusEvent {
 
     private final UUID objectId;
     private final UUID accountId;
     private final UUID tenantId;
     private final ObjectType objectType;
     private final ExtBusEventType eventType;
-
+    private final Long searchKey1;
+    private final Long searchKey2;
+    private final UUID userToken;
 
     @JsonCreator
     public DefaultBusExternalEvent(@JsonProperty("objectId") final UUID objectId,
                                    @JsonProperty("objectType") final ObjectType objectType,
                                    @JsonProperty("eventType") final ExtBusEventType eventType,
                                    @JsonProperty("accountId") final UUID accountId,
-                                   @JsonProperty("tenantId") final UUID tenantId) {
+                                   @JsonProperty("tenantId") final UUID tenantId,
+                                   @JsonProperty("searchKey1") final Long searchKey1,
+                                   @JsonProperty("searchKey2") final Long searchKey2,
+                                   @JsonProperty("userToken") final UUID userToken) {
         this.eventType = eventType;
         this.objectType = objectType;
         this.objectId = objectId;
         this.accountId = accountId;
         this.tenantId = tenantId;
+        this.searchKey1 = searchKey1;
+        this.searchKey2 = searchKey2;
+        this.userToken = userToken;
     }
 
     @Override
@@ -58,6 +67,24 @@ public class DefaultBusExternalEvent implements ExtBusEvent {
         return objectType;
     }
 
+    @JsonIgnore
+    @Override
+    public Long getSearchKey1() {
+        return searchKey1;
+    }
+
+    @JsonIgnore
+    @Override
+    public Long getSearchKey2() {
+        return searchKey2;
+    }
+
+    @JsonIgnore
+    @Override
+    public UUID getUserToken() {
+        return userToken;
+    }
+
 
     @Override
     public boolean equals(final Object o) {
@@ -98,4 +125,16 @@ public class DefaultBusExternalEvent implements ExtBusEvent {
         result = 31 * result + eventType.hashCode();
         return result;
     }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder("DefaultBusExternalEvent{");
+        sb.append("objectId=").append(objectId);
+        sb.append(", accountId=").append(accountId);
+        sb.append(", tenantId=").append(tenantId);
+        sb.append(", objectType=").append(objectType);
+        sb.append(", eventType=").append(eventType);
+        sb.append('}');
+        return sb.toString();
+    }
 }
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/extbus/TestEventJson.java b/beatrix/src/test/java/com/ning/billing/beatrix/extbus/TestEventJson.java
index e238290..3f24137 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/extbus/TestEventJson.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/extbus/TestEventJson.java
@@ -24,7 +24,7 @@ public class TestEventJson extends BeatrixTestSuite {
         final ObjectType objectType = ObjectType.ACCOUNT;
         final ExtBusEventType extBusEventType = ExtBusEventType.ACCOUNT_CREATION;
 
-        final DefaultBusExternalEvent e = new DefaultBusExternalEvent(objectId, objectType, extBusEventType, accountId, tenantId);
+        final DefaultBusExternalEvent e = new DefaultBusExternalEvent(objectId, objectType, extBusEventType, accountId, tenantId, 1L, 2L, UUID.randomUUID());
         final String json = mapper.writeValueAsString(e);
 
         final Class<?> claz = Class.forName(DefaultBusExternalEvent.class.getName());
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
index d46ba45..e871545 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
@@ -42,6 +42,7 @@ import com.ning.billing.api.TestApiListener;
 import com.ning.billing.api.TestApiListener.NextEvent;
 import com.ning.billing.api.TestListenerStatus;
 import com.ning.billing.beatrix.BeatrixTestSuiteWithEmbeddedDB;
+import com.ning.billing.beatrix.glue.BeatrixModule;
 import com.ning.billing.beatrix.lifecycle.Lifecycle;
 import com.ning.billing.beatrix.osgi.SetupBundleWithAssertion;
 import com.ning.billing.beatrix.util.AccountChecker;
@@ -180,7 +181,7 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
     protected AccountChecker accountChecker;
 
     @Inject
-    protected PersistentBus externalBus;
+    protected @javax.inject.Named(BeatrixModule.EXTERNAL_BUS)PersistentBus externalBus;
 
     @Inject
     protected RefundChecker refundChecker;
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java
index b4e1844..be6ff80 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java
@@ -17,6 +17,8 @@
 package com.ning.billing.beatrix.integration;
 
 import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.joda.time.DateTime;
 import org.testng.annotations.BeforeMethod;
@@ -34,6 +36,8 @@ import com.ning.billing.notification.plugin.api.ExtBusEvent;
 
 import com.google.common.eventbus.Subscribe;
 
+import static com.jayway.awaitility.Awaitility.await;
+import static java.util.concurrent.TimeUnit.SECONDS;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
@@ -42,11 +46,15 @@ public class TestPublicBus extends TestIntegrationBase {
     private PublicListener publicListener;
 
 
+    private AtomicInteger externalBusCount;
+
     public class PublicListener {
 
         @Subscribe
         public void handleExternalEvents(final ExtBusEvent event) {
-            log.info("GOT EXT EVENT " + event.toString());
+            log.info("GOT EXT EVENT " + event);
+            externalBusCount.incrementAndGet();
+
         }
     }
 
@@ -69,6 +77,8 @@ public class TestPublicBus extends TestIntegrationBase {
         busService.getBus().register(busHandler);
         externalBus.register(publicListener);
         lifecycle.fireStartupSequencePostEventRegistration();
+
+        this.externalBusCount = new AtomicInteger(0);
     }
 
 
@@ -102,6 +112,16 @@ public class TestPublicBus extends TestIntegrationBase {
 
         assertNotNull(subscription);
         assertTrue(busHandler.isCompleted(DELAY));
+
+        await().atMost(10, SECONDS).until(new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                // expecting ACCOUNT_CREATION, ACCOUNT_CHANGE, SUBSCRIPTION_CREATION, INVOICE_CREATION
+                return externalBusCount.get() == 4;
+            }
+        });
+
+
     }
 
 
diff --git a/osgi/src/main/java/com/ning/billing/osgi/KillbillEventObservable.java b/osgi/src/main/java/com/ning/billing/osgi/KillbillEventObservable.java
index 531a8bc..31f94c2 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/KillbillEventObservable.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/KillbillEventObservable.java
@@ -19,6 +19,7 @@ package com.ning.billing.osgi;
 import java.util.Observable;
 
 import javax.inject.Inject;
+import javax.inject.Named;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,7 +38,7 @@ public class KillbillEventObservable extends Observable {
     private final PersistentBus externalBus;
 
     @Inject
-    public KillbillEventObservable(final PersistentBus externalBus) {
+    public KillbillEventObservable(@Named("externalBus") final PersistentBus externalBus) {
         this.externalBus = externalBus;
     }
 
diff --git a/server/src/main/java/com/ning/billing/server/DefaultServerService.java b/server/src/main/java/com/ning/billing/server/DefaultServerService.java
index 9a12a17..a28ca50 100644
--- a/server/src/main/java/com/ning/billing/server/DefaultServerService.java
+++ b/server/src/main/java/com/ning/billing/server/DefaultServerService.java
@@ -17,10 +17,12 @@
 package com.ning.billing.server;
 
 import javax.inject.Inject;
+import javax.inject.Named;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.ning.billing.beatrix.glue.BeatrixModule;
 import com.ning.billing.bus.api.PersistentBus;
 import com.ning.billing.bus.api.PersistentBus.EventBusException;
 import com.ning.billing.lifecycle.LifecycleHandlerType;
@@ -38,7 +40,7 @@ public class DefaultServerService implements ServerService {
     private final PushNotificationListener pushNotificationListener;
 
     @Inject
-    public DefaultServerService(final PersistentBus bus, final PushNotificationListener pushNotificationListener) {
+    public DefaultServerService(@Named(BeatrixModule.EXTERNAL_BUS) final PersistentBus bus, final PushNotificationListener pushNotificationListener) {
         this.bus = bus;
         this.pushNotificationListener = pushNotificationListener;
     }
diff --git a/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java b/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
index 7da4500..6195436 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
@@ -336,7 +336,7 @@ public abstract class KillbillClient extends GuicyKillbillTestSuiteWithEmbeddedD
         String baseJson = mapper.writeValueAsString(input);
 
         final Map<String, String> queryParams = waitCompletion ? getQueryParamsForCallCompletion("5") : DEFAULT_EMPTY_QUERY;
-        Response response = doPost(JaxrsResource.SUBSCRIPTIONS_PATH, baseJson, queryParams, DEFAULT_HTTP_TIMEOUT_SEC);
+        Response response = doPost(JaxrsResource.SUBSCRIPTIONS_PATH, baseJson, queryParams, DEFAULT_HTTP_TIMEOUT_SEC * 1000);
         Assert.assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode());
 
         final String location = response.getHeader("Location");
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java b/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java
index 5ec6ec7..dc5186f 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java
@@ -30,6 +30,7 @@ import org.eclipse.jetty.servlet.FilterHolder;
 import org.joda.time.LocalDate;
 import org.skife.config.ConfigSource;
 import org.skife.config.ConfigurationObjectFactory;
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.AfterSuite;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.BeforeMethod;
@@ -40,6 +41,7 @@ import com.ning.billing.KillbillConfigSource;
 import com.ning.billing.account.glue.DefaultAccountModule;
 import com.ning.billing.api.TestApiListener;
 import com.ning.billing.beatrix.glue.BeatrixModule;
+import com.ning.billing.bus.api.PersistentBus;
 import com.ning.billing.catalog.glue.CatalogModule;
 import com.ning.billing.dbi.DBTestingHelper;
 import com.ning.billing.dbi.MysqlTestingHelper;
@@ -97,6 +99,12 @@ public class TestJaxrsBase extends KillbillClient {
     @Inject
     protected CacheControllerDispatcher cacheControllerDispatcher;
 
+    @Inject
+    protected @javax.inject.Named(BeatrixModule.EXTERNAL_BUS)PersistentBus externalBus;
+
+    @Inject
+    protected PersistentBus internalBus;
+
     protected static TestKillbillGuiceListener listener;
 
     private HttpServer server;
@@ -217,12 +225,20 @@ public class TestJaxrsBase extends KillbillClient {
     @BeforeMethod(groups = "slow")
     public void beforeMethod() throws Exception {
         super.beforeMethod();
+        externalBus.start();
+        internalBus.start();
         cacheControllerDispatcher.clearAll();
         busHandler.reset();
         clock.resetDeltaFromReality();
         clock.setDay(new LocalDate(2012, 8, 25));
     }
 
+    @AfterMethod(groups = "slow")
+    public void afterMethod() throws Exception {
+        externalBus.stop();
+        internalBus.stop();
+    }
+
     @BeforeClass(groups = "slow")
     public void beforeClass() throws Exception {
         loadConfig();
diff --git a/util/src/main/java/com/ning/billing/util/userrequest/CompletionUserRequestBase.java b/util/src/main/java/com/ning/billing/util/userrequest/CompletionUserRequestBase.java
index 890bdab..9ba614f 100644
--- a/util/src/main/java/com/ning/billing/util/userrequest/CompletionUserRequestBase.java
+++ b/util/src/main/java/com/ning/billing/util/userrequest/CompletionUserRequestBase.java
@@ -105,7 +105,11 @@ public class CompletionUserRequestBase implements CompletionUserRequest {
                 onAccountChange((AccountChangeInternalEvent) curEvent);
                 break;
             case SUBSCRIPTION_TRANSITION:
-                onSubscriptionTransition((EffectiveSubscriptionInternalEvent) curEvent);
+                // We only dispatch the event for the effective date and not the requested date since we have both
+                // for entitlement events.
+                if (curEvent instanceof EffectiveSubscriptionInternalEvent) {
+                    onSubscriptionTransition((EffectiveSubscriptionInternalEvent) curEvent);
+                }
                 break;
             case INVOICE_EMPTY:
                 onEmptyInvoice((NullInvoiceInternalEvent) curEvent);