Skip to content

Commit 9dd8b0c

Browse files
committed
Pull request #469: Aboldyrev MM-6879 mappers
Merge in MML/infobip-mobile-messaging-android from aboldyrev-MM-6879-mappers to master Squashed commit of the following: commit 64d9b08cb15bd8b818ea30baa91f724a9860c6fb Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Wed Nov 13 18:53:59 2024 +0300 json objectified commit 6d6db2f382805ce176e01878cff501296c61123b Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Wed Nov 13 12:58:05 2024 +0300 PR comments commit fb48e5880886bfb8c14f7d90c041904674165741 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 16:11:18 2024 +0300 description commit 78cd92eae873995d7085aabfbb42dfda055ae7a4 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 16:09:28 2024 +0300 descriptions commit a56ce62ab5bde2c1b0cc1cdbf336ff18e0faee45 Merge: c069acef 9943971 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 16:01:18 2024 +0300 Merge branch 'master' into aboldyrev-MM-6879-mappers commit c069acef0a78095924b408f309923b9dd8f94168 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 15:59:20 2024 +0300 personalization context + naming unification commit a8c7f5828664917f89eb11dd84922f60854b6129 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 13:00:19 2024 +0300 installation json removed extra commit 6fdbb6a550ffcf58d8e94a5626cebd10a92fac86 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 12:43:58 2024 +0300 missing parameter for test and extracted mobile inbox attributes commit 9e4bff842cefd8ab10e9338afa7989b46435cb4d Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 12:32:21 2024 +0300 missing webViewUrl added commit b10beaf5068f5028e647b376bcd289d13823b7a5 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 12:03:16 2024 +0300 extra tests commit c7b9875633a1001fc21c965bd122533164462029 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Tue Nov 12 11:35:26 2024 +0300 user json test commit 0662c40abe804ae6036d0f64ed9104fb5115ae72 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Mon Nov 11 18:58:08 2024 +0300 message json test commit 985bfb8737a7d0ae9be6e704a27b9c6eef240601 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Mon Nov 11 17:31:16 2024 +0300 installation json tests commit 20df6bcbf475896cef5d2926f2bfed3d79a8733d Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 8 19:23:09 2024 +0300 custom event json test commit 4c09ae7bb5961971176b6627335766c0b9ed4f3a Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 8 16:24:14 2024 +0300 mobile inbox mappers tests commit 136719fa0792ce140f30b403b7882f98e058672a Merge: 4ff81836 8734e564 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 8 15:27:35 2024 +0300 Merge branch 'aboldyrev-MM-6908-sdk_versioning' into aboldyrev-MM-6879-mappers commit 8734e564b0ae040bb931437812bd74ad39ac009c Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 8 13:52:59 2024 +0300 added test to avoid null being set again commit 7c56e41a5941bc320a0aeae0bff9e8fb4be616f0 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 8 11:52:44 2024 +0300 versioning commit 4ff8183608a511d78abe3898aaed534552219d6b Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 8 11:49:39 2024 +0300 some tests commit 2bab878d70f45402f57c401503a674ac6b092d49 Author: Alexander Boldyrev <alexander.boldyrev@infobip.com> Date: Fri Nov 1 21:33:25 2024 +0300 mappers part I
1 parent 9943971 commit 9dd8b0c

File tree

15 files changed

+896
-2
lines changed

15 files changed

+896
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.infobip.mobile.messaging.inbox;
2+
3+
import static org.infobip.mobile.messaging.inbox.MobileInboxFilterOptionsJson.mobileInboxFilterOptionsFromJSON;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.infobip.mobile.messaging.util.DateTimeUtil;
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
import org.junit.Test;
11+
12+
public class MobileInboxMappersTest {
13+
14+
@Test
15+
public void inboxMapper_toJSON_return_empty_for_null() {
16+
JSONObject inbox = InboxMapper.toJSON(null);
17+
assertEquals(inbox.length(), 0);
18+
}
19+
20+
@Test
21+
public void inboxMapper_toJSON() {
22+
Inbox inbox = new Inbox();
23+
inbox.setCountTotal(2);
24+
inbox.setCountUnread(1);
25+
JSONObject inboxJSON = InboxMapper.toJSON(inbox);
26+
assertEquals(inboxJSON.toString(), "{\"countTotal\":2,\"countUnread\":1}");
27+
}
28+
29+
@Test
30+
public void mobileInboxFilterOptionsFromJSON_is_null_for_null() {
31+
assertNull(mobileInboxFilterOptionsFromJSON(null));
32+
}
33+
34+
@Test
35+
public void mobileInboxFilterOptionsFromJSON_is_object_with_nulls() throws JSONException {
36+
MobileInboxFilterOptions filterOptions = mobileInboxFilterOptionsFromJSON(new JSONObject("{}"));
37+
38+
assertNull(filterOptions.getLimit());
39+
assertNull(filterOptions.getTopic());
40+
assertNull(filterOptions.getFromDateTime());
41+
assertNull(filterOptions.getToDateTime());
42+
}
43+
44+
@Test
45+
public void mobileInboxFilterOptionsFromJSON_is_object_with_actual_values() throws JSONException {
46+
String fromDateStr = "2024-11-07T00:00:00Z";
47+
String toDateStr = "2024-11-08T00:00:00Z";
48+
int limit = 100;
49+
String topic = "topicName";
50+
51+
JSONObject jsonObject = new JSONObject("{\"fromDateTime\":\"" + fromDateStr + "\"," +
52+
"\"toDateTime\":\"" + toDateStr + "\"," +
53+
"\"topic\":\"" + topic + "\"," +
54+
"\"limit\":" + limit + "}");
55+
56+
MobileInboxFilterOptions filterOptions = mobileInboxFilterOptionsFromJSON(jsonObject);
57+
58+
assertEquals(limit, (int) filterOptions.getLimit());
59+
assertEquals("topicName", filterOptions.getTopic());
60+
assertEquals(DateTimeUtil.ISO8601DateFromString(fromDateStr), filterOptions.getFromDateTime());
61+
assertEquals(DateTimeUtil.ISO8601DateFromString(toDateStr), filterOptions.getToDateTime());
62+
}
63+
}

infobip-mobile-messaging-android-inbox-sdk/src/main/java/org/infobip/mobile/messaging/inbox/Inbox.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Inbox {
2121

2222
/**
2323
* Total number of messages available in the Inbox. Maximum is limited to 100 messages.
24-
*/
24+
*/
2525
public int getCountTotal() {
2626
return countTotal;
2727
}

infobip-mobile-messaging-android-inbox-sdk/src/main/java/org/infobip/mobile/messaging/inbox/InboxMapper.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.infobip.mobile.messaging.api.inbox.FetchInboxResponse;
77
import org.infobip.mobile.messaging.api.messages.MessageResponse;
88
import org.infobip.mobile.messaging.dal.json.InternalDataMapper;
9+
import org.infobip.mobile.messaging.logging.MobileMessagingLogger;
910
import org.infobip.mobile.messaging.platform.Time;
1011
import org.json.JSONException;
1112
import org.json.JSONObject;
@@ -33,7 +34,7 @@ private static InboxMessage responseToMessage(MessageResponse response) {
3334
try {
3435
customPayload = response.getCustomPayload() != null ? new JSONObject(response.getCustomPayload()) : null;
3536
} catch (JSONException e) {
36-
e.printStackTrace();
37+
MobileMessagingLogger.w("Cannot get Inbox response", e);
3738
}
3839

3940
final String internalData = response.getInternalData();
@@ -69,4 +70,16 @@ private static InboxMessage responseToMessage(MessageResponse response) {
6970
InternalDataMapper.updateMessageWithInternalData(message, internalData);
7071
return message;
7172
}
73+
74+
public static JSONObject toJSON(final Inbox inbox) {
75+
if (inbox == null) {
76+
return new JSONObject();
77+
}
78+
try {
79+
return new JSONObject(inbox.toString());
80+
} catch (Exception e) {
81+
MobileMessagingLogger.w("Cannot convert Inbox toJSON", e);
82+
return new JSONObject();
83+
}
84+
}
7285
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.infobip.mobile.messaging.inbox;
2+
3+
public class MobileInboxFilterOptionsAttrs {
4+
public static final String fromDateTime = "fromDateTime";
5+
public static final String toDateTime = "toDateTime";
6+
public static final String topic = "topic";
7+
public static final String limit = "limit";
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.infobip.mobile.messaging.inbox;
2+
3+
import org.infobip.mobile.messaging.util.DateTimeUtil;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
7+
import java.util.Date;
8+
9+
public class MobileInboxFilterOptionsJson {
10+
public static MobileInboxFilterOptions mobileInboxFilterOptionsFromJSON(JSONObject json) {
11+
if (json == null) {
12+
return null;
13+
}
14+
15+
try {
16+
Date fromDateTime = null, toDateTime = null;
17+
String topic = null;
18+
Integer limit = null;
19+
20+
if (json.has(MobileInboxFilterOptionsAttrs.fromDateTime) && !json.isNull(MobileInboxFilterOptionsAttrs.fromDateTime))
21+
fromDateTime = DateTimeUtil.ISO8601DateFromString(json.getString(MobileInboxFilterOptionsAttrs.fromDateTime));
22+
if (json.has(MobileInboxFilterOptionsAttrs.toDateTime) && !json.isNull(MobileInboxFilterOptionsAttrs.toDateTime))
23+
toDateTime = DateTimeUtil.ISO8601DateFromString(json.getString(MobileInboxFilterOptionsAttrs.toDateTime));
24+
if (json.has(MobileInboxFilterOptionsAttrs.topic) && !json.isNull(MobileInboxFilterOptionsAttrs.topic))
25+
topic = json.getString(MobileInboxFilterOptionsAttrs.topic);
26+
if (json.has(MobileInboxFilterOptionsAttrs.limit) && !json.isNull(MobileInboxFilterOptionsAttrs.limit))
27+
limit = json.getInt(MobileInboxFilterOptionsAttrs.limit);
28+
29+
return new MobileInboxFilterOptions(
30+
fromDateTime,
31+
toDateTime,
32+
topic,
33+
limit
34+
);
35+
} catch (JSONException e) {
36+
throw new RuntimeException(e);
37+
}
38+
}
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.infobip.mobile.messaging.plugins;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.infobip.mobile.messaging.CustomEvent;
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
import org.junit.Test;
10+
11+
import java.util.Objects;
12+
13+
public class CustomEventJsonTest {
14+
@Test
15+
public void fromJSON_should_be_ok() throws JSONException {
16+
String definitionId = "someDefinitionID";
17+
String someString = "SomeString";
18+
double someNumber = 12345.0;
19+
JSONObject jsonObject = new JSONObject("{\"definitionId\":\"" + definitionId + "\",\"properties\":{\"someString\":\"" + someString + "\",\"someNumber\":" + someNumber + ",\"someBool\":true,\"someDate\":\"2024-11-08\"}}");
20+
21+
CustomEvent customEvent = CustomEventJson.fromJSON(jsonObject);
22+
23+
assertEquals(definitionId, customEvent.getDefinitionId());
24+
assertTrue(Objects.requireNonNull(customEvent.getProperties().get("someBool")).booleanValue());
25+
assertEquals(someString, Objects.requireNonNull(customEvent.getProperties().get("someString")).stringValue());
26+
assertEquals(someNumber, Objects.requireNonNull(customEvent.getProperties().get("someNumber")).numberValue());
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.infobip.mobile.messaging.plugins;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import org.infobip.mobile.messaging.Installation;
9+
import org.json.JSONArray;
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
import org.junit.Test;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
public class InstallationJsonTest {
18+
19+
@Test
20+
public void array_toJSON_should_be_null_for_null() {
21+
assertNull(InstallationJson.toJSON((List<Installation>) null));
22+
}
23+
24+
@Test
25+
public void toJSON_should_be_valid() throws JSONException {
26+
Installation installation = installation();
27+
JSONObject json = InstallationJson.toJSON(installation);
28+
29+
assertEquals(18, json.length());
30+
assertEquals(installation.getAppVersion(), json.getString("appVersion"));
31+
assertTrue(json.has("applicationUserId"));
32+
assertEquals("null", json.getString("applicationUserId"));
33+
assertEquals(installation.getDeviceManufacturer(), json.getString("deviceManufacturer"));
34+
assertEquals(installation.getDeviceModel(), json.getString("deviceModel"));
35+
assertEquals(installation.getDeviceName(), json.getString("deviceName"));
36+
assertEquals(installation.getDeviceSecure(), json.get("deviceSecure"));
37+
assertEquals(installation.getDeviceTimezoneOffset(), json.getString("deviceTimezoneOffset"));
38+
assertEquals(installation.isPrimaryDevice(), json.getBoolean("isPrimaryDevice"));
39+
assertEquals(installation.isPushRegistrationEnabled(), json.getBoolean("isPushRegistrationEnabled"));
40+
assertEquals(installation.getLanguage(), json.getString("language"));
41+
assertEquals(installation.getNotificationsEnabled(), json.getBoolean("notificationsEnabled"));
42+
assertEquals(installation.getOs(), json.get("os"));
43+
assertEquals(installation.getOsVersion(), json.get("osVersion"));
44+
assertEquals(installation.getPushRegistrationId(), json.get("pushRegistrationId"));
45+
assertEquals(installation.getPushServiceToken(), json.get("pushServiceToken"));
46+
assertEquals(installation.getPushServiceType().toString(), json.get("pushServiceType"));
47+
assertEquals(installation.getSdkVersion(), json.get("sdkVersion"));
48+
assertTrue(json.has("customAttributes"));
49+
assertEquals("null", json.get("customAttributes").toString());
50+
}
51+
52+
@Test
53+
public void toJSON_with_list() throws JSONException {
54+
List<Installation> installations = new ArrayList<>();
55+
installations.add(installation());
56+
installations.add(installation());
57+
installations.add(installation());
58+
59+
JSONArray json = InstallationJson.toJSON(installations);
60+
61+
assertNotNull(json);
62+
assertEquals(3, json.length());
63+
assertEquals("somePushRegId", json.getJSONObject(0).get("pushRegistrationId"));
64+
}
65+
66+
@Test
67+
public void resolveInstallation_should_return_limited_installation() throws JSONException {
68+
JSONObject json = new JSONObject("{\"isPrimaryDevice\":false,\"isPushRegistrationEnabled\":true,\"customAttributes\":{}}");
69+
70+
Installation fromJson = InstallationJson.resolveInstallation(json);
71+
Installation installation = installation();
72+
73+
assertEquals(installation.isPushRegistrationEnabled(), fromJson.isPushRegistrationEnabled());
74+
assertEquals(installation.isPrimaryDevice(), fromJson.isPrimaryDevice());
75+
assertEquals(0, fromJson.getCustomAttributes().size());
76+
}
77+
78+
private Installation installation() {
79+
Installation installation = new Installation(
80+
"somePushRegId",
81+
true,
82+
true,
83+
"1.2.3",
84+
"1.0",
85+
"Android",
86+
"15",
87+
"infobip",
88+
"justStarting",
89+
true,
90+
"en",
91+
"GMT+01:00",
92+
null,
93+
"someDeviceName",
94+
false,
95+
Installation.PushServiceType.Firebase,
96+
"somepushservicetoken",
97+
null
98+
);
99+
installation.setCustomAttributes(null);
100+
101+
return installation;
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.infobip.mobile.messaging.plugins;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.infobip.mobile.messaging.Message;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
import org.junit.Test;
9+
10+
public class MessageJsonTest {
11+
private final static int MESSAGE_JSON_NUMBER_OF_PARAMS = 19;
12+
13+
@Test
14+
public void toJSON_should_be_expected_number_of_parameters() throws JSONException {
15+
Message message = message();
16+
17+
JSONObject json = MessageJson.toJSON(message);
18+
19+
assert json != null;
20+
assertEquals(message.getMessageId(), json.getString("messageId"));
21+
assertEquals(MESSAGE_JSON_NUMBER_OF_PARAMS, json.length());
22+
}
23+
24+
private Message message() {
25+
return new Message(
26+
"someID",
27+
"someTitle",
28+
"someBody",
29+
"someSound",
30+
true,
31+
"someIcon",
32+
false,
33+
"someCat",
34+
"someFrom",
35+
0,
36+
0,
37+
0,
38+
null,
39+
null,
40+
"someDestination",
41+
Message.Status.SUCCESS,
42+
"statusMessage",
43+
"http://www.infobip.com",
44+
Message.InAppStyle.BANNER,
45+
0,
46+
"some-web-view-url",
47+
"some-browser-url",
48+
"type",
49+
"deeplink",
50+
"openTitle",
51+
"dismissTitle"
52+
);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.infobip.mobile.messaging.plugins;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
import org.junit.Test;
10+
11+
public class PersonalizationCtxTest {
12+
13+
@Test
14+
public void should_resolve_default_params() throws JSONException {
15+
JSONObject json = new JSONObject("{\"userIdentity\":{\"externalUserId\":\"someId\"}}");
16+
17+
PersonalizationCtx personalizationCtx = PersonalizationCtx.resolvePersonalizationCtx(json);
18+
19+
assertFalse(personalizationCtx.forceDepersonalize);
20+
assertFalse(personalizationCtx.keepAsLead);
21+
assertEquals("someId", personalizationCtx.userIdentity.getExternalUserId());
22+
assertNull(personalizationCtx.userAttributes);
23+
assertNull(personalizationCtx.userIdentity.getPhones());
24+
assertNull(personalizationCtx.userIdentity.getEmails());
25+
}
26+
}

0 commit comments

Comments
 (0)