Skip to content

Commit bbd8be1

Browse files
authored
Merge pull request #1461 from OneSignal/feat/OSInAppMessage-toJSONObject
Add `toJSONObject` method to `OSInAppMessage`
2 parents bbd6545 + c3432df commit bbd8be1

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessage.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929

3030
import androidx.annotation.NonNull;
3131

32+
import org.json.JSONException;
33+
import org.json.JSONObject;
34+
3235
public class OSInAppMessage {
3336

37+
public static final String IAM_ID = "messageId";
38+
3439
/**
3540
* The unique identifier for this in-app message
3641
*/
@@ -46,4 +51,16 @@ public String getMessageId() {
4651
return messageId;
4752
}
4853

54+
public JSONObject toJSONObject() {
55+
JSONObject mainObj = new JSONObject();
56+
try {
57+
mainObj.put(IAM_ID, messageId);
58+
}
59+
catch(JSONException e) {
60+
e.printStackTrace();
61+
}
62+
63+
return mainObj;
64+
}
65+
4966
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageInternal.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
class OSInAppMessageInternal extends OSInAppMessage {
1919

20-
private static final String IAM_ID = "id";
20+
// "id" is expected instead of "messageId" when parsing JSON from the backend
21+
private static final String ID = "id";
22+
private static final String IAM_ID = "messageId";
2123
private static final String IAM_VARIANTS = "variants";
2224
private static final String IAM_TRIGGERS = "triggers";
2325
private static final String IAM_REDISPLAY_STATS = "redisplay";
@@ -74,7 +76,8 @@ class OSInAppMessageInternal extends OSInAppMessage {
7476

7577
OSInAppMessageInternal(JSONObject json) throws JSONException {
7678
// initialize simple root properties
77-
super(json.getString(IAM_ID));
79+
// "id" is expected instead of "messageId" when parsing JSON from the backend
80+
super(json.getString(ID));
7881
this.variants = parseVariants(json.getJSONObject(IAM_VARIANTS));
7982
this.triggers = parseTriggerJson(json.getJSONArray(IAM_TRIGGERS));
8083
this.clickedClickIds = new HashSet<>();
@@ -145,7 +148,8 @@ protected ArrayList<ArrayList<OSTrigger>> parseTriggerJson(JSONArray triggersJso
145148
return parsedTriggers;
146149
}
147150

148-
JSONObject toJSONObject() {
151+
@Override
152+
public JSONObject toJSONObject() {
149153
JSONObject json = new JSONObject();
150154

151155
try {

OneSignalSDK/unittest/src/test/java/com/onesignal/InAppMessagingHelpers.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ public class InAppMessagingHelpers {
1919
public static final String IAM_PAGE_ID = "12345678-1234-ABCD-1234-123456789012";
2020
public static final String IAM_HAS_LIQUID = "has_liquid";
2121

22+
// unit tests will create an IAM based off JSON of another IAM
23+
// toJSONObject uses key of "messageId" so we need to replace that with "id" for creating IAM
24+
public static JSONObject convertIAMtoJSONObject(OSInAppMessageInternal inAppMessage) {
25+
JSONObject json = inAppMessage.toJSONObject();
26+
try {
27+
json.put("id", json.get("messageId"));
28+
json.remove("messageId");
29+
} catch (JSONException e) {
30+
e.printStackTrace();
31+
}
32+
33+
return json;
34+
}
35+
2236
public static boolean evaluateMessage(OSInAppMessageInternal message) {
2337
return OneSignal.getInAppMessageController().triggerController.evaluateMessageTriggers(message);
2438
}

OneSignalSDK/unittest/src/test/java/com/onesignal/OneSignalPackagePrivateHelper.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,6 @@ public OSTestInAppMessageInternal(@NonNull String messageId, int displaysQuantit
362362
super(json);
363363
}
364364

365-
OSTestInAppMessageInternal(OSInAppMessageInternal inAppMessage) throws JSONException {
366-
super(inAppMessage.toJSONObject());
367-
}
368-
369365
public void setMessageId(String messageId) {
370366
this.messageId = messageId;
371367
}
@@ -590,7 +586,8 @@ public static List<OSTestInAppMessageInternal> getRedisplayInAppMessages() {
590586

591587
for (OSInAppMessageInternal message : messages) {
592588
try {
593-
OSTestInAppMessageInternal testInAppMessage = new OSTestInAppMessageInternal(message);
589+
JSONObject json = InAppMessagingHelpers.convertIAMtoJSONObject(message);
590+
OSTestInAppMessageInternal testInAppMessage = new OSTestInAppMessageInternal(json);
594591
testInAppMessage.getRedisplayStats().setDisplayStats(message.getRedisplayStats());
595592
testMessages.add(testInAppMessage);
596593

OneSignalSDK/unittest/src/test/java/com/test/onesignal/InAppMessageIntegrationTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,11 +1907,32 @@ public void testLiquidIAMDisplayWaitsForGetTags() throws Exception {
19071907
assertEquals("in_app_messages/" + message.getMessageId() + "/impression", lastRequest.url);
19081908
}
19091909

1910+
// Test toJSONObject() method currently only checks JSON for "messageId"
1911+
@Test
1912+
public void testInAppMessageInternalToJSONObject_messageId() throws Exception {
1913+
// 1. Create a basic test IAM
1914+
OSTestInAppMessageInternal iam = InAppMessagingHelpers.buildTestMessage(null);
1915+
1916+
// 2. Set a message ID for the IAM
1917+
String messageId = new String(new char[64]).replace('\0', '0');
1918+
iam.setMessageId(messageId);
1919+
1920+
// 3. Init
1921+
OneSignalInit();
1922+
threadAndTaskWait();
1923+
1924+
// 4. call toJSONObject() on IAM
1925+
JSONObject testJsonObj = iam.toJSONObject();
1926+
1927+
// 5. Check "messageId" in JSON Object
1928+
assertEquals(messageId, testJsonObj.optString("messageId"));
1929+
}
1930+
19101931
private void setMockRegistrationResponseWithMessages(ArrayList<OSTestInAppMessageInternal> messages) throws JSONException {
19111932
final JSONArray jsonMessages = new JSONArray();
19121933

19131934
for (OSTestInAppMessageInternal message : messages)
1914-
jsonMessages.put(message.toJSONObject());
1935+
jsonMessages.put(InAppMessagingHelpers.convertIAMtoJSONObject(message));
19151936

19161937
ShadowOneSignalRestClient.setNextSuccessfulRegistrationResponse(new JSONObject() {{
19171938
put("id", "df8f05be55ba-b2f7f966-d8cc-11e4-bed1");

0 commit comments

Comments
 (0)