ChannelRegistrationPayload implements JsonSerializable { @NonNull @Override public JsonValue toJsonValue() { JsonMap.Builder channel = JsonMap.newBuilder() .put(DEVICE_TYPE_KEY, deviceType) .put(SET_TAGS_KEY, setTags) .put(OPT_IN_KEY, optIn) .put(PUSH_ADDRESS_KEY, pushAddress) .put(BACKGROUND_ENABLED_KEY, backgroundEnabled) .put(TIMEZONE_KEY, timezone) .put(LANGUAGE_KEY, language) .put(COUNTRY_KEY, country) .put(APP_VERSION_KEY, appVersion) .put(SDK_VERSION_KEY, sdkVersion) .put(DEVICE_MODEL_KEY, deviceModel) .put(CARRIER_KEY, carrier) .put(NAMED_USER_ID_KEY, namedUserId); if (ANDROID_DEVICE_TYPE.equals(deviceType) && deliveryType != null) { channel.put(ANDROID_EXTRAS_KEY, JsonMap.newBuilder() .put(ANDROID_DELIVERY_TYPE, deliveryType) .build()); } if (locationSettings != null) { channel.put(LOCATION_SETTINGS_KEY, locationSettings); } if (apiVersion != null) { channel.put(API_VERSION_KEY, apiVersion); } if (setTags && tags != null) { channel.put(TAGS_KEY, JsonValue.wrapOpt(tags).getList()); } JsonMap.Builder identityHints = JsonMap.newBuilder() .put(USER_ID_KEY, userId) .put(APID_KEY, apid) .put(ACCENGAGE_DEVICE_ID, accengageDeviceId); JsonMap.Builder data = JsonMap.newBuilder() .put(CHANNEL_KEY, channel.build()); JsonMap identityHintsMap = identityHints.build(); if (!identityHintsMap.isEmpty()) { data.put(IDENTITY_HINTS_KEY, identityHintsMap); } return data.build().toJsonValue(); } private  ChannelRegistrationPayload(Builder builder); @NonNull ChannelRegistrationPayload minimizedPayload(@Nullable ChannelRegistrationPayload last); @NonNull @Override JsonValue toJsonValue(); @NonNull @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); @NonNull
    static final String ANDROID_DEVICE_TYPE; @NonNull
    static final String AMAZON_DEVICE_TYPE; final boolean optIn; final boolean backgroundEnabled; final String deviceType; final String pushAddress; final boolean setTags; final Set<String> tags; final String userId; final String apid; final String timezone; final String language; final String country; final Boolean locationSettings; final String appVersion; final String sdkVersion; final String deviceModel; final Integer apiVersion; final String carrier; final String accengageDeviceId; final String deliveryType; final String namedUserId; }
@Test public void testAsJsonFullPayloadAnalyticsEnabled() throws JSONException { payload = new ChannelRegistrationPayload.Builder() .setOptIn(testOptIn) .setBackgroundEnabled(testBackgroundEnabled) .setDeviceType(testDeviceType) .setPushAddress(testPushAddress) .setTags(testSetTags, testTags) .setUserId(testUserId) .setApid(testApid) .setAccengageDeviceId(testAccengageDeviceId) .setLanguage(testLanguage) .setTimezone(testTimezone) .setCountry(testCountry) .setLocationSettings(true) .setAppVersion("123") .setApiVersion(123) .setSdkVersion("1.2.3") .setDeviceModel("Device model") .setCarrier("Carrier") .build(); JsonMap body = payload.toJsonValue().getMap(); assertTrue("Channel should be present in payload.", body.containsKey(ChannelRegistrationPayload.CHANNEL_KEY)); assertTrue("Identity hints should be present in payload.", body.containsKey(ChannelRegistrationPayload.IDENTITY_HINTS_KEY)); JsonMap identityHints = body.opt(ChannelRegistrationPayload.IDENTITY_HINTS_KEY).getMap(); JsonMap channel = body.opt(ChannelRegistrationPayload.CHANNEL_KEY).getMap(); assertTrue("User ID should be present in payload.", identityHints.containsKey(ChannelRegistrationPayload.USER_ID_KEY)); assertEquals("User ID should be fakeUserId.", identityHints.get(ChannelRegistrationPayload.USER_ID_KEY).getString(), testUserId); assertTrue("APID should be present in payload.", identityHints.containsKey(ChannelRegistrationPayload.APID_KEY)); assertEquals("APID should match.", identityHints.get(ChannelRegistrationPayload.APID_KEY).getString(), testApid); assertTrue("Accengage Device ID should be present in payload.", identityHints.containsKey(ChannelRegistrationPayload.ACCENGAGE_DEVICE_ID)); assertEquals("Accengage Device ID should match.", identityHints.get(ChannelRegistrationPayload.ACCENGAGE_DEVICE_ID).getString(), testAccengageDeviceId); assertTrue("Device type should be present in payload.", channel.containsKey(ChannelRegistrationPayload.DEVICE_TYPE_KEY)); assertEquals("Device type should be android.", channel.get(ChannelRegistrationPayload.DEVICE_TYPE_KEY).getString(), testDeviceType); assertTrue("Opt in should be present in payload.", channel.containsKey(ChannelRegistrationPayload.OPT_IN_KEY)); assertEquals("Opt in should be true.", channel.get(ChannelRegistrationPayload.OPT_IN_KEY).getBoolean(!testOptIn), testOptIn); assertTrue("Background flag should be present in payload.", channel.containsKey(ChannelRegistrationPayload.BACKGROUND_ENABLED_KEY)); assertEquals("Background flag should be true.", channel.get(ChannelRegistrationPayload.BACKGROUND_ENABLED_KEY).getBoolean(!testBackgroundEnabled), testBackgroundEnabled); assertTrue("Push address should be present in payload.", channel.containsKey(ChannelRegistrationPayload.PUSH_ADDRESS_KEY)); assertEquals("Push address should be gcmRegistrationId.", channel.get(ChannelRegistrationPayload.PUSH_ADDRESS_KEY).getString(), testPushAddress); assertTrue("Set tags should be present in payload", channel.containsKey(ChannelRegistrationPayload.SET_TAGS_KEY)); assertEquals("Set tags should be true.", channel.get(ChannelRegistrationPayload.SET_TAGS_KEY).getBoolean(!testSetTags), testSetTags); assertTrue("Tags should be present in payload", channel.containsKey(ChannelRegistrationPayload.TAGS_KEY)); assertTrue("Timezone should be in payload", channel.containsKey(ChannelRegistrationPayload.TIMEZONE_KEY)); assertTrue("Language should be in payload", channel.containsKey(ChannelRegistrationPayload.LANGUAGE_KEY)); assertTrue("Country should be in payload", channel.containsKey(ChannelRegistrationPayload.COUNTRY_KEY)); JsonList tags = channel.get(ChannelRegistrationPayload.TAGS_KEY).getList(); assertEquals("Tags size should be 2.", tags.size(), testTags.size()); assertTrue("Tags should contain tagOne.", testTags.contains(tags.get(0).getString())); assertTrue("Tags should contain tagTwo.", testTags.contains(tags.get(1).getString())); assertTrue(channel.containsKey(ChannelRegistrationPayload.LOCATION_SETTINGS_KEY)); assertEquals(channel.get(ChannelRegistrationPayload.LOCATION_SETTINGS_KEY).getBoolean(false), true); assertTrue(channel.containsKey(ChannelRegistrationPayload.APP_VERSION_KEY)); assertEquals(channel.get(ChannelRegistrationPayload.APP_VERSION_KEY).getString(), "123"); assertTrue(channel.containsKey(ChannelRegistrationPayload.API_VERSION_KEY)); assertEquals(channel.get(ChannelRegistrationPayload.API_VERSION_KEY).getInt(234), 123); assertTrue(channel.containsKey(ChannelRegistrationPayload.SDK_VERSION_KEY)); assertEquals(channel.get(ChannelRegistrationPayload.SDK_VERSION_KEY).getString(), "1.2.3"); assertTrue(channel.containsKey(ChannelRegistrationPayload.DEVICE_MODEL_KEY)); assertEquals(channel.get(ChannelRegistrationPayload.DEVICE_MODEL_KEY).getString(), "Device model"); assertTrue(channel.containsKey(ChannelRegistrationPayload.CARRIER_KEY)); assertEquals(channel.get(ChannelRegistrationPayload.CARRIER_KEY).getString(), "Carrier"); }
@Test public void testAsJsonEmptyTags() throws JSONException { ChannelRegistrationPayload payload = new ChannelRegistrationPayload.Builder().setTags(testSetTags, new HashSet<String>()).build(); JsonMap channel = payload.toJsonValue().getMap().opt(ChannelRegistrationPayload.CHANNEL_KEY).getMap(); assertTrue("Set tags should be present in payload.", channel.containsKey(ChannelRegistrationPayload.SET_TAGS_KEY)); assertEquals("Set tags should be true.", channel.get(ChannelRegistrationPayload.SET_TAGS_KEY).getBoolean(!testSetTags), testSetTags); assertTrue("Tags should be present in payload.", channel.containsKey(ChannelRegistrationPayload.TAGS_KEY)); JsonList tags = channel.opt(ChannelRegistrationPayload.TAGS_KEY).getList(); Assert.assertTrue("Tags size should be 0.", tags.isEmpty()); }
@Test public void testAsJsonNoTags() throws JSONException { ChannelRegistrationPayload payload = new ChannelRegistrationPayload.Builder().setTags(false, testTags).build(); JsonMap channel = payload.toJsonValue().getMap().opt(ChannelRegistrationPayload.CHANNEL_KEY).getMap(); assertTrue("Set tags should be present in payload.", channel.containsKey(ChannelRegistrationPayload.SET_TAGS_KEY)); assertEquals("Set tags should be false.", channel.opt(ChannelRegistrationPayload.SET_TAGS_KEY).getBoolean(true), false); assertFalse("Tags should not be present in payload.", channel.containsKey(ChannelRegistrationPayload.TAGS_KEY)); }
@Test public void testAsJsonEmptyIdentityHints() throws JSONException { ChannelRegistrationPayload payload = new ChannelRegistrationPayload.Builder().build(); JsonMap body = payload.toJsonValue().getMap(); assertFalse("Identity hints should not be present in payload.", body.containsKey(ChannelRegistrationPayload.IDENTITY_HINTS_KEY)); }
@Test public void testAsJsonEmptyUserId() throws JSONException { ChannelRegistrationPayload payload = new ChannelRegistrationPayload.Builder().setUserId("").build(); JsonMap body = payload.toJsonValue().getMap(); assertFalse("Identity hints should not be present in payload.", body.containsKey(ChannelRegistrationPayload.IDENTITY_HINTS_KEY)); }
@Test public void testEmptyBuilder() throws JSONException { ChannelRegistrationPayload payload = new ChannelRegistrationPayload.Builder().build(); JsonMap body = payload.toJsonValue().getMap(); assertTrue("Channel should be present in payload.", body.containsKey(ChannelRegistrationPayload.CHANNEL_KEY)); assertFalse("Identity hints should not be present in payload.", body.containsKey(ChannelRegistrationPayload.IDENTITY_HINTS_KEY)); JsonMap channel = body.get(ChannelRegistrationPayload.CHANNEL_KEY).getMap(); assertTrue("Opt in should be present in payload.", channel.containsKey(ChannelRegistrationPayload.OPT_IN_KEY)); assertEquals("Opt in should be false.", channel.get(ChannelRegistrationPayload.OPT_IN_KEY).getBoolean(true), false); assertTrue("Background flag should be present in payload.", channel.containsKey(ChannelRegistrationPayload.BACKGROUND_ENABLED_KEY)); assertEquals("Background flag should be false.", channel.get(ChannelRegistrationPayload.BACKGROUND_ENABLED_KEY).getBoolean(true), false); assertFalse("Push address should not be present in payload.", channel.containsKey(ChannelRegistrationPayload.PUSH_ADDRESS_KEY)); assertTrue("Set tags should be present in payload.", channel.containsKey(ChannelRegistrationPayload.SET_TAGS_KEY)); assertEquals("Set tags should be false.", channel.get(ChannelRegistrationPayload.SET_TAGS_KEY).getBoolean(true), false); assertFalse("Tags should not be present in payload.", channel.containsKey(ChannelRegistrationPayload.TAGS_KEY)); }
@Test public void testDeliveryTypeAndroid() throws JsonException { payload = new ChannelRegistrationPayload.Builder() .setDeviceType(ChannelRegistrationPayload.ANDROID_DEVICE_TYPE) .setDeliveryType(PushProvider.HMS_DELIVERY_TYPE) .build(); JsonValue expected = JsonMap.newBuilder() .put("channel", JsonMap.newBuilder() .put("set_tags", false) .put("device_type", "android") .put("opt_in", false) .put("background", false) .put("android", JsonMap.newBuilder() .put("delivery_type", "hms") .build()) .build()) .build() .toJsonValue(); assertEquals(expected, payload.toJsonValue()); }
@Test public void testDeliveryTypeAmazon() { payload = new ChannelRegistrationPayload.Builder() .setDeviceType(ChannelRegistrationPayload.AMAZON_DEVICE_TYPE) .setDeliveryType(PushProvider.ADM_DELIVERY_TYPE) .build(); JsonValue expected = JsonMap.newBuilder() .put("channel", JsonMap.newBuilder() .put("set_tags", false) .put("device_type", "amazon") .put("opt_in", false) .put("background", false) .build()) .build() .toJsonValue(); assertEquals(expected, payload.toJsonValue()); }