killbill-memoizeit

Merge branch 'integration' of github.com:ning/killbill

7/3/2012 8:26:14 PM

Details

diff --git a/api/src/main/java/com/ning/billing/overdue/OverdueChangeEvent.java b/api/src/main/java/com/ning/billing/overdue/OverdueChangeEvent.java
new file mode 100644
index 0000000..5b6aa57
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/overdue/OverdueChangeEvent.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.overdue;
+
+import java.util.UUID;
+
+import com.ning.billing.junction.api.Blockable;
+import com.ning.billing.util.bus.BusEvent;
+
+public interface OverdueChangeEvent extends BusEvent {
+
+    UUID getOverdueObjectId();
+    
+    Blockable.Type getOverdueObjectType();
+    
+    String getPreviousOverdueStateName();
+    
+    String getNextOverdueStateName();
+}
diff --git a/api/src/main/java/com/ning/billing/util/bus/BusEvent.java b/api/src/main/java/com/ning/billing/util/bus/BusEvent.java
index cfb8b0b..919924e 100644
--- a/api/src/main/java/com/ning/billing/util/bus/BusEvent.java
+++ b/api/src/main/java/com/ning/billing/util/bus/BusEvent.java
@@ -35,7 +35,8 @@ public interface BusEvent {
         CONTROL_TAGDEFINITION_CREATION,
         CONTROL_TAGDEFINITION_DELETION,
         USER_TAGDEFINITION_CREATION,
-        USER_TAGDEFINITION_DELETION
+        USER_TAGDEFINITION_DELETION,
+        OVERDUE_CHANGE
     }
 
     public BusEventType getBusEventType();
diff --git a/overdue/src/main/java/com/ning/billing/overdue/applicator/DefaultOverdueChangeEvent.java b/overdue/src/main/java/com/ning/billing/overdue/applicator/DefaultOverdueChangeEvent.java
new file mode 100644
index 0000000..2f7b7ce
--- /dev/null
+++ b/overdue/src/main/java/com/ning/billing/overdue/applicator/DefaultOverdueChangeEvent.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.overdue.applicator;
+
+import java.util.UUID;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.ning.billing.junction.api.Blockable;
+import com.ning.billing.junction.api.Blockable.Type;
+import com.ning.billing.overdue.OverdueChangeEvent;
+
+public class DefaultOverdueChangeEvent implements OverdueChangeEvent {
+    private final UUID overdueObjectId;
+    private final Blockable.Type overdueObjectType;
+    private final String previousOverdueStateName;
+    private final String nextOverdueStateName;
+    private final UUID userToken;
+
+
+    @JsonCreator
+    public DefaultOverdueChangeEvent(@JsonProperty("overdueObjectId") final UUID overdueObjectId,
+                                    @JsonProperty("overdueObjectType") final Blockable.Type overdueObjectType,
+                                    @JsonProperty("previousOverdueStateName") final String previousOverdueStateName,
+                                    @JsonProperty("nextOverdueStateName") final String nextOverdueStateName,
+                                    @JsonProperty("userToken") final UUID userToken) {
+        this.overdueObjectId = overdueObjectId;
+        this.overdueObjectType = overdueObjectType;
+        this.previousOverdueStateName = previousOverdueStateName;
+        this.nextOverdueStateName = nextOverdueStateName;
+        this.userToken = userToken;
+    }
+    
+    @Override
+    public BusEventType getBusEventType() {
+        return BusEventType.OVERDUE_CHANGE;
+    }
+
+    @Override
+    public UUID getUserToken() {
+        return userToken;
+    }
+    @Override
+    public String getPreviousOverdueStateName() {
+        return previousOverdueStateName;
+    }
+
+    @Override
+    public UUID getOverdueObjectId() {
+        return overdueObjectId;
+    }
+
+    @Override
+    public Type getOverdueObjectType() {
+        return overdueObjectType;
+    }
+
+    @Override
+    public String getNextOverdueStateName() {
+        return nextOverdueStateName;
+    }
+
+}
diff --git a/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java b/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java
index 3f9704b..86c9a72 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java
@@ -18,31 +18,39 @@ package com.ning.billing.overdue.applicator;
 
 import org.joda.time.DateTime;
 import org.joda.time.Period;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.inject.Inject;
 import com.ning.billing.ErrorCode;
 import com.ning.billing.junction.api.Blockable;
 import com.ning.billing.junction.api.BlockingApi;
+import com.ning.billing.junction.api.BlockingApiException;
 import com.ning.billing.junction.api.DefaultBlockingState;
 import com.ning.billing.ovedue.notification.OverdueCheckPoster;
 import com.ning.billing.overdue.OverdueApiException;
+import com.ning.billing.overdue.OverdueChangeEvent;
 import com.ning.billing.overdue.OverdueService;
 import com.ning.billing.overdue.OverdueState;
 import com.ning.billing.overdue.config.api.OverdueError;
+import com.ning.billing.util.bus.Bus;
 import com.ning.billing.util.clock.Clock;
 
 public class OverdueStateApplicator<T extends Blockable> {
+    private static final Logger log = LoggerFactory.getLogger(OverdueStateApplicator.class);
 
     private final BlockingApi blockingApi;
     private final Clock clock;
     private final OverdueCheckPoster poster;
+    private final Bus bus;
 
 
     @Inject
-    public OverdueStateApplicator(final BlockingApi accessApi, final Clock clock, final OverdueCheckPoster poster) {
+    public OverdueStateApplicator(final BlockingApi accessApi, final Clock clock, final OverdueCheckPoster poster, final Bus bus) {
         this.blockingApi = accessApi;
         this.clock = clock;
         this.poster = poster;
+        this.bus = bus;
     }
 
     public void apply(final T overdueable, final String previousOverdueStateName, final OverdueState<T> nextOverdueState) throws OverdueError {
@@ -65,9 +73,19 @@ public class OverdueStateApplicator<T extends Blockable> {
         if (nextOverdueState.isClearState()) {
             clear(overdueable);
         }
+        
+        try {
+            bus.post(createOverdueEvent(overdueable, previousOverdueStateName, nextOverdueState.getName()));
+        } catch (Exception e) {
+            log.error("Error posting overdue change event to bus",e);
+        }
     }
 
 
+    private OverdueChangeEvent createOverdueEvent(T overdueable, String previousOverdueStateName, String nextOverdueStateName) throws BlockingApiException {
+        return new DefaultOverdueChangeEvent(overdueable.getId(), Blockable.Type.get(overdueable), previousOverdueStateName, nextOverdueStateName, null);
+    }
+
     protected void storeNewState(final T blockable, final OverdueState<T> nextOverdueState) throws OverdueError {
         try {
             blockingApi.setBlockingState(new DefaultBlockingState(blockable.getId(), nextOverdueState.getName(), Blockable.Type.get(blockable),
diff --git a/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java b/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java
index a81e791..8536748 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java
@@ -48,6 +48,7 @@ public class DefaultOverdueModule extends AbstractModule implements OverdueModul
         bind(ExtendedOverdueService.class).to(DefaultOverdueService.class).asEagerSingleton();
         bind(OverdueCheckNotifier.class).to(DefaultOverdueCheckNotifier.class).asEagerSingleton();
         bind(OverdueCheckPoster.class).to(DefaultOverdueCheckPoster.class).asEagerSingleton();
+        
     }
 
     protected void installOverdueService() {
diff --git a/overdue/src/test/java/com/ning/billing/overdue/applicator/OverdueBusListenerTester.java b/overdue/src/test/java/com/ning/billing/overdue/applicator/OverdueBusListenerTester.java
new file mode 100644
index 0000000..ee5e833
--- /dev/null
+++ b/overdue/src/test/java/com/ning/billing/overdue/applicator/OverdueBusListenerTester.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.overdue.applicator;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.Subscribe;
+import com.ning.billing.overdue.OverdueChangeEvent;
+
+public class OverdueBusListenerTester {
+    public static final Logger log = LoggerFactory.getLogger(OverdueBusListenerTester.class);
+    
+    private List<OverdueChangeEvent> eventsReceived = new ArrayList<OverdueChangeEvent>();
+    
+    @Subscribe
+    public void handleOverdueChange(final OverdueChangeEvent changeEvent) {
+        log.info("Received subscription transition.");
+        eventsReceived.add(changeEvent);
+    }
+    
+    public List<OverdueChangeEvent> getEventsReceived() {
+        return eventsReceived;
+    }
+    
+    public void clearEventsReceived() {
+        eventsReceived.clear();
+    }
+
+}
diff --git a/overdue/src/test/java/com/ning/billing/overdue/applicator/OverdueListenerTesterModule.java b/overdue/src/test/java/com/ning/billing/overdue/applicator/OverdueListenerTesterModule.java
new file mode 100644
index 0000000..b0774b4
--- /dev/null
+++ b/overdue/src/test/java/com/ning/billing/overdue/applicator/OverdueListenerTesterModule.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.overdue.applicator;
+
+import com.google.inject.AbstractModule;
+import com.ning.billing.util.glue.BusModule;
+
+public class OverdueListenerTesterModule extends AbstractModule {
+
+    @Override
+    protected void configure() {
+        install(new BusModule(BusModule.BusType.PERSISTENT));
+        bind(OverdueBusListenerTester.class).asEagerSingleton();
+    }
+}
diff --git a/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java b/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java
index 35425d0..3205dc5 100644
--- a/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java
+++ b/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java
@@ -17,9 +17,13 @@
 package com.ning.billing.overdue.applicator;
 
 
+import static com.jayway.awaitility.Awaitility.await;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.UUID;
+import java.util.concurrent.Callable;
 
 import org.testng.annotations.Test;
 
@@ -31,14 +35,24 @@ import com.ning.billing.mock.BrainDeadProxyFactory.ZombieControl;
 import com.ning.billing.overdue.OverdueState;
 import com.ning.billing.overdue.OverdueTestBase;
 import com.ning.billing.overdue.config.OverdueConfig;
+import com.ning.billing.util.bus.Bus;
 import com.ning.billing.util.config.XMLLoader;
 
 public class TestOverdueStateApplicator extends OverdueTestBase {
     @Inject
     OverdueStateApplicator<SubscriptionBundle> applicator;
 
+    @Inject
+    OverdueBusListenerTester listener;
+    
+    @Inject 
+    Bus bus;
+    
+
     @Test(groups = {"slow"}, enabled = true)
     public void testApplicator() throws Exception {
+        bus.register(listener);
+        bus.start();
         final InputStream is = new ByteArrayInputStream(configXml.getBytes());
         config = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class);
         overdueWrapperFactory.setOverdueConfig(config);
@@ -51,6 +65,13 @@ public class TestOverdueStateApplicator extends OverdueTestBase {
         state = config.getBundleStateSet().findState("OD1");
         applicator.apply(bundle, BlockingApi.CLEAR_STATE_NAME, state);
         checkStateApplied(state);
+//        await().atMost(10, SECONDS).until(new Callable<Boolean>() {
+//            @Override
+//            public Boolean call() throws Exception {
+//                return listener.getEventsReceived().size() == 1;
+//            }
+//        });
+        
 
 
         state = config.getBundleStateSet().findState("OD2");
diff --git a/overdue/src/test/java/com/ning/billing/overdue/OverdueTestBase.java b/overdue/src/test/java/com/ning/billing/overdue/OverdueTestBase.java
index f68ce0a..afc93e5 100644
--- a/overdue/src/test/java/com/ning/billing/overdue/OverdueTestBase.java
+++ b/overdue/src/test/java/com/ning/billing/overdue/OverdueTestBase.java
@@ -52,6 +52,7 @@ import com.ning.billing.mock.glue.MockPaymentModule;
 import com.ning.billing.mock.glue.TestDbiModule;
 import com.ning.billing.overdue.applicator.ApplicatorMockJunctionModule;
 import com.ning.billing.overdue.applicator.ApplicatorMockJunctionModule.ApplicatorBlockingApi;
+import com.ning.billing.overdue.applicator.OverdueListenerTesterModule;
 import com.ning.billing.overdue.applicator.TestOverdueStateApplicator;
 import com.ning.billing.overdue.config.OverdueConfig;
 import com.ning.billing.overdue.glue.DefaultOverdueModule;
@@ -64,7 +65,7 @@ import com.ning.billing.util.glue.NotificationQueueModule;
 import com.ning.billing.util.io.IOUtils;
 import com.ning.billing.util.notificationq.NotificationQueueService.NotificationQueueAlreadyExists;
 
-@Guice(modules = {DefaultOverdueModule.class, MockClockModule.class, ApplicatorMockJunctionModule.class, CatalogModule.class, MockInvoiceModule.class, MockPaymentModule.class, BusModule.class, NotificationQueueModule.class, TestDbiModule.class})
+@Guice(modules = {DefaultOverdueModule.class,OverdueListenerTesterModule.class, MockClockModule.class, ApplicatorMockJunctionModule.class, CatalogModule.class, MockInvoiceModule.class, MockPaymentModule.class, NotificationQueueModule.class, TestDbiModule.class})
 public class OverdueTestBase {
     protected final String configXml =
             "<overdueConfig>" +