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.fail;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
38 import javax.xml.stream.XMLStreamConstants;
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;
46 import com.fasterxml.jackson.core.JsonProcessingException;
49 * The Class NodeUtilsTest.
51 public class NodeUtilsTest {
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;
69 * @throws Exception the exception
72 public void init() throws Exception {}
75 * String buildDepthPadding(int depth)
79 * Builds the depth padding with negative depth.
82 public void buildDepthPaddingWithNegativeDepth() {
83 String paddingString = NodeUtils.buildDepthPadding(-1);
84 assertEquals(paddingString.length(), 0);
88 * Builds the depth padding with zero depth.
91 public void buildDepthPaddingWithZeroDepth() {
92 String paddingString = NodeUtils.buildDepthPadding(0);
93 assertEquals(paddingString.length(), 0);
97 * Builds the depth padding with small depth.
100 public void buildDepthPaddingWithSmallDepth() {
101 String paddingString = NodeUtils.buildDepthPadding(1);
102 assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 1);
106 * Builds the depth padding with large depth.
109 public void buildDepthPaddingWithLargeDepth() {
110 String paddingString = NodeUtils.buildDepthPadding(100);
111 assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 100);
115 * String buildEntityResourceKey(String entityType, String resourceId)
119 * TODO: we should probably throw an IllegalArgumentExecption or just return null if a required
120 * parameter is passed to us with a null.
124 * Builds the entity resource key with null entity type.
127 public void buildEntityResourceKeyWithNullEntityType() {
128 String resourceId = NodeUtils.buildEntityResourceKey(null, "generic-vnf-123");
129 assertEquals(resourceId, "null.generic-vnf-123");
133 * Builds the entity resource key with null resource id.
136 public void buildEntityResourceKeyWithNullResourceId() {
137 String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", null);
138 assertEquals(resourceId, "generic-vnf.null");
142 * Builds the entity resource key success path.
145 public void buildEntityResourceKeySuccessPath() {
146 String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", "generic-vnf-123");
147 assertEquals(resourceId, "generic-vnf.generic-vnf-123");
151 * String extractResourceIdFromLink(String link)
155 * Id extraction when url has trailing forward slash.
158 public void idExtractionWhenUrlHasTrailingForwardSlash() {
160 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK1);
162 if (!"155.196.36.1".equals(resourceId)) {
163 fail("Failed to extract expected resourceId");
168 * Id extraction when url does not have trailing forward slash.
171 public void idExtractionWhenUrlDoesNotHaveTrailingForwardSlash() {
173 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK2);
175 if (!"155.196.36.1".equals(resourceId)) {
176 fail("Failed to extract expected resourceId");
181 * Id extraction when url contains url encoded hex characters.
184 public void idExtractionWhenUrlContainsUrlEncodedHexCharacters() {
186 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK3);
188 if (!"ge-0/1/0".equals(resourceId)) {
189 fail("Failed to extract expected resourceId");
195 * Id extraction when url contains non standard hex characters.
198 public void idExtractionWhenUrlContainsNonStandardHexCharacters() {
200 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK4);
203 * This is not an expected hex encoding, so the decode will fail and the original parameter will
204 * be returned instead.
207 if (!"ge-%bad%wolf%timelord".equals(resourceId)) {
208 fail("Failed to extract expected resourceId");
214 * Id extraction when url is null.
217 public void idExtractionWhenUrlIsNull() {
218 String resourceId = NodeUtils.extractResourceIdFromLink(null);
219 assertEquals(null, resourceId);
223 * Id extraction when url is empty string.
226 public void idExtractionWhenUrlIsEmptyString() {
227 String resourceId = NodeUtils.extractResourceIdFromLink("");
228 assertEquals(null, resourceId);
232 * String getXMLStreamConstantAsStr(int c)
236 * Test string conversion of xml stream constants.
239 public void testStringConversionOfXmlStreamConstants() {
242 * Range of enum is 0 - 256
245 for (int id = 0; id <= 256; id++) {
248 case XMLStreamConstants.ATTRIBUTE: {
249 assertEquals("ATTRIBUTE", NodeUtils.getXmlStreamConstantAsStr(id));
253 case XMLStreamConstants.CDATA: {
254 assertEquals("CDATA", NodeUtils.getXmlStreamConstantAsStr(id));
258 case XMLStreamConstants.CHARACTERS: {
259 assertEquals("CHARACTERS", NodeUtils.getXmlStreamConstantAsStr(id));
263 case XMLStreamConstants.COMMENT: {
264 assertEquals("COMMENT", NodeUtils.getXmlStreamConstantAsStr(id));
268 case XMLStreamConstants.DTD: {
269 assertEquals("DTD", NodeUtils.getXmlStreamConstantAsStr(id));
273 case XMLStreamConstants.END_DOCUMENT: {
274 assertEquals("END_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
278 case XMLStreamConstants.END_ELEMENT: {
279 assertEquals("END_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
283 case XMLStreamConstants.ENTITY_DECLARATION: {
284 assertEquals("ENTITY_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
288 case XMLStreamConstants.ENTITY_REFERENCE: {
289 assertEquals("ENTITY_REFERENCE", NodeUtils.getXmlStreamConstantAsStr(id));
293 case XMLStreamConstants.NAMESPACE: {
294 assertEquals("NAMESPACE", NodeUtils.getXmlStreamConstantAsStr(id));
298 case XMLStreamConstants.NOTATION_DECLARATION: {
299 assertEquals("NOTATION_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
303 case XMLStreamConstants.PROCESSING_INSTRUCTION: {
304 assertEquals("PROCESSING_INSTRUCTION", NodeUtils.getXmlStreamConstantAsStr(id));
308 case XMLStreamConstants.SPACE: {
309 assertEquals("SPACE", NodeUtils.getXmlStreamConstantAsStr(id));
313 case XMLStreamConstants.START_DOCUMENT: {
314 assertEquals("START_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
318 case XMLStreamConstants.START_ELEMENT: {
319 assertEquals("START_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
324 String result = NodeUtils.getXmlStreamConstantAsStr(id);
325 assertNotNull(result);
326 if (!result.startsWith("Unknown")) {
327 fail("Unexecpted XML Stream Constant definition for id = " + id);
336 * Convert object to json successful.
338 * @throws JsonProcessingException the json processing exception
341 public void convertObjectToJsonSuccessful() throws JsonProcessingException {
343 OperationResult opResult = new OperationResult(200, "op result");
344 String asJson = NodeUtils.convertObjectToJson(opResult, false);
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"));
353 * Convert object to json successful pretty.
355 * @throws JsonProcessingException the json processing exception
358 public void convertObjectToJsonSuccessful_pretty() throws JsonProcessingException {
360 OperationResult opResult = new OperationResult(200, "op result");
361 String asJson = NodeUtils.convertObjectToJson(opResult, true);
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"));
370 * Convert object to json failure caused by null.
372 * @throws JsonProcessingException the json processing exception
375 public void convertObjectToJsonFailure_causedBy_null() throws JsonProcessingException {
377 String asJson = NodeUtils.convertObjectToJson(null, true);
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"));
387 * Convert object to xml successful.
389 * @throws JsonProcessingException the json processing exception
392 public void convertObjectToXmlSuccessful() throws JsonProcessingException {
394 OperationResult opResult = new OperationResult(200, "op result");
395 String asXml = NodeUtils.convertObjectToXml(opResult);
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"));
404 * Convert object to xml failure caused by null.
406 * @throws JsonProcessingException the json processing exception
408 @Test(expected = JSONException.class)
409 public void convertObjectToXmlFailure_causedBy_null() throws JsonProcessingException {
411 String asXml = NodeUtils.convertObjectToXml(null);
412 assertNull("Output should be null", asXml);
417 * Validate concatonate list empty list.
419 * @throws JsonProcessingException the json processing exception
422 public void validateConcatonateList_EmptyList() throws JsonProcessingException {
424 String[] array = null;
425 String result = NodeUtils.concatArray(array);
426 assertEquals("", result);
428 List<String> emptyList = Collections.emptyList();
429 result = NodeUtils.concatArray(emptyList);
430 assertEquals("", result);
434 * Validate concatonate list multiple values.
436 * @throws JsonProcessingException the json processing exception
439 public void validateConcatonateList_MultipleValues() throws JsonProcessingException {
441 List<String> numberList = new ArrayList<String>();
447 String result = NodeUtils.concatArray(numberList);
448 assertEquals("1 2 3", result);
452 * Test format timestamp expect valid result.
455 public void test_formatTimestamp_expectValidResult() {
456 String validTimeStamp = "20170111T123116Z";
457 String result = NodeUtils.formatTimestamp(validTimeStamp);
459 assertEquals("2017-01-11T12:31:16Z", result);
463 * Test format timestamp expect invalid result.
466 public void test_formatTimestamp_expectInvalidResult() {
467 String validTimeStamp = "#20170011T123116Z";
468 String result = NodeUtils.formatTimestamp(validTimeStamp);
470 assertEquals(validTimeStamp, result);
474 * test calculate edit attributes urls
477 public void validateCalculateEditAttributeLogic() {
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"));