Skip to content

Commit 94c7523

Browse files
committed
Don't create IAMs in unit tests from other IAMs (use JSON only)
- some unit tests will create an IAM based off JSON of another IAM by calling toJSONObject() - toJSONObject uses key of "messageId" so we need to replace that with "id" for creating an IAM - added convertIAMtoJSONObject() that replaces "messageId" with "id" - refactored code to use this method instead of toJSONObject() - removed OSTestInAppMessageInternal constructor that took argument of OSInAppMessageInternal in favor of passing in JSON only
1 parent f409462 commit 94c7523

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

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
@@ -358,10 +358,6 @@ public OSTestInAppMessageInternal(@NonNull String messageId, int displaysQuantit
358358
super(json);
359359
}
360360

361-
OSTestInAppMessageInternal(OSInAppMessageInternal inAppMessage) throws JSONException {
362-
super(inAppMessage.toJSONObject());
363-
}
364-
365361
public void setMessageId(String messageId) {
366362
this.messageId = messageId;
367363
}
@@ -586,7 +582,8 @@ public static List<OSTestInAppMessageInternal> getRedisplayInAppMessages() {
586582

587583
for (OSInAppMessageInternal message : messages) {
588584
try {
589-
OSTestInAppMessageInternal testInAppMessage = new OSTestInAppMessageInternal(message);
585+
JSONObject json = InAppMessagingHelpers.convertIAMtoJSONObject(message);
586+
OSTestInAppMessageInternal testInAppMessage = new OSTestInAppMessageInternal(json);
590587
testInAppMessage.getRedisplayStats().setDisplayStats(message.getRedisplayStats());
591588
testMessages.add(testInAppMessage);
592589

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ private void setMockRegistrationResponseWithMessages(ArrayList<OSTestInAppMessag
19111911
final JSONArray jsonMessages = new JSONArray();
19121912

19131913
for (OSTestInAppMessageInternal message : messages)
1914-
jsonMessages.put(message.toJSONObject());
1914+
jsonMessages.put(InAppMessagingHelpers.convertIAMtoJSONObject(message));
19151915

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

0 commit comments

Comments
 (0)