2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017 Amdocs
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 package org.onap.aai.datarouter.util;
\r
25 import com.fasterxml.jackson.databind.JsonNode;
\r
26 import com.fasterxml.jackson.databind.ObjectMapper;
\r
27 import com.fasterxml.jackson.databind.node.ObjectNode;
\r
28 import org.junit.Assert;
\r
29 import org.junit.Test;
\r
31 import java.io.IOException;
\r
32 import java.util.ArrayList;
\r
33 import java.util.Collection;
\r
34 import java.util.List;
\r
36 import static org.junit.Assert.*;
\r
38 public class NodeUtilsTest {
\r
41 public void testGenerateUniqueShaDigest_NullParam(){
\r
42 NodeUtils utils = new NodeUtils();
\r
44 String hashdId = NodeUtils.generateUniqueShaDigest();
\r
45 Assert.assertNull(hashdId);
\r
49 public void testExtractFieldValueFromObject_NullNode_ExpectNullString(){
\r
50 JsonNode node = null;
\r
51 String valueNode = NodeUtils.extractFieldValueFromObject(node, "field-1");
\r
52 Assert.assertNull(valueNode);
\r
56 public void testExtractFieldValueFromObject_NotNullNode_ExpectNotNullString() throws IOException {
\r
57 String jsonStr = "{\"key\":\"value\"}";
\r
58 ObjectMapper mapper = new ObjectMapper();
\r
59 JsonNode node = mapper.readTree(jsonStr);
\r
60 String valueNode = NodeUtils.extractFieldValueFromObject(node, "key");
\r
61 Assert.assertEquals(valueNode, "value");
\r
65 public void testConvertJsonStrToJsonNode_NullJsonStr() throws IOException {
\r
66 String jsonStr = null;
\r
67 JsonNode jsonNode = NodeUtils.convertJsonStrToJsonNode(jsonStr);
\r
68 Assert.assertNull(jsonNode);
\r
72 public void testExtractObjectsByKey() throws IOException {
\r
73 String jsonStr = "{\n" +
\r
76 " \"first\" : \"user\",\n" +
\r
77 " \"last\" : \"name\"\n" +
\r
79 " \"contact\" : [\n" +
\r
80 " { \"type\" : \"phone/home\", \"ref\" : \"111-111-1234\"},\n" +
\r
81 " { \"type\" : \"phone/work\", \"ref\" : \"222-222-2222\"}\n" +
\r
84 ObjectMapper mapper = new ObjectMapper();
\r
85 JsonNode node = mapper.readTree(jsonStr);
\r
86 List arrList = new ArrayList();
\r
87 NodeUtils.extractObjectsByKey(node, "name", arrList);
\r
91 public void testConvertObjectToJson() throws IOException {
\r
92 String jsonStr = "{\"key\":\"value\"}";
\r
93 ObjectMapper mapper = new ObjectMapper();
\r
94 Object node = mapper.readTree(jsonStr);
\r
95 String retNode1 = NodeUtils.convertObjectToJson(node, false);
\r
96 Assert.assertNotNull(retNode1);
\r
97 String retNode2 = NodeUtils.convertObjectToJson(node, true);
\r
98 Assert.assertNotNull(retNode2);
\r