Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / util / NodeUtilsTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.sparky.util;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37
38 import javax.xml.stream.XMLStreamConstants;
39
40 import org.json.JSONException;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.aai.sparky.dal.rest.OperationResult;
44 import org.onap.aai.sparky.util.NodeUtils;
45
46 import com.fasterxml.jackson.core.JsonProcessingException;
47
48 /**
49  * The Class NodeUtilsTest.
50  */
51 public class NodeUtilsTest {
52
53
54   private static final String TEST_LINK1 =
55       "https://aai-hostname:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/155.196.36.1/";
56   private static final String TEST_LINK2 =
57       "https://aai-hostname:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/155.196.36.1";
58   private static final String TEST_LINK3 =
59       "https://aai-hostname:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/ge-0%2f1%2f0";
60   private static final String TEST_LINK4 =
61       "https://aai-hostname:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/ge-%bad%wolf%timelord";
62   private static final String TEST_LINK5_NO_RESOURCE_ID =
63       "https://aai-hostname:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list//";
64   private static final int NODE_UTILS_TAB_WIDTH = 3;
65
66   /**
67    * Inits the.
68    *
69    * @throws Exception the exception
70    */
71   @Before
72   public void init() throws Exception {}
73
74   /*
75    * String buildDepthPadding(int depth)
76    */
77
78   /**
79    * Builds the depth padding with negative depth.
80    */
81   @Test
82   public void buildDepthPaddingWithNegativeDepth() {
83     String paddingString = NodeUtils.buildDepthPadding(-1);
84     assertEquals(paddingString.length(), 0);
85   }
86
87   /**
88    * Builds the depth padding with zero depth.
89    */
90   @Test
91   public void buildDepthPaddingWithZeroDepth() {
92     String paddingString = NodeUtils.buildDepthPadding(0);
93     assertEquals(paddingString.length(), 0);
94   }
95
96   /**
97    * Builds the depth padding with small depth.
98    */
99   @Test
100   public void buildDepthPaddingWithSmallDepth() {
101     String paddingString = NodeUtils.buildDepthPadding(1);
102     assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 1);
103   }
104
105   /**
106    * Builds the depth padding with large depth.
107    */
108   @Test
109   public void buildDepthPaddingWithLargeDepth() {
110     String paddingString = NodeUtils.buildDepthPadding(100);
111     assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 100);
112   }
113
114   /*
115    * String buildEntityResourceKey(String entityType, String resourceId)
116    */
117
118   /*
119    * TODO: we should probably throw an IllegalArgumentExecption or just return null if a required
120    * parameter is passed to us with a null.
121    */
122
123   /**
124    * Builds the entity resource key with null entity type.
125    */
126   @Test
127   public void buildEntityResourceKeyWithNullEntityType() {
128     String resourceId = NodeUtils.buildEntityResourceKey(null, "generic-vnf-123");
129     assertEquals(resourceId, "null.generic-vnf-123");
130   }
131
132   /**
133    * Builds the entity resource key with null resource id.
134    */
135   @Test
136   public void buildEntityResourceKeyWithNullResourceId() {
137     String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", null);
138     assertEquals(resourceId, "generic-vnf.null");
139   }
140
141   /**
142    * Builds the entity resource key success path.
143    */
144   @Test
145   public void buildEntityResourceKeySuccessPath() {
146     String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", "generic-vnf-123");
147     assertEquals(resourceId, "generic-vnf.generic-vnf-123");
148   }
149
150   /*
151    * String extractResourceIdFromLink(String link)
152    */
153
154   /**
155    * Id extraction when url has trailing forward slash.
156    */
157   @Test
158   public void idExtractionWhenUrlHasTrailingForwardSlash() {
159
160     String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK1);
161
162     if (!"155.196.36.1".equals(resourceId)) {
163       fail("Failed to extract expected resourceId");
164     }
165   }
166
167   /**
168    * Id extraction when url does not have trailing forward slash.
169    */
170   @Test
171   public void idExtractionWhenUrlDoesNotHaveTrailingForwardSlash() {
172
173     String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK2);
174
175     if (!"155.196.36.1".equals(resourceId)) {
176       fail("Failed to extract expected resourceId");
177     }
178   }
179
180   /**
181    * Id extraction when url contains url encoded hex characters.
182    */
183   @Test
184   public void idExtractionWhenUrlContainsUrlEncodedHexCharacters() {
185
186     String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK3);
187
188     if (!"ge-0/1/0".equals(resourceId)) {
189       fail("Failed to extract expected resourceId");
190     }
191
192   }
193
194   /**
195    * Id extraction when url contains non standard hex characters.
196    */
197   @Test
198   public void idExtractionWhenUrlContainsNonStandardHexCharacters() {
199
200     String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK4);
201
202     /*
203      * This is not an expected hex encoding, so the decode will fail and the original parameter will
204      * be returned instead.
205      */
206
207     if (!"ge-%bad%wolf%timelord".equals(resourceId)) {
208       fail("Failed to extract expected resourceId");
209     }
210
211   }
212
213   /**
214    * Id extraction when url is null.
215    */
216   @Test
217   public void idExtractionWhenUrlIsNull() {
218     String resourceId = NodeUtils.extractResourceIdFromLink(null);
219     assertEquals(null, resourceId);
220   }
221
222   /**
223    * Id extraction when url is empty string.
224    */
225   @Test
226   public void idExtractionWhenUrlIsEmptyString() {
227     String resourceId = NodeUtils.extractResourceIdFromLink("");
228     assertEquals(null, resourceId);
229   }
230
231   /*
232    * String getXMLStreamConstantAsStr(int c)
233    */
234
235   /**
236    * Test string conversion of xml stream constants.
237    */
238   @Test
239   public void testStringConversionOfXmlStreamConstants() {
240
241     /*
242      * Range of enum is 0 - 256
243      */
244
245     for (int id = 0; id <= 256; id++) {
246
247       switch (id) {
248         case XMLStreamConstants.ATTRIBUTE: {
249           assertEquals("ATTRIBUTE", NodeUtils.getXmlStreamConstantAsStr(id));
250           break;
251         }
252
253         case XMLStreamConstants.CDATA: {
254           assertEquals("CDATA", NodeUtils.getXmlStreamConstantAsStr(id));
255           break;
256         }
257
258         case XMLStreamConstants.CHARACTERS: {
259           assertEquals("CHARACTERS", NodeUtils.getXmlStreamConstantAsStr(id));
260           break;
261         }
262
263         case XMLStreamConstants.COMMENT: {
264           assertEquals("COMMENT", NodeUtils.getXmlStreamConstantAsStr(id));
265           break;
266         }
267
268         case XMLStreamConstants.DTD: {
269           assertEquals("DTD", NodeUtils.getXmlStreamConstantAsStr(id));
270           break;
271         }
272
273         case XMLStreamConstants.END_DOCUMENT: {
274           assertEquals("END_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
275           break;
276         }
277
278         case XMLStreamConstants.END_ELEMENT: {
279           assertEquals("END_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
280           break;
281         }
282
283         case XMLStreamConstants.ENTITY_DECLARATION: {
284           assertEquals("ENTITY_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
285           break;
286         }
287
288         case XMLStreamConstants.ENTITY_REFERENCE: {
289           assertEquals("ENTITY_REFERENCE", NodeUtils.getXmlStreamConstantAsStr(id));
290           break;
291         }
292
293         case XMLStreamConstants.NAMESPACE: {
294           assertEquals("NAMESPACE", NodeUtils.getXmlStreamConstantAsStr(id));
295           break;
296         }
297
298         case XMLStreamConstants.NOTATION_DECLARATION: {
299           assertEquals("NOTATION_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
300           break;
301         }
302
303         case XMLStreamConstants.PROCESSING_INSTRUCTION: {
304           assertEquals("PROCESSING_INSTRUCTION", NodeUtils.getXmlStreamConstantAsStr(id));
305           break;
306         }
307
308         case XMLStreamConstants.SPACE: {
309           assertEquals("SPACE", NodeUtils.getXmlStreamConstantAsStr(id));
310           break;
311         }
312
313         case XMLStreamConstants.START_DOCUMENT: {
314           assertEquals("START_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
315           break;
316         }
317
318         case XMLStreamConstants.START_ELEMENT: {
319           assertEquals("START_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
320           break;
321         }
322
323         default:
324           String result = NodeUtils.getXmlStreamConstantAsStr(id);
325           assertNotNull(result);
326           if (!result.startsWith("Unknown")) {
327             fail("Unexecpted XML Stream Constant definition for id = " + id);
328           }
329
330       }
331
332     }
333   }
334
335   /**
336    * Convert object to json successful.
337    *
338    * @throws JsonProcessingException the json processing exception
339    */
340   @Test
341   public void convertObjectToJsonSuccessful() throws JsonProcessingException {
342
343     OperationResult opResult = new OperationResult(200, "op result");
344     String asJson = NodeUtils.convertObjectToJson(opResult, false);
345
346     assertTrue("Doesn't contain result field", asJson.contains("result"));
347     assertTrue("Doesn't contain resultCode field", asJson.contains("resultCode"));
348     assertTrue("Doesn't contain resolvedLinkFailure field", asJson.contains("resolvedLinkFailure"));
349
350   }
351
352   /**
353    * Convert object to json successful pretty.
354    *
355    * @throws JsonProcessingException the json processing exception
356    */
357   @Test
358   public void convertObjectToJsonSuccessful_pretty() throws JsonProcessingException {
359
360     OperationResult opResult = new OperationResult(200, "op result");
361     String asJson = NodeUtils.convertObjectToJson(opResult, true);
362
363     assertTrue("Doesn't contain result field", asJson.contains("result"));
364     assertTrue("Doesn't contain resultCode field", asJson.contains("resultCode"));
365     assertTrue("Doesn't contain resolvedLinkFailure field", asJson.contains("resolvedLinkFailure"));
366
367   }
368
369   /**
370    * Convert object to json failure caused by null.
371    *
372    * @throws JsonProcessingException the json processing exception
373    */
374   @Test()
375   public void convertObjectToJsonFailure_causedBy_null() throws JsonProcessingException {
376
377     String asJson = NodeUtils.convertObjectToJson(null, true);
378
379     assertTrue("Doesn't contain result field", !asJson.contains("result"));
380     assertTrue("Doesn't contain resultCode field", !asJson.contains("resultCode"));
381     assertTrue("Doesn't contain resolvedLinkFailure field",
382         !asJson.contains("resolvedLinkFailure"));
383
384   }
385
386   /**
387    * Convert object to xml successful.
388    *
389    * @throws JsonProcessingException the json processing exception
390    */
391   @Test
392   public void convertObjectToXmlSuccessful() throws JsonProcessingException {
393
394     OperationResult opResult = new OperationResult(200, "op result");
395     String asXml = NodeUtils.convertObjectToXml(opResult);
396
397     assertTrue("Doesn't contain result field", asXml.contains("result"));
398     assertTrue("Doesn't contain resultCode field", asXml.contains("resultCode"));
399     assertTrue("Doesn't contain resolvedLinkFailure field", asXml.contains("resolvedLinkFailure"));
400
401   }
402
403   /**
404    * Convert object to xml failure caused by null.
405    *
406    * @throws JsonProcessingException the json processing exception
407    */
408   @Test(expected = JSONException.class)
409   public void convertObjectToXmlFailure_causedBy_null() throws JsonProcessingException {
410
411     String asXml = NodeUtils.convertObjectToXml(null);
412     assertNull("Output should be null", asXml);
413
414   }
415
416   /**
417    * Validate concatonate list empty list.
418    *
419    * @throws JsonProcessingException the json processing exception
420    */
421   @Test
422   public void validateConcatonateList_EmptyList() throws JsonProcessingException {
423
424     String[] array = null;
425     String result = NodeUtils.concatArray(array);
426     assertEquals("", result);
427
428     List<String> emptyList = Collections.emptyList();
429     result = NodeUtils.concatArray(emptyList);
430     assertEquals("", result);
431   }
432
433   /**
434    * Validate concatonate list multiple values.
435    *
436    * @throws JsonProcessingException the json processing exception
437    */
438   @Test
439   public void validateConcatonateList_MultipleValues() throws JsonProcessingException {
440
441     List<String> numberList = new ArrayList<String>();
442
443     numberList.add("1");
444     numberList.add("2");
445     numberList.add("3");
446
447     String result = NodeUtils.concatArray(numberList);
448     assertEquals("1 2 3", result);
449   }
450  
451   /**
452    * Test format timestamp expect valid result.
453    */
454   @Test
455   public void test_formatTimestamp_expectValidResult() {
456     String validTimeStamp = "20170111T123116Z";
457     String result = NodeUtils.formatTimestamp(validTimeStamp);
458
459     assertEquals("2017-01-11T12:31:16Z", result);
460   }
461
462   /**
463    * Test format timestamp expect invalid result.
464    */
465   @Test
466   public void test_formatTimestamp_expectInvalidResult() {
467     String validTimeStamp = "#20170011T123116Z";
468     String result = NodeUtils.formatTimestamp(validTimeStamp);
469
470     assertEquals(validTimeStamp, result);
471   }
472   
473   /**
474    * test calculate edit attributes urls
475    */
476   @Test
477   public void validateCalculateEditAttributeLogic() {
478     
479     assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v7/pservers/pserver/12345"),"pservers/pserver/12345");
480     assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v1/pservers/pserver/12345"),"pservers/pserver/12345");
481     assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v21/pservers/pserver/12345"),"pservers/pserver/12345");
482     assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v211/pservers/pserver/12345"),"pservers/pserver/12345");
483     assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v5252/pservers/pserver/12345"),"pservers/pserver/12345");
484     assertNull(NodeUtils.calculateEditAttributeUri(null));
485     assertNull(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/noVersionTag/pservers/pserver/12345"));
486
487   }
488
489
490 }