Added oparent to sdc main
[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.Test;
25
26 import java.util.*;
27 import java.util.function.Function;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.assertThatThrownBy;
31 import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps;
32
33 public class MapUtilTest {
34
35     @Test
36     public void mergeMaps_whenBothMapsAreNull_returnEmptyMap() {
37         assertThat(mergeMaps(null, null)).isEmpty();
38     }
39
40     @Test
41     public void mergeMaps_whenFirstMapIsNull_returnSecondMap() {
42         ImmutableMap<String, String> second = ImmutableMap.of("a", "b", "c", "d");
43         assertThat(mergeMaps(null, second))
44                 .isNotSameAs(second)
45                 .containsAllEntriesOf(second);
46     }
47
48     @Test
49     public void mergeMaps_whenSecondMapsIsNull_returnFirstMap() {
50         ImmutableMap<String, String> first = ImmutableMap.of("a", "b", "c", "d");
51         assertThat(mergeMaps(first, null))
52                 .isNotSameAs(first)
53                 .containsAllEntriesOf(first);
54     }
55
56     @Test
57     public void mergeMaps_avoidDuplications_takeValFromFirstMap() {
58         ImmutableMap<String, String> first = ImmutableMap.of("key1", "val1", "key2", "val2");
59         ImmutableMap<String, String> second = ImmutableMap.of("key1", "val11", "key3", "val3");
60         assertThat(mergeMaps(first, second))
61                 .containsEntry("key1", "val1")
62                 .containsEntry("key2", "val2")
63                 .containsEntry("key3", "val3");
64      }
65         @Test
66         public void testGet() throws Exception {
67                 Map<String, ? extends Object> map = null;
68                 String path = "";
69                 Object result;
70
71                 // default test
72                 result = MapUtil.get(map, path);
73                 path = "\\mock\\mock";
74                 result = MapUtil.get(map, path);
75         }
76
77         @Test
78         public void testGroupListBy() throws Exception {
79                 Collection valuesToMap = new LinkedList<String>();
80                 Function<String, String> groupingFunction = new Function<String, String>() {
81                         
82                         @Override
83                         public String apply(String t) {
84                                 return t;
85                         }
86                 };
87                 Map<String, List<String>> result;
88
89                 // default test
90                 result = MapUtil.groupListBy(valuesToMap, groupingFunction);
91         }
92
93         @Test
94         public void testToMap() throws Exception {
95                 Collection<String> valuesToMap = null;
96                 Function<String, String> mappingFunction = null;
97                 Map<String, String> result;
98
99                 // default test
100                 result = MapUtil.toMap(valuesToMap, mappingFunction);
101         }
102
103         @Test
104         public void testConvertMapKeys() throws Exception {
105                 Map<String, List<String>> map = new HashMap<>();
106                 Function<String, String> keyMappingFunction = new Function<String, String>() {
107                         
108                         @Override
109                         public String apply(String t) {
110                                 return t;
111                         }
112                 };
113                 Map<String, List<String>> result;
114
115                 // default test
116                 result = MapUtil.convertMapKeys(map, keyMappingFunction);
117         }
118
119         @Test
120         public void testNewHashMap() throws Exception {
121         final String[] keys1 = new String[] { "mock" };
122         final String[] values1 = new String[] { "mock" };
123                 Map<String, String> result;
124
125                 // test 1
126                 result = MapUtil.newHashMap(keys1, values1);
127
128                 // test 2
129         final String[] keys2 = new String[] { "mock" };
130         final String[] values2 = null;
131         assertThatThrownBy(() -> MapUtil.newHashMap(keys2, values2))
132                     .isInstanceOf(IllegalArgumentException.class);
133
134                 // test 3
135         final String[] keys3 = null;
136         final String[] values3 = null;
137         assertThatThrownBy(() -> MapUtil.newHashMap(keys3, values3))
138                 .isInstanceOf(IllegalArgumentException.class);
139
140                 // test 4
141                 final String[] values4 = new String[] { "mock" };
142                 final String[] keys4 = null;
143                 assertThatThrownBy(() -> MapUtil.newHashMap(keys4, values4))
144                                         .isInstanceOf(IllegalArgumentException.class);
145
146         }
147 }