|
| 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 | +} |
0 commit comments