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.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.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;
34 import org.apache.commons.lang3.ArrayUtils;
35 import org.junit.Test;
37 public class CommonMethodsTest {
39 private static final String[] ARRAY = {"A", "B", "C"};
40 private static final String JAVA_LANG_STRING = "java.lang.String";
43 public void testPrintStackTrace() {
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"));
53 public void testArrayToCommaSeparatedString() {
54 assertEquals(CommonMethods.arrayToCommaSeparatedString(ARRAY), "A,B,C");
58 public void testArrayToCommaSeparatedStringEmpty() {
59 assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[0]), "");
63 public void testArrayToCommaSeparatedStringNulls() {
64 assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {null, null}), "null,null");
68 public void testArrayToCommaSeparatedStringEmptyStrings() {
69 assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] {"", ""}), ",");
72 @Test(expected = NullPointerException.class)
73 public void testArrayToCommaSeparatedStringNull() {
74 CommonMethods.arrayToCommaSeparatedString(null);
78 public void testArrayToSeparatedString() {
79 assertEquals(CommonMethods.arrayToSeparatedString(ARRAY, '/'), "A/B/C");
83 public void testArrayToSeparatedStringEmpty() {
84 assertEquals(CommonMethods.arrayToSeparatedString(new String[0], '/'), "");
88 public void testArrayToSeparatedStringNulls() {
89 assertEquals(CommonMethods.arrayToSeparatedString(new String[] {null, null}, '/'), "null/null");
93 public void testArrayToSeparatedStringEmptyStrings() {
94 assertEquals(CommonMethods.arrayToSeparatedString(new String[] {"", ""}, '/'), "/");
97 @Test(expected = NullPointerException.class)
98 public void testArrayToSeparatedStringNull() {
99 CommonMethods.arrayToSeparatedString(null, '/');
103 public void testCollectionToCommaSeparatedString() {
104 assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(ARRAY)), "A,B,C");
108 public void testCollectionToCommaSeparatedStringNulls() {
109 assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(null, null)), "null,null");
113 public void testCollectionToCommaSeparatedStringEmptyStrings() {
114 assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList("", "")), ",");
118 public void testCollectionToCommaSeparatedStringEmtpy() {
119 assertEquals(CommonMethods.collectionToCommaSeparatedString(Collections.emptySet()), "");
122 @Test(expected = NullPointerException.class)
123 public void testCollectionToCommaSeparatedStringNull() {
124 assertNull(CommonMethods.collectionToCommaSeparatedString(null));
128 public void testNextUuId() {
129 assertNotNull(CommonMethods.nextUuId());
133 public void testConcatBothValuePresent() {
134 String []firstArray = {"1", "2"};
135 String []secondArray = {"3", "4"};
137 String []resultArray = CommonMethods.concat(firstArray, secondArray);
139 assertEquals(resultArray.length, 4);
140 assertTrue(ArrayUtils.contains(resultArray, secondArray[0])
141 && ArrayUtils.contains(resultArray, firstArray[0]));
145 public void testConcatBothFirstValuePresent() {
146 String []firstArray = {"1", "2"};
148 String []resultArray = CommonMethods.concat(firstArray, null);
150 assertEquals(resultArray.length, 2);
151 assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(firstArray)));
155 public void testConcatBothSecondValuePresent() {
156 String []secondArray = {"3", "4"};
158 String []resultArray = CommonMethods.concat(null, secondArray);
160 assertEquals(resultArray.length, 2);
161 assertTrue(Arrays.asList(resultArray).containsAll(Arrays.asList(secondArray)));
165 public void testConcatBothValueNull() {
166 assertNull(CommonMethods.concat(null, null));
170 public void testNewInstance() {
171 Object obj = CommonMethods.newInstance(JAVA_LANG_STRING);
173 assertTrue(obj instanceof String);
176 @Test(expected = IllegalArgumentException.class)
177 public void testNewInstanceIncorrectClassProvided() {
178 assertNull(CommonMethods.newInstance("java.lang.Stringss"));
181 @Test(expected = IllegalArgumentException.class)
182 public void testNewInstanceClassNotProvided() {
183 assertNull(CommonMethods.newInstance(null, Object.class));
186 @Test(expected = IllegalArgumentException.class)
187 public void testNewInstanceObjectNotProvided() {
188 assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, null));
191 @Test(expected = ClassCastException.class)
192 public void testNewInstanceClassCastException() {
193 assertNull(CommonMethods.newInstance(JAVA_LANG_STRING, ArrayList.class));
196 @Test(expected = RuntimeException.class)
197 public void testNewInstanceInvalidClassProvided() {
198 assertNull(CommonMethods.newInstance(List.class));
202 public void testListToSeparatedString() {
203 String str = "Concat,String";
204 String result = CommonMethods.listToSeparatedString(
205 Stream.of("Concat", "String").collect(Collectors.toList()), ',');
207 assertNotNull(result);
208 assertEquals(str, result);
212 public void testDuplicateStringWithDelimiter() {
213 String duplicateStr = CommonMethods.duplicateStringWithDelimiter("Duplicate", '#', 4);
215 assertNotNull(duplicateStr);
217 String[] duplicateStrArray = duplicateStr.split("#");
218 assertTrue(duplicateStr.contains("Duplicate"));
219 assertEquals(duplicateStrArray.length, 4);
223 public void testRoSingleElement() {
224 Set<String> stringSet = CommonMethods.toSingleElementSet("Set Element");
225 assertNotNull(stringSet);
226 assertTrue(stringSet.contains("Set Element"));
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");
235 Map<String, String> map2 = new HashMap<>();
236 map2.put("Server1", "NovaServer_1");
237 map2.put("Server2", "NovaServer_2");
239 List<Map<String, String>> list1 = Stream.of(map1, map2).collect(Collectors.toList());
241 Map<String, String> map3 = new HashMap<>();
242 map3.put("Port3", "NeutronPort_CP_3");
243 map3.put("Port4", "NeutronPort_CP_4");
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");
250 List<Map<String, String>> list2 = Stream.of(map3, map4).collect(Collectors.toList());
252 List<Map<String, String>> resultList = CommonMethods.mergeListsOfMap(list1, list2);
254 assertEquals(resultList.size(), 6);
256 //Verify for duplicate key
258 for(Map<String, String> map : resultList) {
259 if(map.containsKey("Server2"))
263 assertEquals(1, count);
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());
271 List<String> resultList = CommonMethods.mergeLists(list1, list2);
273 assertEquals(resultList.size(), 4);
274 assertTrue(resultList.containsAll(list1));
275 assertTrue(resultList.containsAll(list2));
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));
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));
293 Map<String, String> resultMap = CommonMethods.mergeMaps(map1, map2);
295 assertEquals(resultMap.size(), 4);
296 assertEquals(resultMap.get("Port"), "VMI");