diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppMessage.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppMessage.java index 6b948a4b1..c049f3f6d 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppMessage.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppMessage.java @@ -17,7 +17,7 @@ public class IterableInAppMessage { private final @NonNull String messageId; private final @NonNull Content content; - private final @NonNull JSONObject customPayload; + private final @Nullable JSONObject customPayload; private final @NonNull Date createdAt; private final @NonNull Date expiresAt; private final @NonNull Trigger trigger; @@ -35,7 +35,7 @@ public class IterableInAppMessage { IterableInAppMessage(@NonNull String messageId, @NonNull Content content, - @NonNull JSONObject customPayload, + @Nullable JSONObject customPayload, @NonNull Date createdAt, @NonNull Date expiresAt, @NonNull Trigger trigger, @@ -255,7 +255,7 @@ public Content getContent() { return content; } - @NonNull + @Nullable public JSONObject getCustomPayload() { return customPayload; } diff --git a/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppMessageTest.java b/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppMessageTest.java index 69e1b9187..1bb613296 100644 --- a/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppMessageTest.java +++ b/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppMessageTest.java @@ -11,6 +11,7 @@ import org.skyscreamer.jsonassert.JSONCompareMode; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -47,6 +48,93 @@ public void testInAppMessageDeserialization() throws Exception { } } + @Test + public void testHtmlMessageWithoutCustomPayload() throws Exception { + JSONObject messageJson = new JSONObject() + .put("messageId", "test123") + .put("content", new JSONObject() + .put("html", "Test") + .put("inAppDisplaySettings", new JSONObject() + .put("top", new JSONObject().put("percentage", 0)) + .put("right", new JSONObject().put("percentage", 0)) + .put("bottom", new JSONObject().put("percentage", 0)) + .put("left", new JSONObject().put("percentage", 0)))); + + IterableInAppMessage message = IterableInAppMessage.fromJSONObject(messageJson, null); + assertNotNull(message); + assertNull(message.getCustomPayload()); + } + + @Test + public void testEmptyCustomPayloadReturnsEmptyObject() throws Exception { + JSONObject messageJson = new JSONObject() + .put("messageId", "test123") + .put("customPayload", new JSONObject()) + .put("content", new JSONObject() + .put("html", "Test") + .put("inAppDisplaySettings", new JSONObject() + .put("top", new JSONObject().put("percentage", 0)) + .put("right", new JSONObject().put("percentage", 0)) + .put("bottom", new JSONObject().put("percentage", 0)) + .put("left", new JSONObject().put("percentage", 0)))); + + IterableInAppMessage message = IterableInAppMessage.fromJSONObject(messageJson, null); + assertNotNull(message.getCustomPayload()); + assertEquals(0, message.getCustomPayload().length()); + } + + @Test + public void testNullCustomPayloadSerializationRoundTrip() throws Exception { + JSONObject messageJson = new JSONObject() + .put("messageId", "test123") + .put("content", new JSONObject() + .put("html", "Test") + .put("inAppDisplaySettings", new JSONObject() + .put("top", new JSONObject().put("percentage", 0)) + .put("right", new JSONObject().put("percentage", 0)) + .put("bottom", new JSONObject().put("percentage", 0)) + .put("left", new JSONObject().put("percentage", 0)))); + + IterableInAppMessage message = IterableInAppMessage.fromJSONObject(messageJson, null); + assertNull(message.getCustomPayload()); + + JSONObject serialized = message.toJSONObject(); + assertFalse(serialized.has("customPayload")); + + IterableInAppMessage rehydrated = IterableInAppMessage.fromJSONObject(serialized, null); + assertNull(rehydrated.getCustomPayload()); + } + + @Test + public void testCustomPayloadTakesPrecedenceOverLegacyPayload() throws Exception { + JSONObject messageJson = new JSONObject() + .put("messageId", "test123") + .put("customPayload", new JSONObject().put("source", "customPayload")) + .put("content", new JSONObject() + .put("html", "Test") + .put("payload", new JSONObject().put("source", "legacyPayload")) + .put("inAppDisplaySettings", new JSONObject() + .put("top", new JSONObject().put("percentage", 0)) + .put("right", new JSONObject().put("percentage", 0)) + .put("bottom", new JSONObject().put("percentage", 0)) + .put("left", new JSONObject().put("percentage", 0)))); + + IterableInAppMessage message = IterableInAppMessage.fromJSONObject(messageJson, null); + assertEquals("customPayload", message.getCustomPayload().getString("source")); + } + + @Test + public void testJsonOnlyWithoutCustomPayloadReturnsEmptyObject() throws Exception { + JSONObject messageJson = new JSONObject() + .put("messageId", "test123") + .put("jsonOnly", true) + .put("trigger", new JSONObject().put("type", "never")); + + IterableInAppMessage message = IterableInAppMessage.fromJSONObject(messageJson, null); + assertNotNull(message.getCustomPayload()); + assertEquals(0, message.getCustomPayload().length()); + } + @Test public void testInAppLegacyPayloadDeserialization() throws Exception { JSONObject payload = new JSONObject(IterableTestUtils.getResourceString("inapp_payload_legacy.json"));