Sorted out unit-test libraries in onboarding
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / CommonMethodsTest.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.utilities;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.AbstractMap;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import org.apache.commons.lang3.ArrayUtils;
35 import org.junit.Test;
36
37 public class CommonMethodsTest {
38
39     private static final String[] ARRAY = {"A", "B", "C"};
40     private static final String JAVA_LANG_STRING = "java.lang.String";
41
42     @Test
43     public void testPrintStackTrace() {
44
45         String trace = CommonMethods.printStackTrace();
46         assertTrue(trace.contains("org.openecomp.core.utilities" +
47                 ".CommonMethods.printStackTrace(CommonMethods.java:"));
48         assertTrue(trace.contains("org.openecomp.core.utilities" +
49                 ".CommonMethodsTest.testPrintStackTrace(CommonMethodsTest.java"));
50     }
51
52     @Test
53     public void testArrayToCommaSeparatedString() {
54         assertEquals(CommonMethods.arrayToCommaSeparatedString(ARRAY), "A,B,C");
55     }
56
57     @Test
58     public void testArrayToCommaSeparatedStringEmpty() {
59         assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[0]), "");
60     }
61
62     @Test
63     public void testArrayToCommaSeparatedStringNulls() {
64         assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {null, null}), "null,null");
65     }
66
67     @Test
68     public void testArrayToCommaSeparatedStringEmptyStrings() {
69         assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {"", ""}), ",");
70     }
71
72     @Test(expected = NullPointerException.class)
73     public void testArrayToCommaSeparatedStringNull() {
74         CommonMethods.arrayToCommaSeparatedString(null);
75     }
76
77     @Test
78     public void testArrayToSeparatedString() {
79         assertEquals(CommonMethods.arrayToSeparatedString(ARRAY, '/'), "A/B/C");
80     }
81
82     @Test
83     public void testArrayToSeparatedStringEmpty() {
84         assertEquals(CommonMethods.arrayToSeparatedString(new String[0], '/'), "");
85     }
86
87     @Test
88     public void testArrayToSeparatedStringNulls() {
89         assertEquals(CommonMethods.arrayToSeparatedString(new String[] {null, null}, '/'), "null/null");
90     }
91
92     @Test
93     public void testArrayToSeparatedStringEmptyStrings() {
94         assertEquals(CommonMethods.arrayToSeparatedString(new String[] {"", ""}, '/'), "/");
95     }
96
97     @Test(expected = NullPointerException.class)
98     public void testArrayToSeparatedStringNull() {
99         CommonMethods.arrayToSeparatedString(null, '/');
100     }
101
102     @Test
103     public void testCollectionToCommaSeparatedString() {
104         assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(ARRAY)), "A,B,C");
105     }
106
107     @Test
108     public void testCollectionToCommaSeparatedStringNulls() {
109         assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(null, null)), "null,null");
110     }
111
112     @Test
113     public void testCollectionToCommaSeparatedStringEmptyStrings() {
114         assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList("", "")), ",");
115     }
116
117     @Test
118     public void testCollectionToCommaSeparatedStringEmtpy() {
119         assertEquals(CommonMethods.collectionToCommaSeparatedString(Collections.emptySet()), "");
120     }
121
122     @Test(expected = NullPointerException.class)
123     public void testCollectionToCommaSeparatedStringNull() {
124         assertNull(CommonMethods.collectionToCommaSeparatedString(null));
125     }
126
127     @Test
128     public void testNextUuId() {
129         assertNotNull(CommonMethods.nextUuId());
130     }
131
132     @Test
133     public void testConcatBothValuePresent() {
134         String []firstArray = {"1", "2"};
135         String []secondArray = {"3", "4"};
136
137         String []resultArray = CommonMethods.concat(firstArray, secondArray);
138
139         assertEquals(resultArray.length, 4);
140         assertTrue(ArrayUtils.contains(resultArray, secondArray[0])
141                 && ArrayUtils.contains(resultArray, firstArray[0]));
142     }
143
144     @Test
145     public void testConcatBothFirstValuePresent() {
146         String []firstArray = {"1", "2"};
147
148         String []resultArray = CommonMethods.concat(firstArray, null);
149
150         assertEquals(resultArray.length, 2);
151         assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(firstArray)));
152     }
153
154     @Test
155     public void testConcatBothSecondValuePresent() {
156         String []secondArray = {"3", "4"};
157
158         String []resultArray = CommonMethods.concat(null, secondArray);
159
160         assertEquals(resultArray.length, 2);
161         assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(secondArray)));
162     }
163
164     @Test
165     public void testConcatBothValueNull() {
166         assertNull(CommonMethods.concat(null, null));
167     }
168
169     @Test
170     public void testNewInstance() {
171         Object obj = CommonMethods.newInstance(JAVA_LANG_STRING);
172         assertNotNull(obj);
173         assertTrue(obj instanceof String);
174     }
175
176     @Test(expected = IllegalArgumentException.class)
177     public void testNewInstanceIncorrectClassProvided() {
178         assertNull(CommonMethods.newInstance("java.lang.Stringss"));
179     }
180
181     @Test(expected = IllegalArgumentException.class)
182     public void testNewInstanceClassNotProvided() {
183         assertNull(CommonMethods.newInstance(null, Object.class));
184     }
185
186     @Test(expected = IllegalArgumentException.class)
187     public void testNewInstanceObjectNotProvided() {
188         assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, null));
189     }
190
191     @Test(expected = ClassCastException.class)
192     public void testNewInstanceClassCastException() {
193         assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, ArrayList.class));
194     }
195
196     @Test(expected = RuntimeException.class)
197     public void testNewInstanceInvalidClassProvided() {
198         assertNull(CommonMethods.newInstance(List.class));
199     }
200
201     @Test
202     public void testListToSeparatedString() {
203         String str = "Concat,String";
204         String result = CommonMethods.listToSeparatedString(
205                 Stream.of("Concat", "String").collect(Collectors.toList()), ',');
206
207         assertNotNull(result);
208         assertEquals(str, result);
209     }
210
211     @Test
212     public void testDuplicateStringWithDelimiter() {
213         String duplicateStr = CommonMethods.duplicateStringWithDelimiter("Duplicate", '#', 4);
214
215         assertNotNull(duplicateStr);
216
217         String[] duplicateStrArray = duplicateStr.split("#");
218         assertTrue(duplicateStr.contains("Duplicate"));
219         assertEquals(duplicateStrArray.length, 4);
220     }
221
222     @Test
223     public void testRoSingleElement() {
224         Set<String> stringSet = CommonMethods.toSingleElementSet("Set Element");
225         assertNotNull(stringSet);
226         assertTrue(stringSet.contains("Set Element"));
227     }
228
229     @Test
230     public void testMergeListsOfMap() {
231         Map<String, String> map1 = new HashMap<>();
232         map1.put("Port1", "NeutronPort_CP_1");
233         map1.put("Port2", "NeutronPort_CP_2");
234
235         Map<String, String> map2 = new HashMap<>();
236         map2.put("Server1", "NovaServer_1");
237         map2.put("Server2", "NovaServer_2");
238
239         List<Map<String, String>> list1 = Stream.of(map1, map2).collect(Collectors.toList());
240
241         Map<String, String> map3 = new HashMap<>();
242         map3.put("Port3", "NeutronPort_CP_3");
243         map3.put("Port4", "NeutronPort_CP_4");
244
245         Map<String, String> map4 = new HashMap<>();
246         map4.put("Server3", "NovaServer_3");
247         map4.put("Server4", "NovaServer_4");
248         map4.put("Server2", "NovaServer_2");
249
250         List<Map<String, String>> list2 = Stream.of(map3, map4).collect(Collectors.toList());
251
252         List<Map<String, String>> resultList = CommonMethods.mergeListsOfMap(list1, list2);
253
254         assertEquals(resultList.size(), 6);
255
256         //Verify for duplicate key
257         int count = 0;
258         for(Map<String, String> map : resultList) {
259             if(map.containsKey("Server2"))
260                 count++;
261         }
262
263         assertEquals(1, count);
264     }
265
266     @Test
267     public void testMergeLists() {
268         List<String> list1 = Stream.of("First", "Second").collect(Collectors.toList());
269         List<String> list2 = Stream.of("Third", "Fourth").collect(Collectors.toList());
270
271         List<String> resultList = CommonMethods.mergeLists(list1, list2);
272
273         assertEquals(resultList.size(), 4);
274         assertTrue(resultList.containsAll(list1));
275         assertTrue(resultList.containsAll(list2));
276     }
277
278     @Test
279     public void testMergeMaps() {
280         Map<String, String> map1 = Stream.of(new AbstractMap.SimpleEntry<>("Port", "Neutron"),
281                                              new AbstractMap.SimpleEntry<>("Compute", "NOVA"))
282                                     .collect(Collectors.toMap(
283                                             AbstractMap.SimpleEntry::getKey,
284                                             AbstractMap.SimpleEntry::getValue));
285
286         Map<String, String> map2 = Stream.of(new AbstractMap.SimpleEntry<>("VLAN", "VMI"),
287                 new AbstractMap.SimpleEntry<>("Volume", "Cinder"),
288                 new AbstractMap.SimpleEntry<>("Port", "VMI"))
289                 .collect(Collectors.toMap(
290                         AbstractMap.SimpleEntry::getKey,
291                         AbstractMap.SimpleEntry::getValue));
292
293         Map<String, String> resultMap = CommonMethods.mergeMaps(map1, map2);
294
295         assertEquals(resultMap.size(), 4);
296         assertEquals(resultMap.get("Port"), "VMI");
297     }
298 }