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.openecomp.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.openecomp.sparky.dal.rest.OperationResult;
45 import com.fasterxml.jackson.core.JsonProcessingException;
48 * The Class NodeUtilsTest.
50 public class NodeUtilsTest {
53 private static final String TEST_LINK1 =
54 "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/";
55 private static final String TEST_LINK2 =
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_LINK3 =
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/ge-0%2f1%2f0";
59 private static final String TEST_LINK4 =
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-%bad%wolf%timelord";
61 private static final String TEST_LINK5_NO_RESOURCE_ID =
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//";
63 private static final int NODE_UTILS_TAB_WIDTH = 3;
68 * @throws Exception the exception
71 public void init() throws Exception {}
74 * String buildDepthPadding(int depth)
78 * Builds the depth padding with negative depth.
81 public void buildDepthPaddingWithNegativeDepth() {
82 String paddingString = NodeUtils.buildDepthPadding(-1);
83 assertEquals(paddingString.length(), 0);
87 * Builds the depth padding with zero depth.
90 public void buildDepthPaddingWithZeroDepth() {
91 String paddingString = NodeUtils.buildDepthPadding(0);
92 assertEquals(paddingString.length(), 0);
96 * Builds the depth padding with small depth.
99 public void buildDepthPaddingWithSmallDepth() {
100 String paddingString = NodeUtils.buildDepthPadding(1);
101 assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 1);
105 * Builds the depth padding with large depth.
108 public void buildDepthPaddingWithLargeDepth() {
109 String paddingString = NodeUtils.buildDepthPadding(100);
110 assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 100);
114 * String buildEntityResourceKey(String entityType, String resourceId)
118 * TODO: we should probably throw an IllegalArgumentExecption or just return null if a required
119 * parameter is passed to us with a null.
123 * Builds the entity resource key with null entity type.
126 public void buildEntityResourceKeyWithNullEntityType() {
127 String resourceId = NodeUtils.buildEntityResourceKey(null, "generic-vnf-123");
128 assertEquals(resourceId, "null.generic-vnf-123");
132 * Builds the entity resource key with null resource id.
135 public void buildEntityResourceKeyWithNullResourceId() {
136 String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", null);
137 assertEquals(resourceId, "generic-vnf.null");
141 * Builds the entity resource key success path.
144 public void buildEntityResourceKeySuccessPath() {
145 String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", "generic-vnf-123");
146 assertEquals(resourceId, "generic-vnf.generic-vnf-123");
150 * String extractResourceIdFromLink(String link)
154 * Id extraction when url has trailing forward slash.
157 public void idExtractionWhenUrlHasTrailingForwardSlash() {
159 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK1);
161 if (!"155.196.36.1".equals(resourceId)) {
162 fail("Failed to extract expected resourceId");
167 * Id extraction when url does not have trailing forward slash.
170 public void idExtractionWhenUrlDoesNotHaveTrailingForwardSlash() {
172 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK2);
174 if (!"155.196.36.1".equals(resourceId)) {
175 fail("Failed to extract expected resourceId");
180 * Id extraction when url contains url encoded hex characters.
183 public void idExtractionWhenUrlContainsUrlEncodedHexCharacters() {
185 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK3);
187 if (!"ge-0/1/0".equals(resourceId)) {
188 fail("Failed to extract expected resourceId");
194 * Id extraction when url contains non standard hex characters.
197 public void idExtractionWhenUrlContainsNonStandardHexCharacters() {
199 String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK4);
202 * This is not an expected hex encoding, so the decode will fail and the original parameter will
203 * be returned instead.
206 if (!"ge-%bad%wolf%timelord".equals(resourceId)) {
207 fail("Failed to extract expected resourceId");
213 * Id extraction when url is null.
216 public void idExtractionWhenUrlIsNull() {
217 String resourceId = NodeUtils.extractResourceIdFromLink(null);
218 assertEquals(null, resourceId);
222 * Id extraction when url is empty string.
225 public void idExtractionWhenUrlIsEmptyString() {
226 String resourceId = NodeUtils.extractResourceIdFromLink("");
227 assertEquals(null, resourceId);
231 * String getXMLStreamConstantAsStr(int c)
235 * Test string conversion of xml stream constants.
238 public void testStringConversionOfXmlStreamConstants() {
241 * Range of enum is 0 - 256
244 for (int id = 0; id <= 256; id++) {
247 case XMLStreamConstants.ATTRIBUTE: {
248 assertEquals("ATTRIBUTE", NodeUtils.getXmlStreamConstantAsStr(id));
252 case XMLStreamConstants.CDATA: {
253 assertEquals("CDATA", NodeUtils.getXmlStreamConstantAsStr(id));
257 case XMLStreamConstants.CHARACTERS: {
258 assertEquals("CHARACTERS", NodeUtils.getXmlStreamConstantAsStr(id));
262 case XMLStreamConstants.COMMENT: {
263 assertEquals("COMMENT", NodeUtils.getXmlStreamConstantAsStr(id));
267 case XMLStreamConstants.DTD: {
268 assertEquals("DTD", NodeUtils.getXmlStreamConstantAsStr(id));
272 case XMLStreamConstants.END_DOCUMENT: {
273 assertEquals("END_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
277 case XMLStreamConstants.END_ELEMENT: {
278 assertEquals("END_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
282 case XMLStreamConstants.ENTITY_DECLARATION: {
283 assertEquals("ENTITY_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
287 case XMLStreamConstants.ENTITY_REFERENCE: {
288 assertEquals("ENTITY_REFERENCE", NodeUtils.getXmlStreamConstantAsStr(id));
292 case XMLStreamConstants.NAMESPACE: {
293 assertEquals("NAMESPACE", NodeUtils.getXmlStreamConstantAsStr(id));
297 case XMLStreamConstants.NOTATION_DECLARATION: {
298 assertEquals("NOTATION_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
302 case XMLStreamConstants.PROCESSING_INSTRUCTION: {
303 assertEquals("PROCESSING_INSTRUCTION", NodeUtils.getXmlStreamConstantAsStr(id));
307 case XMLStreamConstants.SPACE: {
308 assertEquals("SPACE", NodeUtils.getXmlStreamConstantAsStr(id));
312 case XMLStreamConstants.START_DOCUMENT: {
313 assertEquals("START_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
317 case XMLStreamConstants.START_ELEMENT: {
318 assertEquals("START_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
323 String result = NodeUtils.getXmlStreamConstantAsStr(id);
324 assertNotNull(result);
325 if (!result.startsWith("Unknown")) {
326 fail("Unexecpted XML Stream Constant definition for id = " + id);
335 * Convert object to json successful.
337 * @throws JsonProcessingException the json processing exception
340 public void convertObjectToJsonSuccessful() throws JsonProcessingException {
342 OperationResult opResult = new OperationResult(200, "op result");
343 String asJson = NodeUtils.convertObjectToJson(opResult, false);
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"));
352 * Convert object to json successful pretty.
354 * @throws JsonProcessingException the json processing exception
357 public void convertObjectToJsonSuccessful_pretty() throws JsonProcessingException {
359 OperationResult opResult = new OperationResult(200, "op result");
360 String asJson = NodeUtils.convertObjectToJson(opResult, true);
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"));
369 * Convert object to json failure caused by null.
371 * @throws JsonProcessingException the json processing exception
374 public void convertObjectToJsonFailure_causedBy_null() throws JsonProcessingException {
376 String asJson = NodeUtils.convertObjectToJson(null, true);
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"));
386 * Convert object to xml successful.
388 * @throws JsonProcessingException the json processing exception
391 public void convertObjectToXmlSuccessful() throws JsonProcessingException {
393 OperationResult opResult = new OperationResult(200, "op result");
394 String asXml = NodeUtils.convertObjectToXml(opResult);
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"));
403 * Convert object to xml failure caused by null.
405 * @throws JsonProcessingException the json processing exception
407 @Test(expected = JSONException.class)
408 public void convertObjectToXmlFailure_causedBy_null() throws JsonProcessingException {
410 String asXml = NodeUtils.convertObjectToXml(null);
411 assertNull("Output should be null", asXml);
416 * Validate concatonate list empty list.
418 * @throws JsonProcessingException the json processing exception
421 public void validateConcatonateList_EmptyList() throws JsonProcessingException {
423 String[] array = null;
424 String result = NodeUtils.concatArray(array);
425 assertEquals("", result);
427 List<String> emptyList = Collections.emptyList();
428 result = NodeUtils.concatArray(emptyList);
429 assertEquals("", result);
433 * Validate concatonate list multiple values.
435 * @throws JsonProcessingException the json processing exception
438 public void validateConcatonateList_MultipleValues() throws JsonProcessingException {
440 List<String> numberList = new ArrayList<String>();
446 String result = NodeUtils.concatArray(numberList);
447 assertEquals("1 2 3", result);
451 * Test format timestamp expect valid result.
454 public void test_formatTimestamp_expectValidResult() {
455 String validTimeStamp = "20170111T123116Z";
456 String result = NodeUtils.formatTimestamp(validTimeStamp);
458 assertEquals("2017-01-11T12:31:16Z", result);
462 * Test format timestamp expect invalid result.
465 public void test_formatTimestamp_expectInvalidResult() {
466 String validTimeStamp = "#20170011T123116Z";
467 String result = NodeUtils.formatTimestamp(validTimeStamp);
469 assertEquals(validTimeStamp, result);
473 * test calculate edit attributes urls
476 public void validateCalculateEditAttributeLogic() {
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"));