re base code
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / dao / utils / MapUtilTest.java
1 package org.openecomp.sdc.be.dao.utils;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.junit.Test;
5
6 import java.util.*;
7 import java.util.function.Function;
8
9 import static org.assertj.core.api.Assertions.assertThat;
10 import static org.assertj.core.api.Assertions.assertThatThrownBy;
11 import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps;
12
13 public class MapUtilTest {
14
15     @Test
16     public void mergeMaps_whenBothMapsAreNull_returnEmptyMap() {
17         assertThat(mergeMaps(null, null)).isEmpty();
18     }
19
20     @Test
21     public void mergeMaps_whenFirstMapIsNull_returnSecondMap() {
22         ImmutableMap<String, String> second = ImmutableMap.of("a", "b", "c", "d");
23         assertThat(mergeMaps(null, second))
24                 .isNotSameAs(second)
25                 .containsAllEntriesOf(second);
26     }
27
28     @Test
29     public void mergeMaps_whenSecondMapsIsNull_returnFirstMap() {
30         ImmutableMap<String, String> first = ImmutableMap.of("a", "b", "c", "d");
31         assertThat(mergeMaps(first, null))
32                 .isNotSameAs(first)
33                 .containsAllEntriesOf(first);
34     }
35
36     @Test
37     public void mergeMaps_avoidDuplications_takeValFromFirstMap() {
38         ImmutableMap<String, String> first = ImmutableMap.of("key1", "val1", "key2", "val2");
39         ImmutableMap<String, String> second = ImmutableMap.of("key1", "val11", "key3", "val3");
40         assertThat(mergeMaps(first, second))
41                 .containsEntry("key1", "val1")
42                 .containsEntry("key2", "val2")
43                 .containsEntry("key3", "val3");
44      }
45         @Test
46         public void testGet() throws Exception {
47                 Map<String, ? extends Object> map = null;
48                 String path = "";
49                 Object result;
50
51                 // default test
52                 result = MapUtil.get(map, path);
53                 path = "\\mock\\mock";
54                 result = MapUtil.get(map, path);
55         }
56
57         @Test
58         public void testGroupListBy() throws Exception {
59                 Collection valuesToMap = new LinkedList<String>();
60                 Function<String, String> groupingFunction = new Function<String, String>() {
61                         
62                         @Override
63                         public String apply(String t) {
64                                 return t;
65                         }
66                 };
67                 Map<String, List<String>> result;
68
69                 // default test
70                 result = MapUtil.groupListBy(valuesToMap, groupingFunction);
71         }
72
73         @Test
74         public void testToMap() throws Exception {
75                 Collection<String> valuesToMap = null;
76                 Function<String, String> mappingFunction = null;
77                 Map<String, String> result;
78
79                 // default test
80                 result = MapUtil.toMap(valuesToMap, mappingFunction);
81         }
82
83         @Test
84         public void testConvertMapKeys() throws Exception {
85                 Map<String, List<String>> map = new HashMap<>();
86                 Function<String, String> keyMappingFunction = new Function<String, String>() {
87                         
88                         @Override
89                         public String apply(String t) {
90                                 return t;
91                         }
92                 };
93                 Map<String, List<String>> result;
94
95                 // default test
96                 result = MapUtil.convertMapKeys(map, keyMappingFunction);
97         }
98
99         @Test
100         public void testNewHashMap() throws Exception {
101         final String[] keys1 = new String[] { "mock" };
102         final String[] values1 = new String[] { "mock" };
103                 Map<String, String> result;
104
105                 // test 1
106                 result = MapUtil.newHashMap(keys1, values1);
107
108                 // test 2
109         final String[] keys2 = new String[] { "mock" };
110         final String[] values2 = null;
111         assertThatThrownBy(() -> MapUtil.newHashMap(keys2, values2))
112                     .isInstanceOf(IllegalArgumentException.class);
113
114                 // test 3
115         final String[] keys3 = null;
116         final String[] values3 = null;
117         assertThatThrownBy(() -> MapUtil.newHashMap(keys3, values3))
118                 .isInstanceOf(IllegalArgumentException.class);
119
120                 // test 4
121                 final String[] values4 = new String[] { "mock" };
122                 final String[] keys4 = null;
123                 assertThatThrownBy(() -> MapUtil.newHashMap(keys4, values4))
124                                         .isInstanceOf(IllegalArgumentException.class);
125
126         }
127 }