2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.dao.utils;
23 import com.google.common.collect.ImmutableMap;
24 import org.junit.jupiter.api.Test;
28 import java.util.function.Function;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.assertThatThrownBy;
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.assertNull;
34 import static org.junit.jupiter.api.Assertions.assertNotNull;
35 import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps;
37 public class MapUtilTest {
40 public void mergeMaps_whenBothMapsAreNull_returnEmptyMap() {
41 assertThat(mergeMaps(null, null)).isEmpty();
45 public void mergeMaps_whenFirstMapIsNull_returnSecondMap() {
46 ImmutableMap<String, String> second = ImmutableMap.of("a", "b", "c", "d");
47 assertThat(mergeMaps(null, second))
49 .containsAllEntriesOf(second);
53 public void mergeMaps_whenSecondMapsIsNull_returnFirstMap() {
54 ImmutableMap<String, String> first = ImmutableMap.of("a", "b", "c", "d");
55 assertThat(mergeMaps(first, null))
57 .containsAllEntriesOf(first);
61 public void mergeMaps_avoidDuplications_takeValFromFirstMap() {
62 ImmutableMap<String, String> first = ImmutableMap.of("key1", "val1", "key2", "val2");
63 ImmutableMap<String, String> second = ImmutableMap.of("key1", "val11", "key3", "val3");
64 assertThat(mergeMaps(first, second))
65 .containsEntry("key1", "val1")
66 .containsEntry("key2", "val2")
67 .containsEntry("key3", "val3");
70 public void testGet() throws Exception {
71 Map<String, ? extends Object> mapWildcard = null;
75 result = MapUtil.get(mapWildcard, path);
80 Map<String, Object> map = new HashMap<>();
81 map.put("mock1", "test");
83 result = MapUtil.get(mapWildcard, path);
86 Map<String, Integer> subMap = new HashMap<>();
87 subMap.put("mock2", 1);
88 Map<String, ? extends Object> subMapWildcard = subMap;
89 map.put("mock1", subMapWildcard);
91 result = MapUtil.get(mapWildcard, path);
92 assertEquals(1, result);
96 public void testFlattenMapValues() throws Exception {
97 assertNotNull(MapUtil.flattenMapValues(null));
99 Map<String, List<String>> map = new HashMap<>();
100 List<String> list1 = new LinkedList<>();
102 List<String> list2 = new LinkedList<>();
104 map.put("key1", list1);
105 map.put("key2", list2);
106 List<String> result = MapUtil.flattenMapValues(map);
107 assertEquals(2, result.size());
108 assertEquals("test1", result.get(0));
109 assertEquals("test2", result.get(1));
113 public void testStreamOfNullable() throws Exception {
114 assertEquals(0, MapUtil.streamOfNullable(null).count());
116 Collection collectionTest = new LinkedList<String>();
117 collectionTest.add("test");
118 assertEquals(1, MapUtil.streamOfNullable(collectionTest).count());
122 public void testGroupListBy() throws Exception {
123 Collection valuesToMap = new LinkedList<String>();
124 Function<String, String> groupingFunction = new Function<String, String>() {
127 public String apply(String t) {
131 Map<String, List<String>> result;
134 result = MapUtil.groupListBy(valuesToMap, groupingFunction);
138 public void testToMap() throws Exception {
139 Collection<String> valuesToMap = null;
140 Function<String, String> mappingFunction = null;
141 Map<String, String> result;
144 result = MapUtil.toMap(valuesToMap, mappingFunction);
148 public void testConvertMapKeys() throws Exception {
149 Map<String, List<String>> map = new HashMap<>();
150 Function<String, String> keyMappingFunction = new Function<String, String>() {
153 public String apply(String t) {
157 Map<String, List<String>> result;
160 result = MapUtil.convertMapKeys(map, keyMappingFunction);
164 public void testNewHashMap() throws Exception {
165 final String[] keys1 = new String[] { "mock" };
166 final String[] values1 = new String[] { "mock" };
167 Map<String, String> result;
170 result = MapUtil.newHashMap(keys1, values1);
173 final String[] keys2 = new String[] { "mock" };
174 final String[] values2 = null;
175 assertThatThrownBy(() -> MapUtil.newHashMap(keys2, values2))
176 .isInstanceOf(IllegalArgumentException.class);
179 final String[] keys3 = null;
180 final String[] values3 = null;
181 assertThatThrownBy(() -> MapUtil.newHashMap(keys3, values3))
182 .isInstanceOf(IllegalArgumentException.class);
185 final String[] values4 = new String[] { "mock" };
186 final String[] keys4 = null;
187 assertThatThrownBy(() -> MapUtil.newHashMap(keys4, values4))
188 .isInstanceOf(IllegalArgumentException.class);