killbill-uncached

profiles: Add metadata into push notification events. Fixes

10/13/2017 9:15:12 PM

Details

diff --git a/jaxrs/src/main/java/org/killbill/billing/jaxrs/json/NotificationJson.java b/jaxrs/src/main/java/org/killbill/billing/jaxrs/json/NotificationJson.java
index 22fe656..2f5fbbe 100644
--- a/jaxrs/src/main/java/org/killbill/billing/jaxrs/json/NotificationJson.java
+++ b/jaxrs/src/main/java/org/killbill/billing/jaxrs/json/NotificationJson.java
@@ -33,23 +33,27 @@ public class NotificationJson {
     private final String objectType;
     @ApiModelProperty(dataType = "java.util.UUID")
     private final String objectId;
+    private String metaData;
 
     @JsonCreator
     public NotificationJson(@JsonProperty("eventType") final String eventType,
                             @JsonProperty("accountId") final String accountId,
                             @JsonProperty("objectType") final String objectType,
-                            @JsonProperty("objectId") final String objectId) {
+                            @JsonProperty("objectId") final String objectId,
+                            @JsonProperty("metaData") final String metaData) {
         this.eventType = eventType;
         this.accountId = accountId;
         this.objectType = objectType;
         this.objectId = objectId;
+        this.metaData = metaData;
     }
 
     public NotificationJson(final ExtBusEvent event) {
         this(event.getEventType().toString(),
              event.getAccountId() != null ? event.getAccountId().toString() : null,
              event.getObjectType().toString(),
-             event.getObjectId() != null ? event.getObjectId().toString() : null);
+             event.getObjectId() != null ? event.getObjectId().toString() : null,
+             event.getMetaData());
     }
 
     public String getEventType() {
@@ -67,4 +71,8 @@ public class NotificationJson {
     public String getObjectId() {
         return objectId;
     }
+
+    public String getMetaData() {
+        return metaData;
+    }
 }
diff --git a/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationKey.java b/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationKey.java
index e72dec3..eb25be3 100644
--- a/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationKey.java
+++ b/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationKey.java
@@ -33,6 +33,7 @@ public class PushNotificationKey implements NotificationEvent {
     private final UUID objectId;
     private final int attemptNumber;
     private final String url;
+    private final String metaData;
 
     @JsonCreator
     public PushNotificationKey(@JsonProperty("tenantId") final UUID tenantId,
@@ -41,6 +42,7 @@ public class PushNotificationKey implements NotificationEvent {
                                @JsonProperty("objectType") final String objectType,
                                @JsonProperty("objectId") final UUID objectId,
                                @JsonProperty("attemptNumber")  final int attemptNumber,
+                               @JsonProperty("metaData")  final String metaData,
                                @JsonProperty("url") final String url) {
         this.tenantId = tenantId;
         this.accountId = accountId;
@@ -48,12 +50,13 @@ public class PushNotificationKey implements NotificationEvent {
         this.objectType = objectType;
         this.objectId = objectId;
         this.attemptNumber = attemptNumber;
+        this.metaData = metaData;
         this.url = url;
     }
 
     public PushNotificationKey(final PushNotificationKey key, final int attemptNumber) {
         this(key.getTenantId(), key.getAccountId(), key.getEventType(), key.getObjectType(), key.getObjectId(),
-             attemptNumber, key.getUrl());
+             attemptNumber, key.getMetaData(), key.getUrl());
     }
 
     public UUID getTenantId() {
@@ -84,6 +87,10 @@ public class PushNotificationKey implements NotificationEvent {
         return url;
     }
 
+    public String getMetaData() {
+        return metaData;
+    }
+
     @Override
     public String toString() {
         return "PushNotificationKey{" +
@@ -92,6 +99,7 @@ public class PushNotificationKey implements NotificationEvent {
                ", eventType='" + eventType + '\'' +
                ", objectType='" + objectType + '\'' +
                ", objectId=" + objectId +
+               ", metaData=" + metaData +
                ", attemptNumber=" + attemptNumber +
                ", url='" + url + '\'' +
                '}';
diff --git a/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationListener.java b/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationListener.java
index 0f35812..01875b1 100644
--- a/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationListener.java
+++ b/profiles/killbill/src/main/java/org/killbill/billing/server/notifications/PushNotificationListener.java
@@ -162,7 +162,8 @@ public class PushNotificationListener {
         final NotificationJson notification = new NotificationJson(key.getEventType() != null ? key.getEventType().toString() : null,
                                                                    key.getAccountId() != null ? key.getAccountId().toString() : null,
                                                                    key.getObjectType() != null ? key.getObjectType().toString() : null,
-                                                                   key.getObjectId() != null ? key.getObjectId().toString() : null);
+                                                                   key.getObjectId() != null ? key.getObjectId().toString() : null,
+                                                                   key.getMetaData());
         final String body = mapper.writeValueAsString(notification);
         doPost(key.getTenantId(), key.getUrl(), body, notification, TIMEOUT_NOTIFICATION, key.getAttemptNumber());
     }
@@ -173,7 +174,9 @@ public class PushNotificationListener {
                                                                 notificationJson.getEventType(),
                                                                 notificationJson.getObjectType(),
                                                                 notificationJson.getObjectId() != null ? UUID.fromString(notificationJson.getObjectId()) : null,
-                                                                attemptRetryNumber + 1, url);
+                                                                attemptRetryNumber + 1,
+                                                                notificationJson.getMetaData(),
+                                                                url);
 
         final TenantContext tenantContext = contextFactory.createTenantContext(tenantId);
         final DateTime nextNotificationTime = getNextNotificationTime(key.getAttemptNumber(), internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(tenantContext));
diff --git a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPushNotification.java b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPushNotification.java
index 28eb1ee..4b32eba 100644
--- a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPushNotification.java
+++ b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPushNotification.java
@@ -34,6 +34,7 @@ import org.eclipse.jetty.servlet.ServletHolder;
 import org.killbill.billing.client.KillBillClientException;
 import org.killbill.billing.client.model.TenantKey;
 import org.killbill.billing.jaxrs.json.NotificationJson;
+import org.killbill.billing.notification.plugin.api.ExtBusEventType;
 import org.killbill.billing.server.notifications.PushNotificationListener;
 import org.killbill.billing.tenant.api.TenantKV;
 import org.slf4j.Logger;
@@ -140,8 +141,7 @@ public class TestPushNotification extends TestJaxrsBase {
         final String callback = "http://127.0.0.1:" + SERVER_PORT + CALLBACK_ENDPOINT;
         final TenantKey result0 = killBillClient.registerCallbackNotificationForTenant(callback, requestOptions);
 
-        assertAllCallbacksCompleted();
-        Assert.assertTrue(callbackCompletedWithError); // expected true because is not an ACCOUNT_CREATION event
+        Assert.assertTrue(waitForCallbacksToComplete());
 
         Assert.assertEquals(result0.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
         Assert.assertEquals(result0.getValues().size(), 1);
@@ -321,14 +321,12 @@ public class TestPushNotification extends TestJaxrsBase {
         public CallmebackServlet(final TestPushNotification test) {
             this.test = test;
             this.receivedCalls = new AtomicInteger(0);
-            this.withError = false;
         }
 
         @Override
         protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
             final int current = receivedCalls.incrementAndGet();
             final String body = CharStreams.toString(new InputStreamReader(request.getInputStream(), "UTF-8"));
-            withError = false;
 
             log.info("CallmebackServlet received {} calls , current = {} at {}", current, body, getClock().getUTCNow());
 
@@ -343,22 +341,31 @@ public class TestPushNotification extends TestJaxrsBase {
 
             log.info("Got body {}", body);
 
-            try {
-                final NotificationJson notification = objectMapper.readValue(body, NotificationJson.class);
-                Assert.assertEquals(notification.getEventType(), "ACCOUNT_CREATION");
-                Assert.assertEquals(notification.getObjectType(), "ACCOUNT");
-                Assert.assertNotNull(notification.getObjectId());
-                Assert.assertNotNull(notification.getAccountId());
-                Assert.assertEquals(notification.getObjectId(), notification.getAccountId());
-
-                test.retrieveAccountWithAsserts(notification.getObjectId());
-
-                Assert.assertEquals(request.getHeader(PushNotificationListener.HTTP_HEADER_CONTENT_TYPE), PushNotificationListener.CONTENT_TYPE_JSON);
-            } catch (final AssertionError e) {
-                withError = true;
+            final NotificationJson notification = objectMapper.readValue(body, NotificationJson.class);
+
+            final ExtBusEventType type = ExtBusEventType.valueOf(notification.getEventType());
+            switch (type) {
+                case TENANT_CONFIG_CHANGE:
+                    Assert.assertEquals(notification.getEventType(), "TENANT_CONFIG_CHANGE");
+                    Assert.assertEquals(notification.getObjectType(), "TENANT_KVS");
+                    Assert.assertNotNull(notification.getObjectId());
+                    Assert.assertNull(notification.getAccountId());
+                    Assert.assertNotNull(notification.getMetaData());
+                    Assert.assertEquals(notification.getMetaData(), "PUSH_NOTIFICATION_CB");
+                    break;
+                case ACCOUNT_CREATION:
+                    Assert.assertEquals(notification.getEventType(), "ACCOUNT_CREATION");
+                    Assert.assertEquals(notification.getObjectType(), "ACCOUNT");
+                    Assert.assertNotNull(notification.getObjectId());
+                    Assert.assertNotNull(notification.getAccountId());
+                    Assert.assertEquals(notification.getObjectId(), notification.getAccountId());
+                    break;
             }
 
-            stopServerWhenComplete(current, withError);
+            test.retrieveAccountWithAsserts(notification.getObjectId());
+
+            Assert.assertEquals(request.getHeader(PushNotificationListener.HTTP_HEADER_CONTENT_TYPE), PushNotificationListener.CONTENT_TYPE_JSON);
+            stopServerWhenComplete(current, false);
         }
 
         private void stopServerWhenComplete(final int current, final boolean withError) {