Release version 1.13.7
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / dao / utils / MapUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.utils;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.junit.jupiter.api.Test;
25
26
27 import java.util.*;
28 import java.util.function.Function;
29
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;
36
37 public class MapUtilTest {
38
39     @Test
40     public void mergeMaps_whenBothMapsAreNull_returnEmptyMap() {
41         assertThat(mergeMaps(null, null)).isEmpty();
42     }
43
44     @Test
45     public void mergeMaps_whenFirstMapIsNull_returnSecondMap() {
46         ImmutableMap<String, String> second = ImmutableMap.of("a", "b", "c", "d");
47         assertThat(mergeMaps(null, second))
48                 .isNotSameAs(second)
49                 .containsAllEntriesOf(second);
50     }
51
52     @Test
53     public void mergeMaps_whenSecondMapsIsNull_returnFirstMap() {
54         ImmutableMap<String, String> first = ImmutableMap.of("a", "b", "c", "d");
55         assertThat(mergeMaps(first, null))
56                 .isNotSameAs(first)
57                 .containsAllEntriesOf(first);
58     }
59
60     @Test
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");
68      }
69         @Test
70         public void testGet() throws Exception {
71                 Map<String, ? extends Object> mapWildcard = null;
72                 String path = "";
73                 Object result;
74
75                 result = MapUtil.get(mapWildcard, path);
76                 assertNull(result);
77
78                 path = "mock1.mock2";
79
80                 Map<String, Object> map = new HashMap<>();
81                 map.put("mock1", "test");
82                 mapWildcard = map;
83                 result = MapUtil.get(mapWildcard, path);
84                 assertNull(result);
85
86                 Map<String, Integer> subMap = new HashMap<>();
87                 subMap.put("mock2", 1);
88                 Map<String, ? extends Object> subMapWildcard = subMap;
89                 map.put("mock1", subMapWildcard);
90                 mapWildcard = map;
91                 result = MapUtil.get(mapWildcard, path);
92                 assertEquals(1, result);
93         }
94
95         @Test
96         public void testFlattenMapValues() throws Exception {
97                 assertNotNull(MapUtil.flattenMapValues(null));
98
99                 Map<String, List<String>> map = new HashMap<>();
100                 List<String> list1 = new LinkedList<>();
101                 list1.add("test1");
102                 List<String> list2 = new LinkedList<>();
103                 list2.add("test2");
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));
110         }
111
112         @Test
113         public void testStreamOfNullable() throws Exception {
114                 assertEquals(0, MapUtil.streamOfNullable(null).count());
115
116                 Collection collectionTest = new LinkedList<String>();
117                 collectionTest.add("test");
118                 assertEquals(1, MapUtil.streamOfNullable(collectionTest).count());
119         }
120
121         @Test
122         public void testGroupListBy() throws Exception {
123                 Collection valuesToMap = new LinkedList<String>();
124                 Function<String, String> groupingFunction = new Function<String, String>() {
125                         
126                         @Override
127                         public String apply(String t) {
128                                 return t;
129                         }
130                 };
131                 Map<String, List<String>> result;
132
133                 // default test
134                 result = MapUtil.groupListBy(valuesToMap, groupingFunction);
135         }
136
137         @Test
138         public void testToMap() throws Exception {
139                 Collection<String> valuesToMap = null;
140                 Function<String, String> mappingFunction = null;
141                 Map<String, String> result;
142
143                 // default test
144                 result = MapUtil.toMap(valuesToMap, mappingFunction);
145         }
146
147         @Test
148         public void testConvertMapKeys() throws Exception {
149                 Map<String, List<String>> map = new HashMap<>();
150                 Function<String, String> keyMappingFunction = new Function<String, String>() {
151                         
152                         @Override
153                         public String apply(String t) {
154                                 return t;
155                         }
156                 };
157                 Map<String, List<String>> result;
158
159                 // default test
160                 result = MapUtil.convertMapKeys(map, keyMappingFunction);
161         }
162
163         @Test
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;
168
169                 // test 1
170                 result = MapUtil.newHashMap(keys1, values1);
171
172                 // test 2
173         final String[] keys2 = new String[] { "mock" };
174         final String[] values2 = null;
175         assertThatThrownBy(() -> MapUtil.newHashMap(keys2, values2))
176                     .isInstanceOf(IllegalArgumentException.class);
177
178                 // test 3
179         final String[] keys3 = null;
180         final String[] values3 = null;
181         assertThatThrownBy(() -> MapUtil.newHashMap(keys3, values3))
182                 .isInstanceOf(IllegalArgumentException.class);
183
184                 // test 4
185                 final String[] values4 = new String[] { "mock" };
186                 final String[] keys4 = null;
187                 assertThatThrownBy(() -> MapUtil.newHashMap(keys4, values4))
188                                         .isInstanceOf(IllegalArgumentException.class);
189
190         }
191 }