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