diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java index 89f9af8e1d529..0a5832539f3b6 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java @@ -88,6 +88,8 @@ static Map merge(final Map fields, final Map(), (Map) v, fn)); } else { newValues.add(v); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java index 4806412aeaa60..afc3d4dfb5582 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java @@ -27,9 +27,11 @@ import org.joda.time.DateTimeZone; import org.mockito.ArgumentCaptor; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.function.Function; @@ -311,4 +313,54 @@ public String render(TextTemplate textTemplate, Map model) { return textTemplate.getTemplate().toUpperCase(Locale.ROOT); } } + + public void testMerge() { + Map writeableMap = new HashMap<>(); + Map mergeNull = ExecutableJiraAction.merge(writeableMap, null, s -> s); + assertTrue(mergeNull.isEmpty()); + Map map = new HashMap<>(); + map.put("foo", "bar"); + map.put("list", Arrays.asList("test1", "test2")); + Map valueMap = new HashMap<>(); + valueMap.put("var", "abc"); + map.put("map", valueMap); + Map componentMap = new HashMap<>(); + componentMap.put("name", "value"); + List> list = new ArrayList<>(); + list.add(componentMap); + map.put("components", list); + Map result = ExecutableJiraAction.merge(writeableMap, map, s -> s.toUpperCase(Locale.ROOT)); + assertThat(result, hasEntry("FOO", "BAR")); + assertThat(result.get("LIST"), instanceOf(List.class)); + List mergedList = (List) result.get("LIST"); + assertEquals(2, mergedList.size()); + assertEquals("TEST1", mergedList.get(0)); + assertEquals("TEST2", mergedList.get(1)); + Map mergedMap = (Map) result.get("MAP"); + assertEquals(1, mergedMap.size()); + assertEquals("ABC", mergedMap.get("VAR")); + assertThat(result.get("COMPONENTS"), instanceOf(List.class)); + List> components = (List>) result.get("COMPONENTS"); + assertThat(components.get(0), hasEntry("NAME", "VALUE")); + + // test the fields is not overwritten + Map fields = new HashMap<>(); + fields.put("FOO", "bob"); + fields.put("LIST", Arrays.asList("test3")); + fields.put("MAP", new HashMap<>()); + fields.put("COMPONENTS", new ArrayList<>()); + + result = ExecutableJiraAction.merge(fields, map, s -> s.toUpperCase(Locale.ROOT)); + assertThat(result, hasEntry("FOO", "bob")); + assertThat(result.get("LIST"), instanceOf(List.class)); + mergedList = (List) result.get("LIST"); + assertEquals(1, mergedList.size()); + assertEquals("test3", mergedList.get(0)); + mergedMap = (Map) result.get("MAP"); + assertTrue(mergedMap.isEmpty()); + assertThat(result.get("COMPONENTS"), instanceOf(List.class)); + components = (List>) result.get("COMPONENTS"); + assertTrue(components.isEmpty()); + } + }