2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.util;
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;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
39 import javax.xml.stream.XMLStreamConstants;
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;
47 import com.fasterxml.jackson.core.JsonProcessingException;
50 * The Class NodeUtilsTest.
52 public class NodeUtilsTest {
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;
70 * @throws Exception the exception
73 public void init() throws Exception {}
76 * String buildDepthPadding(int depth)
80 * Builds the depth padding with negative depth.
83 public void buildDepthPaddingWithNegativeDepth() {
84 String paddingString = NodeUtils.buildDepthPadding(-1);
85 assertEquals(paddingString.length(), 0);
89 * Builds the depth padding with zero depth.
92 public void buildDepthPaddingWithZeroDepth() {
93 String paddingString = NodeUtils.buildDepthPadding(0);
94 assertEquals(paddingString.length(), 0);
98 * Builds the depth padding with small depth.
101 public void buildDepthPaddingWithSmallDepth() {
102 String paddingString = NodeUtils.buildDepthPadding(1);
103 assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 1);
107 * Builds the depth padding with large depth.
110 public void buildDepthPaddingWithLargeDepth() {
111 String paddingString = NodeUtils.buildDepthPadding(100);
112 assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 100);
116 * String buildEntityResourceKey(String entityType, String resourceId)
120 * TODO: we should probably throw an IllegalArgumentExecption or just return null if a required
121 * parameter is passed to us with a null.
125 * Builds the entity resource key with null entity type.
128 public void buildEntityResourceKeyWithNullEntityType() {
129 String resourceId = NodeUtils.buildEntityResourceKey(null, "generic-vnf-123");
130 assertEquals(resourceId, "null.generic-vnf-123");
134 * Builds the entity resource key with null resource id.
137 public void buildEntityResourceKeyWithNullResourceId() {
138 String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", null);
139 assertEquals(resourceId, "generic-vnf.null");
143 * Builds the entity resource key success path.
146 public void buildEntityResourceKeySuccessPath() {
147 String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", "generic-vnf-123");
148 assertEquals(resourceId, "generic-vnf.generic-vnf-123");
152 * String extractResourceIdFromLink(String link)
156 * Id extraction when url has trailing forward slash.
159 public void idExtractionWhenUrlHasTrailingForwardSlash() {
161 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK1);
163 if (!"155.196.36.1".equals(resourceId)) {
164 fail("Failed to extract expected resourceId");
169 * Id extraction when url does not have trailing forward slash.
172 public void idExtractionWhenUrlDoesNotHaveTrailingForwardSlash() {
174 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK2);
176 if (!"155.196.36.1".equals(resourceId)) {
177 fail("Failed to extract expected resourceId");
182 * Id extraction when url contains url encoded hex characters.
185 public void idExtractionWhenUrlContainsUrlEncodedHexCharacters() {
187 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK3);
189 if (!"ge-0/1/0".equals(resourceId)) {
190 fail("Failed to extract expected resourceId");
196 * Id extraction when url contains non standard hex characters.
199 public void idExtractionWhenUrlContainsNonStandardHexCharacters() {
201 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK4);
204 * This is not an expected hex encoding, so the decode will fail and the original parameter will
205 * be returned instead.
208 if (!"ge-%bad%wolf%timelord".equals(resourceId)) {
209 fail("Failed to extract expected resourceId");
215 * Id extraction when url is null.
218 public void idExtractionWhenUrlIsNull() {
219 String resourceId = NodeUtils.extractResourceIdFromLink(null);
220 assertEquals(null, resourceId);
224 * Id extraction when url is empty string.
227 public void idExtractionWhenUrlIsEmptyString() {
228 String resourceId = NodeUtils.extractResourceIdFromLink("");
229 assertEquals(null, resourceId);
233 * String getXMLStreamConstantAsStr(int c)
237 * Test string conversion of xml stream constants.
240 public void testStringConversionOfXmlStreamConstants() {
243 * Range of enum is 0 - 256
246 for (int id = 0; id <= 256; id++) {
249 case XMLStreamConstants.ATTRIBUTE: {
250 assertEquals("ATTRIBUTE", NodeUtils.getXmlStreamConstantAsStr(id));
254 case XMLStreamConstants.CDATA: {
255 assertEquals("CDATA", NodeUtils.getXmlStreamConstantAsStr(id));
259 case XMLStreamConstants.CHARACTERS: {
260 assertEquals("CHARACTERS", NodeUtils.getXmlStreamConstantAsStr(id));
264 case XMLStreamConstants.COMMENT: {
265 assertEquals("COMMENT", NodeUtils.getXmlStreamConstantAsStr(id));
269 case XMLStreamConstants.DTD: {
270 assertEquals("DTD", NodeUtils.getXmlStreamConstantAsStr(id));
274 case XMLStreamConstants.END_DOCUMENT: {
275 assertEquals("END_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
279 case XMLStreamConstants.END_ELEMENT: {
280 assertEquals("END_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
284 case XMLStreamConstants.ENTITY_DECLARATION: {
285 assertEquals("ENTITY_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
289 case XMLStreamConstants.ENTITY_REFERENCE: {
290 assertEquals("ENTITY_REFERENCE", NodeUtils.getXmlStreamConstantAsStr(id));
294 case XMLStreamConstants.NAMESPACE: {
295 assertEquals("NAMESPACE", NodeUtils.getXmlStreamConstantAsStr(id));
299 case XMLStreamConstants.NOTATION_DECLARATION: {
300 assertEquals("NOTATION_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
304 case XMLStreamConstants.PROCESSING_INSTRUCTION: {
305 assertEquals("PROCESSING_INSTRUCTION", NodeUtils.getXmlStreamConstantAsStr(id));
309 case XMLStreamConstants.SPACE: {
310 assertEquals("SPACE", NodeUtils.getXmlStreamConstantAsStr(id));
314 case XMLStreamConstants.START_DOCUMENT: {
315 assertEquals("START_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
319 case XMLStreamConstants.START_ELEMENT: {
320 assertEquals("START_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
325 String result = NodeUtils.getXmlStreamConstantAsStr(id);
326 assertNotNull(result);
327 if (!result.startsWith("Unknown")) {
328 fail("Unexecpted XML Stream Constant definition for id = " + id);
337 * Convert object to json successful.
339 * @throws JsonProcessingException the json processing exception
342 public void convertObjectToJsonSuccessful() throws JsonProcessingException {
344 OperationResult opResult = new OperationResult(200, "op result");
345 String asJson = NodeUtils.convertObjectToJson(opResult, false);
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"));
354 * Convert object to json successful pretty.
356 * @throws JsonProcessingException the json processing exception
359 public void convertObjectToJsonSuccessful_pretty() throws JsonProcessingException {
361 OperationResult opResult = new OperationResult(200, "op result");
362 String asJson = NodeUtils.convertObjectToJson(opResult, true);
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"));
371 * Convert object to json failure caused by null.
373 * @throws JsonProcessingException the json processing exception
376 public void convertObjectToJsonFailure_causedBy_null() throws JsonProcessingException {
378 String asJson = NodeUtils.convertObjectToJson(null, true);
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"));
388 * Convert object to xml successful.
390 * @throws JsonProcessingException the json processing exception
393 public void convertObjectToXmlSuccessful() throws JsonProcessingException {
395 OperationResult opResult = new OperationResult(200, "op result");
396 String asXml = NodeUtils.convertObjectToXml(opResult);
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"));
405 * Convert object to xml failure caused by null.
407 * @throws JsonProcessingException the json processing exception
409 @Test(expected = JSONException.class)
410 public void convertObjectToXmlFailure_causedBy_null() throws JsonProcessingException {
412 String asXml = NodeUtils.convertObjectToXml(null);
413 assertNull("Output should be null", asXml);
418 * Validate concatonate list empty list.
420 * @throws JsonProcessingException the json processing exception
423 public void validateConcatonateList_EmptyList() throws JsonProcessingException {
425 String[] array = null;
426 String result = NodeUtils.concatArray(array);
427 assertEquals("", result);
429 List<String> emptyList = Collections.emptyList();
430 result = NodeUtils.concatArray(emptyList);
431 assertEquals("", result);
435 * Validate concatonate list multiple values.
437 * @throws JsonProcessingException the json processing exception
440 public void validateConcatonateList_MultipleValues() throws JsonProcessingException {
442 List<String> numberList = new ArrayList<String>();
448 String result = NodeUtils.concatArray(numberList);
449 assertEquals("1 2 3", result);
453 * Test format timestamp expect valid result.
456 public void test_formatTimestamp_expectValidResult() {
457 String validTimeStamp = "20170111T123116Z";
458 String result = NodeUtils.formatTimestamp(validTimeStamp);
460 assertEquals("2017-01-11T12:31:16Z", result);
464 * Test format timestamp expect invalid result.
467 public void test_formatTimestamp_expectInvalidResult() {
468 String validTimeStamp = "#20170011T123116Z";
469 String result = NodeUtils.formatTimestamp(validTimeStamp);
471 assertEquals(validTimeStamp, result);
475 * Test isNumeric expect true
478 public void test_isNumeric_expectFalse() {
479 String invalidNumber = "number";
480 assertFalse(NodeUtils.isNumeric(invalidNumber));
484 * Test isNumeric expect true
487 public void test_isNumeric_expectTrue() {
488 String validNumber = "123";
489 assertTrue(NodeUtils.isNumeric(validNumber));
493 * test calculate edit attributes urls
496 public void validateCalculateEditAttributeLogic() {
499 NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v7/pservers/pserver/12345"),
500 "pservers/pserver/12345");
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"));