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