LegacyInAppMessage { @Nullable public static LegacyInAppMessage fromPush(@NonNull PushMessage pushMessage) throws JsonException { if (!pushMessage.containsKey(PushMessage.EXTRA_IN_APP_MESSAGE)) { return null; } JsonValue jsonValue = JsonValue.parseString(pushMessage.getExtra(PushMessage.EXTRA_IN_APP_MESSAGE, "")); JsonMap displayJson = jsonValue.optMap().opt(DISPLAY_KEY).optMap(); JsonMap actionsJson = jsonValue.optMap().opt(ACTIONS_KEY).optMap(); if (!BANNER_TYPE.equals(displayJson.opt(TYPE_KEY).getString())) { throw new JsonException("Only banner types are supported."); } Builder builder = newBuilder(); builder.setExtras(jsonValue.optMap().opt(EXTRA_KEY).optMap()) .setAlert(displayJson.opt(ALERT_KEY).getString()); if (displayJson.containsKey(PRIMARY_COLOR_KEY)) { try { builder.setPrimaryColor(Color.parseColor(displayJson.opt(PRIMARY_COLOR_KEY).optString())); } catch (IllegalArgumentException e) { throw new JsonException("Invalid primary color: " + displayJson.opt(PRIMARY_COLOR_KEY), e); } } if (displayJson.containsKey(SECONDARY_COLOR_KEY)) { try { builder.setSecondaryColor(Color.parseColor(displayJson.opt(SECONDARY_COLOR_KEY).optString())); } catch (IllegalArgumentException e) { throw new JsonException("Invalid secondary color: " + displayJson.opt(SECONDARY_COLOR_KEY), e); } } if (displayJson.containsKey(DURATION_KEY)) { long duration = displayJson.opt(DURATION_KEY).getLong(0); builder.setDuration(TimeUnit.SECONDS.toMillis(duration)); } long defaultExpiry = System.currentTimeMillis() + DEFAULT_EXPIRY_MS; if (jsonValue.optMap().containsKey(EXPIRY_KEY)) { builder.setExpiry(DateUtils.parseIso8601(jsonValue.optMap().opt(EXPIRY_KEY).optString(), defaultExpiry)); } else { builder.setExpiry(defaultExpiry); } if (BannerDisplayContent.PLACEMENT_TOP.equalsIgnoreCase(displayJson.opt(POSITION_KEY).getString())) { builder.setPlacement(BannerDisplayContent.PLACEMENT_TOP); } else { builder.setPlacement(BannerDisplayContent.PLACEMENT_BOTTOM); } Map<String, JsonValue> clickActions = actionsJson.opt(ON_CLICK_KEY).optMap().getMap(); if (!UAStringUtil.isEmpty(pushMessage.getRichPushMessageId())) { clickActions.put(MESSAGE_CENTER_ACTION, JsonValue.wrap(pushMessage.getRichPushMessageId())); } builder.setClickActionValues(clickActions); builder.setButtonGroupId(actionsJson.opt(BUTTON_GROUP_KEY).getString()); JsonMap buttonActionsJson = actionsJson.opt(BUTTON_ACTIONS_KEY).optMap(); for (Map.Entry<String, JsonValue> entry : buttonActionsJson.entrySet()) { String buttonId = entry.getKey(); JsonMap actionJson = buttonActionsJson.opt(buttonId).optMap(); builder.setButtonActionValues(buttonId, actionJson.getMap()); } builder.setId(pushMessage.getSendId()); try { return builder.build(); } catch (IllegalArgumentException e) { throw new JsonException("Invalid legacy in-app message" + jsonValue, e); } } private  LegacyInAppMessage(@NonNull Builder builder); long getExpiry(); @NonNull JsonMap getExtras(); @Nullable String getAlert(); @NonNull Map<String, JsonValue> getClickActionValues(); @Nullable Map<String, JsonValue> getButtonActionValues(@NonNull String buttonId); @Nullable String getButtonGroupId(); @Nullable Long getDuration(); @NonNull @BannerDisplayContent.Placement String getPlacement(); @Nullable Integer getPrimaryColor(); @Nullable Integer getSecondaryColor(); @NonNull String getId(); @Nullable static LegacyInAppMessage fromPush(@NonNull PushMessage pushMessage); @NonNull static Builder newBuilder();  }
@Test public void testFromPush() throws JsonException { Bundle pushBundle = new Bundle(); pushBundle.putString(PushMessage.EXTRA_IN_APP_MESSAGE, VALID_JSON); PushMessage pushMessage = new PushMessage(pushBundle); LegacyInAppMessage message = LegacyInAppMessage.fromPush(pushMessage); assertEquals("Oh hi!", message.getAlert()); assertEquals(10000l, (long) message.getDuration()); assertEquals(1449921600000l, message.getExpiry()); assertEquals("ua_yes_no", message.getButtonGroupId()); assertEquals(BannerDisplayContent.PLACEMENT_BOTTOM, message.getPlacement()); assertEquals(Color.parseColor("#FF0000"), (int) message.getPrimaryColor()); assertEquals(Color.parseColor("#00FF00"), (int) message.getSecondaryColor()); JsonMap extras = message.getExtras(); assertEquals(2, extras.size()); assertEquals("Selleck", extras.get("Tom").getString()); assertEquals(123, extras.get("wat").getInt(0)); Map<String, JsonValue> onClickActionValues = message.getClickActionValues(); assertEquals(1, onClickActionValues.size()); assertEquals("someurl", onClickActionValues.get("^d").getString()); Map<String, JsonValue> yesActionValues = message.getButtonActionValues("yes"); assertEquals(1, yesActionValues.size()); assertEquals("yes_tag", yesActionValues.get("^+t").getString()); Map<String, JsonValue> noActionValues = message.getButtonActionValues("no"); assertEquals(1, noActionValues.size()); assertEquals("no_tag", noActionValues.get("^+t").getString()); }
@Test(expected = JsonException.class) public void testParseJsonDisplayNotBanner() throws JsonException { String invalidMessage = "{\"display\": {\"primary_color\": \"#FF0000\"," + "\"duration\": 10, \"secondary_color\": \"#00FF00\", \"type\": \"modal\"," + "\"alert\": \"Oh hi!\"}, \"actions\": {\"button_group\": \"ua_yes_no\"," + "\"button_actions\": {\"yes\": {\"^+t\": \"yes_tag\"}, \"no\": {\"^+t\": \"no_tag\"}}," + "\"on_click\": {\"^d\": \"someurl\"}}, \"expiry\": \"2015-12-12T12:00:00\", \"extra\":" + "{\"wat\": 123, \"Tom\": \"Selleck\"}}"; Bundle pushBundle = new Bundle(); pushBundle.putString(PushMessage.EXTRA_IN_APP_MESSAGE, invalidMessage); PushMessage pushMessage = new PushMessage(pushBundle); LegacyInAppMessage.fromPush(pushMessage); }
@Test(expected = JsonException.class) public void testParseJsonInvalidPayload() throws JsonException { Bundle pushBundle = new Bundle(); pushBundle.putString(PushMessage.EXTRA_IN_APP_MESSAGE, JsonValue.wrap("wat").toString()); PushMessage pushMessage = new PushMessage(pushBundle); LegacyInAppMessage.fromPush(pushMessage); }