Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -255,7 +255,7 @@ public Content getContent() {
return content;
}

@NonNull
@Nullable
public JSONObject getCustomPayload() {
return customPayload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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", "<html><body>Test</body></html>")
.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", "<html><body>Test</body></html>")
.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", "<html><body>Test</body></html>")
.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", "<html><body>Test</body></html>")
.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"));
Expand Down
Loading