BaseTagsAction extends Action { @NonNull @Override public ActionResult perform(@NonNull ActionArguments arguments) { if (arguments.getValue().getString() != null) { Set<String> tags = new HashSet<>(); tags.add(String.valueOf(arguments.getValue().getString())); applyChannelTags(tags); } if (arguments.getValue().getList() != null) { Set<String> tags = new HashSet<>(); for (JsonValue tag : arguments.getValue().getList()) { if (tag.getString() != null) { tags.add(tag.getString()); } } applyChannelTags(tags); } if (arguments.getValue().getMap() != null) { Map<String, Set<String>> tagsMap = new HashMap<>(); for (Map.Entry<String, JsonValue> entry : arguments.getValue().getMap().opt(CHANNEL_KEY).optMap().getMap().entrySet()) { String group = entry.getKey(); Set<String> tags = new HashSet<>(); for (JsonValue jsonValue : entry.getValue().optList().getList()) { tags.add(jsonValue.getString()); } if (!UAStringUtil.isEmpty(group) && !tags.isEmpty()) { tagsMap.put(group, tags); } } if (!tagsMap.isEmpty()) { applyChannelTagGroups(tagsMap); } tagsMap = new HashMap<>(); for (Map.Entry<String, JsonValue> entry : arguments.getValue().getMap().opt(NAMED_USER_KEY).optMap().getMap().entrySet()) { String group = entry.getKey(); Set<String> tags = new HashSet<>(); for (JsonValue jsonValue : entry.getValue().optList().getList()) { tags.add(jsonValue.getString()); } if (!UAStringUtil.isEmpty(group) && !tags.isEmpty()) { tagsMap.put(group, tags); } } if (!tagsMap.isEmpty()) { applyNamedUserTagGroups(tagsMap); } Set<String> tags = new HashSet<>(); for (JsonValue tag : arguments.getValue().getMap().opt(DEVICE_KEY).optList()) { if (tag.getString() != null) { tags.add(tag.getString()); } } if (!tags.isEmpty()) { applyChannelTags(tags); } } return ActionResult.newEmptyResult(); }  @Override boolean acceptsArguments(@NonNull ActionArguments arguments); @NonNull @Override ActionResult perform(@NonNull ActionArguments arguments);  }
@Test public void testGetTags() throws Exception { ActionArguments singleTagArg = ActionTestUtils.createArgs(Action.SITUATION_PUSH_OPENED, "tag1"); action.perform(singleTagArg); ActionArguments collectionArgs = ActionTestUtils.createArgs(Action.SITUATION_PUSH_RECEIVED, Arrays.asList("tag1", "tag2", "tag3")); action.perform(collectionArgs); ActionArguments badArgs = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, 1); action.perform(badArgs); ActionArguments nullArgs = ActionTestUtils.createArgs(Action.SITUATION_WEB_VIEW_INVOCATION, null); action.perform(nullArgs); assertEquals(2, action.applyTags.size()); assertEquals(1, action.applyTags.get(0).size()); assertTrue(action.applyTags.get(0).contains("tag1")); assertEquals(3, action.applyTags.get(1).size()); assertTrue(action.applyTags.get(1).contains("tag1")); assertTrue(action.applyTags.get(1).contains("tag2")); assertTrue(action.applyTags.get(1).contains("tag3")); assertTrue(action.applyChannelTags.isEmpty()); assertTrue(action.applyNamedUserTags.isEmpty()); }
@Test public void testApplyTagGroups() { Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2", "tag3")); HashMap<String, JsonValue> tagGroup = new HashMap<>(); tagGroup.put("channel", JsonMap.newBuilder().put("group1", JsonValue.wrapOpt(tags)).build().toJsonValue()); tagGroup.put("named_user", JsonMap.newBuilder().put("group2", JsonValue.wrapOpt(tags)).build().toJsonValue()); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_PUSH_RECEIVED, tagGroup); action.perform(args); assertEquals(1, action.applyChannelTags.size()); assertEquals(tags, action.applyChannelTags.get("group1")); assertEquals(1, action.applyNamedUserTags.size()); assertEquals(tags, action.applyNamedUserTags.get("group2")); assertTrue(action.applyTags.isEmpty()); }
@Test public void testApplyTagsAndTagGroups() { Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2", "tag3")); HashMap<String, JsonValue> argument = new HashMap<>(); argument.put("channel", JsonMap.newBuilder().put("group1", JsonValue.wrapOpt(tags)).build().toJsonValue()); argument.put("named_user", JsonMap.newBuilder().put("group2", JsonValue.wrapOpt(tags)).build().toJsonValue()); argument.put("device", JsonValue.wrapOpt(Arrays.asList("device tag 1", "device tag 2"))); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_PUSH_RECEIVED, argument); action.perform(args); assertEquals(1, action.applyChannelTags.size()); assertEquals(tags, action.applyChannelTags.get("group1")); assertEquals(1, action.applyNamedUserTags.size()); assertEquals(tags, action.applyNamedUserTags.get("group2")); assertEquals(2, action.applyTags.get(0).size()); assertTrue(action.applyTags.get(0).contains("device tag 1")); assertTrue(action.applyTags.get(0).contains("device tag 2")); }