HeaderUtils { public static Map<String, String> flattenHeaders(Map<String, List<String>> headers) { return flattenHeaders(headers, headerName -> true); } private  HeaderUtils(); static Map<String, String> flattenHeaders(Map<String, List<String>> headers); static Map<String, String> flattenHeaders(Map<String, List<String>> headers,
			Predicate<String> headerNameFilter); static Map<String, List<String>> expandHeaders(Map<String, String> headers);  }
@Test public void flattenHeaders_NoHeadersGiven_ShouldExpandToEmptyMap() { Map<String, List<String>> listHeaders = new HashMap<>(); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders); assertEquals(ImmutableMap.of(), flattenedHeaders); }
@Test public void flattenHeaders_NullKeyGiven_ShouldFilterOutHeader() { Map<String, List<String>> listHeaders = new HashMap<>(); listHeaders.put("a_k", singletonList("a_v")); listHeaders.put(null, singletonList("b_v")); listHeaders.put("c_k", singletonList("c_v")); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders); assertEquals(ImmutableMap.of("a_k", "a_v", "c_k", "c_v"), flattenedHeaders); }
@Test public void flattenHeaders_SingleNullValueGiven_ShouldFilterOutHeader() { Map<String, List<String>> listHeaders = new HashMap<>(); listHeaders.put("a_k", singletonList("a_v")); listHeaders.put("b_k", null); listHeaders.put("c_k", singletonList("c_v")); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders); assertEquals(ImmutableMap.of("a_k", "a_v", "c_k", "c_v"), flattenedHeaders); }
@Test public void flattenHeaders_EmptyValueListGiven_ShouldFilterOutHeader() { Map<String, List<String>> listHeaders = new HashMap<>(); listHeaders.put("a_k", singletonList("a_v")); listHeaders.put("b_k", new ArrayList<>()); listHeaders.put("c_k", singletonList("c_v")); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders); assertEquals(ImmutableMap.of("a_k", "a_v", "c_k", "c_v"), flattenedHeaders); }
@Test public void flattenHeader_headerNameFilterGiven_ShouldFilterOutHeaders() { Map<String, List<String>> listHeaders = new HashMap<>(); listHeaders.put("a_k", singletonList("a_v")); listHeaders.put("b_k", singletonList("b_v")); listHeaders.put("c_k", singletonList("c_v")); listHeaders.put("d_k", singletonList("d_v")); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders, headerName -> !"b_k".equals(headerName) && !"c_k".equals(headerName)); assertEquals(ImmutableMap.of("a_k", "a_v", "d_k", "d_v"), flattenedHeaders); }
@Test public void flattenHeaders_AnyGiven_ShouldReturnImmutableMap() { assertThrows(UnsupportedOperationException.class, () -> flattenHeaders(new HashMap<>()).put("k", "v")); }
@Test public void flattenHeaders_SingleValueGiven_ShouldPassHeader() { Map<String, List<String>> listHeaders = new HashMap<>(); listHeaders.put("a_k", singletonList("a_v")); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders); assertEquals(ImmutableMap.of("a_k", "a_v"), flattenedHeaders); }
@Test public void flattenHeaders_MultipleValuesGiven_ShouldPassHeader() { Map<String, List<String>> listHeaders = new HashMap<>(); List<String> headerValues = new ArrayList<>(); headerValues.add("a_v0"); headerValues.add("a_v1"); listHeaders.put("a_k", headerValues); Map<String, String> flattenedHeaders = flattenHeaders(listHeaders); assertEquals(ImmutableMap.of("a_k", "a_v0,a_v1"), flattenedHeaders); }