2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.core.utilities;
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;
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;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
35 import org.apache.commons.lang3.ArrayUtils;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
39 public class CommonMethodsTest {
41 private static final String[] ARRAY = {"A", "B", "C"};
42 private static final String JAVA_LANG_STRING = "java.lang.String";
45 public void testPrintStackTrace() {
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"));
55 public void testArrayToCommaSeparatedString() {
56 assertEquals(CommonMethods.arrayToCommaSeparatedString(ARRAY), "A,B,C");
60 public void testArrayToCommaSeparatedStringEmpty() {
61 assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[0]), "");
65 public void testArrayToCommaSeparatedStringNulls() {
66 assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {null, null}), "null,null");
70 public void testArrayToCommaSeparatedStringEmptyStrings() {
71 assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {"", ""}), ",");
74 @Test(expectedExceptions = NullPointerException.class)
75 public void testArrayToCommaSeparatedStringNull() {
76 CommonMethods.arrayToCommaSeparatedString(null);
80 public void testArrayToSeparatedString() {
81 assertEquals(CommonMethods.arrayToSeparatedString(ARRAY, '/'), "A/B/C");
85 public void testArrayToSeparatedStringEmpty() {
86 assertEquals(CommonMethods.arrayToSeparatedString(new String[0], '/'), "");
90 public void testArrayToSeparatedStringNulls() {
91 assertEquals(CommonMethods.arrayToSeparatedString(new String[] {null, null}, '/'), "null/null");
95 public void testArrayToSeparatedStringEmptyStrings() {
96 assertEquals(CommonMethods.arrayToSeparatedString(new String[] {"", ""}, '/'), "/");
99 @Test(expectedExceptions = NullPointerException.class)
100 public void testArrayToSeparatedStringNull() {
101 CommonMethods.arrayToSeparatedString(null, '/');
105 public void testCollectionToCommaSeparatedString() {
106 assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(ARRAY)), "A,B,C");
110 public void testCollectionToCommaSeparatedStringNulls() {
111 assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(null, null)), "null,null");
115 public void testCollectionToCommaSeparatedStringEmptyStrings() {
116 assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList("", "")), ",");
120 public void testCollectionToCommaSeparatedStringEmtpy() {
121 assertEquals(CommonMethods.collectionToCommaSeparatedString(Collections.emptySet()), "");
124 @Test(expectedExceptions = NullPointerException.class)
125 public void testCollectionToCommaSeparatedStringNull() {
126 assertNull(CommonMethods.collectionToCommaSeparatedString(null));
130 public void testNextUuId() {
131 assertNotNull(CommonMethods.nextUuId());
135 public void testConcatBothValuePresent() {
136 String []firstArray = {"1", "2"};
137 String []secondArray = {"3", "4"};
139 String []resultArray = CommonMethods.concat(firstArray, secondArray);
141 Assert.assertEquals(resultArray.length, 4);
142 Assert.assertTrue(ArrayUtils.contains(resultArray, secondArray[0])
143 && ArrayUtils.contains(resultArray, firstArray[0]));
147 public void testConcatBothFirstValuePresent() {
148 String []firstArray = {"1", "2"};
150 String []resultArray = CommonMethods.concat(firstArray, null);
152 Assert.assertEquals(resultArray.length, 2);
153 Assert.assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(firstArray)));
157 public void testConcatBothSecondValuePresent() {
158 String []secondArray = {"3", "4"};
160 String []resultArray = CommonMethods.concat(null, secondArray);
162 Assert.assertEquals(resultArray.length, 2);
163 Assert.assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(secondArray)));
167 public void testConcatBothValueNull() {
168 Assert.assertNull(CommonMethods.concat(null, null));
172 public void testNewInstance() {
173 Object obj = CommonMethods.newInstance(JAVA_LANG_STRING);
174 Assert.assertNotNull(obj);
175 Assert.assertTrue(obj instanceof String);
178 @Test(expectedExceptions = IllegalArgumentException.class)
179 public void testNewInstanceIncorrectClassProvided() {
180 Assert.assertNull(CommonMethods.newInstance("java.lang.Stringss"));
183 @Test(expectedExceptions = IllegalArgumentException.class)
184 public void testNewInstanceClassNotProvided() {
185 Assert.assertNull(CommonMethods.newInstance(null, Object.class));
188 @Test(expectedExceptions = IllegalArgumentException.class)
189 public void testNewInstanceObjectNotProvided() {
190 Assert.assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, null));
193 @Test(expectedExceptions = ClassCastException.class)
194 public void testNewInstanceClassCastException() {
195 Assert.assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, ArrayList.class));
198 @Test(expectedExceptions = RuntimeException.class)
199 public void testNewInstanceInvalidClassProvided() {
200 Assert.assertNull(CommonMethods.newInstance(List.class));
204 public void testListToSeparatedString() {
205 String str = "Concat,String";
206 String result = CommonMethods.listToSeparatedString(
207 Stream.of("Concat", "String").collect(Collectors.toList()), ',');
209 Assert.assertNotNull(result);
210 Assert.assertEquals(str, result);
214 public void testDuplicateStringWithDelimiter() {
215 String duplicateStr = CommonMethods.duplicateStringWithDelimiter("Duplicate", '#', 4);
217 Assert.assertNotNull(duplicateStr);
219 String[] duplicateStrArray = duplicateStr.split("#");
220 Assert.assertTrue(duplicateStr.contains("Duplicate"));
221 Assert.assertEquals(duplicateStrArray.length, 4);
225 public void testRoSingleElement() {
226 Set<String> stringSet = CommonMethods.toSingleElementSet("Set Element");
227 Assert.assertNotNull(stringSet);
228 Assert.assertTrue(stringSet.contains("Set Element"));
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");
237 Map<String, String> map2 = new HashMap<>();
238 map2.put("Server1", "NovaServer_1");
239 map2.put("Server2", "NovaServer_2");
241 List<Map<String, String>> list1 = Stream.of(map1, map2).collect(Collectors.toList());
243 Map<String, String> map3 = new HashMap<>();
244 map3.put("Port3", "NeutronPort_CP_3");
245 map3.put("Port4", "NeutronPort_CP_4");
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");
252 List<Map<String, String>> list2 = Stream.of(map3, map4).collect(Collectors.toList());
254 List<Map<String, String>> resultList = CommonMethods.mergeListsOfMap(list1, list2);
256 Assert.assertEquals(resultList.size(), 6);
258 //Verify for duplicate key
260 for(Map<String, String> map : resultList) {
261 if(map.containsKey("Server2"))
265 Assert.assertEquals(1, count);
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());
273 List<String> resultList = CommonMethods.mergeLists(list1, list2);
275 Assert.assertEquals(resultList.size(), 4);
276 Assert.assertTrue(resultList.containsAll(list1));
277 Assert.assertTrue(resultList.containsAll(list2));
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));
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));
295 Map<String, String> resultMap = CommonMethods.mergeMaps(map1, map2);
297 Assert.assertEquals(resultMap.size(), 4);
298 Assert.assertEquals(resultMap.get("Port"), "VMI");