### Question:
MapUtils { public static Map<String, String> flattenMap(String rootKey, Map<String, Object> collection) { if (collection == null || collection.isEmpty()) { return Collections.emptyMap(); } Map<String, String> result = new HashMap<>(); for (Map.Entry<String, Object> entry : collection.entrySet()) { if (entry.getValue() == null) { result.put( mergeKey(rootKey, entry.getKey()), ""); continue; } executeAction(new Argument(entry, result, rootKey)); } return result; }  static Map<String, String> flattenMap(String rootKey, Map<String, Object> collection);  }### Answer:
@Test public void givenMap_whenFlattenedWithRootKey_shouldReturnMapKeysWithRootKey() { Map<String, Object> testMap = new HashMap<>(); testMap.put("key", null); Map<String, String> resultMap = MapUtils.flattenMap("apiml", testMap); assertThat(resultMap, hasEntry("apiml.key", "")); }
@Test public void givenMapWithNullValue_whenFlattened_shouldReturnValueEmptyString() { Map<String, Object> testMap = new HashMap<>(); testMap.put("key", null); Map<String, String> resultMap = MapUtils.flattenMap(null, testMap); assertThat(resultMap, hasEntry("key", "")); }
@Test public void givenMapWithPrimitiveValues_whenFlattened_shouldReturnCorrectValues() { Map<String, Object> testMap = new HashMap<>(); testMap.put("key1", true); testMap.put("key2", 23); testMap.put("key3", 23.0d); testMap.put("key4", 23.0f); Map<String, String> resultMap = MapUtils.flattenMap(null, testMap); assertThat(resultMap, hasEntry("key1", "true")); assertThat(resultMap, hasEntry("key2", "23")); assertThat(resultMap, hasEntry("key3", "23.0")); assertThat(resultMap, hasEntry("key4", "23.0")); }
@Test public void givenMapWithNestedMap_whenFlattened_shouldReturnFlattened() { Map<String, Object> nestedLvl2 = new HashMap<>(); nestedLvl2.put("keyzzz", "valuezzz"); Map<String, Object> nested = new HashMap<>(); nested.put("key1", "value1"); nested.put("key2", "value2"); nested.put("key3", nestedLvl2); Map<String, Object> testMap = new HashMap<>(); testMap.put("masterKey", nested); Map<String, String> resultMap = MapUtils.flattenMap(null, testMap); assertThat(resultMap, hasEntry("masterKey.key1", "value1")); assertThat(resultMap, hasEntry("masterKey.key2", "value2")); assertThat(resultMap, hasEntry("masterKey.key3.keyzzz", "valuezzz")); }
@Test(expected = IllegalArgumentException.class) public void givenMapWithNestedList_whenFlattened_shouldReturnException() { List<Object> nested = new ArrayList<>(); nested.add("value1"); nested.add("value2"); Map<String, Object> testMap = new HashMap<>(); testMap.put("masterKey", nested); MapUtils.flattenMap(null, testMap); }
@Test(expected = IllegalArgumentException.class) public void givenMapWithNestedArray_whenFlattened_shouldReturnException() { String[] nested = {"value1", "value2"}; Map<String, Object> testMap = new HashMap<>(); testMap.put("masterKey", nested); MapUtils.flattenMap(null, testMap); }
@Test(expected = IllegalArgumentException.class) public void givenMapWithAnythingElseThanExpected_whenFlattened_shouldReturnException() { Map<String, Object> testMap = new HashMap<>(); testMap.put("key1", new BigDecimal(0)); MapUtils.flattenMap(null, testMap); }