c6d87def2b44024d6a75029cebb648ddab19bc29
[sdc.git] /
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.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertNotNull;
21 import static org.testng.Assert.assertNull;
22 import static org.testng.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
35 import org.apache.commons.lang3.ArrayUtils;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38
39 public class CommonMethodsTest {
40
41     private static final String[] ARRAY = {"A", "B", "C"};
42     private static final String JAVA_LANG_STRING = "java.lang.String";
43
44     @Test
45     public void testPrintStackTrace() {
46
47         String trace = CommonMethods.printStackTrace();
48         assertTrue(trace.contains("org.openecomp.core.utilities" +
49                 ".CommonMethods.printStackTrace(CommonMethods.java:"));
50         assertTrue(trace.contains("org.openecomp.core.utilities" +
51                 ".CommonMethodsTest.testPrintStackTrace(CommonMethodsTest.java"));
52     }
53
54     @Test
55     public void testArrayToCommaSeparatedString() {
56         assertEquals(CommonMethods.arrayToCommaSeparatedString(ARRAY), "A,B,C");
57     }
58
59     @Test
60     public void testArrayToCommaSeparatedStringEmpty() {
61         assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[0]), "");
62     }
63
64     @Test
65     public void testArrayToCommaSeparatedStringNulls() {
66         assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {null, null}), "null,null");
67     }
68
69     @Test
70     public void testArrayToCommaSeparatedStringEmptyStrings() {
71         assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {"", ""}), ",");
72     }
73
74     @Test(expectedExceptions = NullPointerException.class)
75     public void testArrayToCommaSeparatedStringNull() {
76         CommonMethods.arrayToCommaSeparatedString(null);
77     }
78
79     @Test
80     public void testArrayToSeparatedString() {
81         assertEquals(CommonMethods.arrayToSeparatedString(ARRAY, '/'), "A/B/C");
82     }
83
84     @Test
85     public void testArrayToSeparatedStringEmpty() {
86         assertEquals(CommonMethods.arrayToSeparatedString(new String[0], '/'), "");
87     }
88
89     @Test
90     public void testArrayToSeparatedStringNulls() {
91         assertEquals(CommonMethods.arrayToSeparatedString(new String[] {null, null}, '/'), "null/null");
92     }
93
94     @Test
95     public void testArrayToSeparatedStringEmptyStrings() {
96         assertEquals(CommonMethods.arrayToSeparatedString(new String[] {"", ""}, '/'), "/");
97     }
98
99     @Test(expectedExceptions = NullPointerException.class)
100     public void testArrayToSeparatedStringNull() {
101         CommonMethods.arrayToSeparatedString(null, '/');
102     }
103
104     @Test
105     public void testCollectionToCommaSeparatedString() {
106         assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(ARRAY)), "A,B,C");
107     }
108
109     @Test
110     public void testCollectionToCommaSeparatedStringNulls() {
111         assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(null, null)), "null,null");
112     }
113
114     @Test
115     public void testCollectionToCommaSeparatedStringEmptyStrings() {
116         assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList("", "")), ",");
117     }
118
119     @Test
120     public void testCollectionToCommaSeparatedStringEmtpy() {
121         assertEquals(CommonMethods.collectionToCommaSeparatedString(Collections.emptySet()), "");
122     }
123
124     @Test(expectedExceptions = NullPointerException.class)
125     public void testCollectionToCommaSeparatedStringNull() {
126         assertNull(CommonMethods.collectionToCommaSeparatedString(null));
127     }
128
129     @Test
130     public void testNextUuId() {
131         assertNotNull(CommonMethods.nextUuId());
132     }
133
134     @Test
135     public void testConcatBothValuePresent() {
136         String []firstArray = {"1", "2"};
137         String []secondArray = {"3", "4"};
138
139         String []resultArray = CommonMethods.concat(firstArray, secondArray);
140
141         Assert.assertEquals(resultArray.length, 4);
142         Assert.assertTrue(ArrayUtils.contains(resultArray, secondArray[0])
143                 && ArrayUtils.contains(resultArray, firstArray[0]));
144     }
145
146     @Test
147     public void testConcatBothFirstValuePresent() {
148         String []firstArray = {"1", "2"};
149
150         String []resultArray = CommonMethods.concat(firstArray, null);
151
152         Assert.assertEquals(resultArray.length, 2);
153         Assert.assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(firstArray)));
154     }
155
156     @Test
157     public void testConcatBothSecondValuePresent() {
158         String []secondArray = {"3", "4"};
159
160         String []resultArray = CommonMethods.concat(null, secondArray);
161
162         Assert.assertEquals(resultArray.length, 2);
163         Assert.assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(secondArray)));
164     }
165
166     @Test
167     public void testConcatBothValueNull() {
168         Assert.assertNull(CommonMethods.concat(null, null));
169     }
170
171     @Test
172     public void testNewInstance() {
173         Object obj = CommonMethods.newInstance(JAVA_LANG_STRING);
174         Assert.assertNotNull(obj);
175         Assert.assertTrue(obj instanceof String);
176     }
177
178     @Test(expectedExceptions = IllegalArgumentException.class)
179     public void testNewInstanceIncorrectClassProvided() {
180         Assert.assertNull(CommonMethods.newInstance("java.lang.Stringss"));
181     }
182
183     @Test(expectedExceptions = IllegalArgumentException.class)
184     public void testNewInstanceClassNotProvided() {
185         Assert.assertNull(CommonMethods.newInstance(null, Object.class));
186     }
187
188     @Test(expectedExceptions = IllegalArgumentException.class)
189     public void testNewInstanceObjectNotProvided() {
190         Assert.assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, null));
191     }
192
193     @Test(expectedExceptions = ClassCastException.class)
194     public void testNewInstanceClassCastException() {
195         Assert.assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, ArrayList.class));
196     }
197
198     @Test(expectedExceptions = RuntimeException.class)
199     public void testNewInstanceInvalidClassProvided() {
200         Assert.assertNull(CommonMethods.newInstance(List.class));
201     }
202
203     @Test
204     public void testListToSeparatedString() {
205         String str = "Concat,String";
206         String result = CommonMethods.listToSeparatedString(
207                 Stream.of("Concat", "String").collect(Collectors.toList()), ',');
208
209         Assert.assertNotNull(result);
210         Assert.assertEquals(str, result);
211     }
212
213     @Test
214     public void testDuplicateStringWithDelimiter() {
215         String duplicateStr = CommonMethods.duplicateStringWithDelimiter("Duplicate", '#', 4);
216
217         Assert.assertNotNull(duplicateStr);
218
219         String[] duplicateStrArray = duplicateStr.split("#");
220         Assert.assertTrue(duplicateStr.contains("Duplicate"));
221         Assert.assertEquals(duplicateStrArray.length, 4);
222     }
223
224     @Test
225     public void testRoSingleElement() {
226         Set<String> stringSet = CommonMethods.toSingleElementSet("Set Element");
227         Assert.assertNotNull(stringSet);
228         Assert.assertTrue(stringSet.contains("Set Element"));
229     }
230
231     @Test
232     public void testMergeListsOfMap() {
233         Map<String, String> map1 = new HashMap<>();
234         map1.put("Port1", "NeutronPort_CP_1");
235         map1.put("Port2", "NeutronPort_CP_2");
236
237         Map<String, String> map2 = new HashMap<>();
238         map2.put("Server1", "NovaServer_1");
239         map2.put("Server2", "NovaServer_2");
240
241         List<Map<String, String>> list1 = Stream.of(map1, map2).collect(Collectors.toList());
242
243         Map<String, String> map3 = new HashMap<>();
244         map3.put("Port3", "NeutronPort_CP_3");
245         map3.put("Port4", "NeutronPort_CP_4");
246
247         Map<String, String> map4 = new HashMap<>();
248         map4.put("Server3", "NovaServer_3");
249         map4.put("Server4", "NovaServer_4");
250         map4.put("Server2", "NovaServer_2");
251
252         List<Map<String, String>> list2 = Stream.of(map3, map4).collect(Collectors.toList());
253
254         List<Map<String, String>> resultList = CommonMethods.mergeListsOfMap(list1, list2);
255
256         Assert.assertEquals(resultList.size(), 6);
257
258         //Verify for duplicate key
259         int count = 0;
260         for(Map<String, String> map : resultList) {
261             if(map.containsKey("Server2"))
262                 count++;
263         }
264
265         Assert.assertEquals(1, count);
266     }
267
268     @Test
269     public void testMergeLists() {
270         List<String> list1 = Stream.of("First", "Second").collect(Collectors.toList());
271         List<String> list2 = Stream.of("Third", "Fourth").collect(Collectors.toList());
272
273         List<String> resultList = CommonMethods.mergeLists(list1, list2);
274
275         Assert.assertEquals(resultList.size(), 4);
276         Assert.assertTrue(resultList.containsAll(list1));
277         Assert.assertTrue(resultList.containsAll(list2));
278     }
279
280     @Test
281     public void testMergeMaps() {
282         Map<String, String> map1 = Stream.of(new AbstractMap.SimpleEntry<>("Port", "Neutron"),
283                                              new AbstractMap.SimpleEntry<>("Compute", "NOVA"))
284                                     .collect(Collectors.toMap(
285                                             AbstractMap.SimpleEntry::getKey,
286                                             AbstractMap.SimpleEntry::getValue));
287
288         Map<String, String> map2 = Stream.of(new AbstractMap.SimpleEntry<>("VLAN", "VMI"),
289                 new AbstractMap.SimpleEntry<>("Volume", "Cinder"),
290                 new AbstractMap.SimpleEntry<>("Port", "VMI"))
291                 .collect(Collectors.toMap(
292                         AbstractMap.SimpleEntry::getKey,
293                         AbstractMap.SimpleEntry::getValue));
294
295         Map<String, String> resultMap = CommonMethods.mergeMaps(map1, map2);
296
297         Assert.assertEquals(resultMap.size(), 4);
298         Assert.assertEquals(resultMap.get("Port"), "VMI");
299     }
300 }