Skip to content

Commit

Permalink
Pull request #76: Ikresic MM-6561 add custompayload to the action mom…
Browse files Browse the repository at this point in the history
…essage

Merge in MML/infobip-mobile-messaging-huawei from ikresic-MM-6561-add-custompayload-to-the-action-momessage to master

Squashed commit of the following:

commit 8ca7ca84b7a4aa9eb4349d64b6680c45e4a4d776
Author: ikresic <ivan.kresic@infobip.com>
Date:   Fri Aug 2 16:36:00 2024 +0200

    - provided customPayload to the MOmessage

commit 0d2a4620977546b06de080d75a456a39656fa6f5
Author: ikresic <ivan.kresic@infobip.com>
Date:   Fri Aug 2 15:33:42 2024 +0200

    - provided customPayload to the MOmessage

commit 23548d679128087175050486720c6998689f1cdb
Author: ikresic <ivan.kresic@infobip.com>
Date:   Fri Aug 2 14:38:16 2024 +0200

    - provided customPayload to the MOmessage
  • Loading branch information
ikresicc committed Aug 7, 2024
1 parent 2289962 commit 0bb75c2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.infobip.mobile.messaging.interactive.platform.InteractiveBroadcaster;
import org.infobip.mobile.messaging.interactive.platform.MockActivity;
import org.infobip.mobile.messaging.interactive.tools.MobileMessagingTestCase;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
Expand All @@ -33,6 +35,7 @@ public class NotificationActionTapReceiverTest extends MobileMessagingTestCase {
private ArgumentCaptor<Message> messageArgumentCaptor;
private NotificationActionTapReceiver notificationActionTapReceiver;
private ActivityStarterWrapper activityStarterWrapper;
private JSONObject userInfo;

@Override
public void setUp() throws Exception {
Expand All @@ -56,6 +59,32 @@ public void setUp() throws Exception {

//noinspection WrongConstant
Mockito.when(contextMock.getSystemService(eq(Context.NOTIFICATION_SERVICE))).thenReturn(notificationManagerMock);

try {
userInfo = new JSONObject();
userInfo.put("messageId", "random-message-id-123456");

JSONObject aps = new JSONObject();
JSONObject alert = new JSONObject();
alert.put("body", "text");
aps.put("alert", alert);
aps.put("category", "categoryId");
userInfo.put("aps", aps);

JSONObject customPayload = new JSONObject();
customPayload.put("key", "value");

JSONObject nestedObject = new JSONObject();
nestedObject.put("key", "value");
customPayload.put("nestedObject", nestedObject);

customPayload.put("nullAttribute", JSONObject.NULL);
customPayload.put("numberAttribute", 123);

userInfo.put("customPayload", customPayload);
} catch (JSONException e) {
e.printStackTrace();
}
}

@Test
Expand All @@ -66,7 +95,9 @@ public void test_should_send_notification_action_clicked_event() throws Exceptio
NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction);
Message givenMessage = createMessage(context, "SomeMessageId", givenNotificationCategory.getCategoryId(), false);
int givenNotificationId = 1234;
givenMessage.setCustomPayload(userInfo);
Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId);
givenMessage.setCustomPayload(userInfo);

// When
notificationActionTapReceiver.onReceive(contextMock, givenIntent);
Expand All @@ -92,8 +123,10 @@ public void test_should_send_action_clicked_event_and_open_activity_if_action_sh
NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction);
Message givenMessage = createMessage(context, "SomeMessageId", givenNotificationCategory.getCategoryId(), false);
int givenNotificationId = 1234;
givenMessage.setCustomPayload(userInfo);
Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId);
Mockito.when(broadcastSender.notificationActionTapped(any(Message.class), any(NotificationCategory.class), any(NotificationAction.class))).thenReturn(givenIntent);
givenMessage.setCustomPayload(userInfo);

// When
notificationActionTapReceiver.onReceive(contextMock, givenIntent);
Expand All @@ -119,8 +152,10 @@ public void test_should_send_action_clicked_event_and_not_open_activity_if_actio
NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction);
Message givenMessage = createMessage(context, "SomeMessageId", givenNotificationCategory.getCategoryId(), false);
int givenNotificationId = 1234;
givenMessage.setCustomPayload(userInfo);
Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId);
Mockito.when(broadcastSender.notificationActionTapped(any(Message.class), any(NotificationCategory.class), any(NotificationAction.class))).thenReturn(givenIntent);
givenMessage.setCustomPayload(userInfo);

// When
notificationActionTapReceiver.onReceive(contextMock, givenIntent);
Expand Down Expand Up @@ -167,6 +202,7 @@ private void verifyProperPayloadWasSentAndActionsTriggered(NotificationAction gi
assertJEquals(givenTappedNotificationAction, actualAction);
assertJEquals(givenNotificationCategory, actualCategory);
assertJEquals(givenMessage, actualMessage);
assertJEquals(givenMessage.getCustomPayload(), actualMessage.getCustomPayload());

Mockito.verify(mobileInteractive, Mockito.times(1)).triggerSdkActionsFor(actualAction, actualMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.infobip.mobile.messaging.storage.SQLiteMessageStore;
import org.infobip.mobile.messaging.tools.MobileMessagingTestCase;
import org.infobip.mobile.messaging.util.PreferenceHelper;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

Expand Down Expand Up @@ -130,6 +132,37 @@ public void shouldSendMultipleMessages() throws Exception {
assertEquals(false, messages.get(1).getCustomPayload().opt("myBooleanKey"));
}

@Test
public void testCustomPayloadNestedObjects() {
String jsonStr = "{\n" +
" \"messageId\": \"messageId\",\n" +
" \"aps\": {\n" +
" \"badge\": 6,\n" +
" \"sound\": \"default\",\n" +
" \"alert\": {\n" +
" \"bidy\":\"text\"\n" +
" }\n" +
" },\n" +
" \"customPayload\": {\n" +
" \"key\": \"value\",\n" +
" \"nestedObject\": {\n" +
" \"key\": \"value\"\n" +
" }\n" +
" }\n" +
"}";

JSONObject message = null;
JSONObject expectedCustomPayload = new JSONObject();
try {
message = new JSONObject(jsonStr);
expectedCustomPayload.put("key", "value");
expectedCustomPayload.put("nestedObject", new JSONObject().put("key", "value"));
assertEquals(expectedCustomPayload.toString(), message.getJSONObject("customPayload").toString());
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

@Test
public void shouldOnlyKeepMessagesToBeRetried() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private Message messageFor(final String categoryId, final NotificationAction act
message.setBody(categoryId + " " + action.getId());
HashMap<String, String> map = new HashMap<>();
map.put("initialMessageId", initialMessage.getMessageId());
message.setCustomPayload(initialMessage.getCustomPayload());
message.setInternalData(InternalDataMapper.mergeExistingInternalDataWithAnythingToJson(initialMessage.getInternalData(), map));
return message;
}
Expand Down

0 comments on commit 0bb75c2

Please sign in to comment.