2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017-2018 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 package org.onap.aai.datarouter.util;
\r
23 import com.fasterxml.jackson.databind.JsonNode;
\r
24 import com.fasterxml.jackson.databind.ObjectMapper;
\r
25 import com.fasterxml.jackson.databind.node.ObjectNode;
\r
26 import org.junit.Assert;
\r
27 import org.junit.Test;
\r
29 import java.io.IOException;
\r
30 import java.util.ArrayList;
\r
31 import java.util.Collection;
\r
32 import java.util.List;
\r
34 import static org.junit.Assert.*;
\r
36 public class NodeUtilsTest {
\r
39 public void testGenerateUniqueShaDigest_NullParam(){
\r
40 NodeUtils utils = new NodeUtils();
\r
42 String hashdId = NodeUtils.generateUniqueShaDigest();
\r
43 Assert.assertNull(hashdId);
\r
47 public void testExtractFieldValueFromObject_NullNode_ExpectNullString(){
\r
48 JsonNode node = null;
\r
49 String valueNode = NodeUtils.extractFieldValueFromObject(node, "field-1");
\r
50 Assert.assertNull(valueNode);
\r
54 public void testExtractFieldValueFromObject_NotNullNode_ExpectNotNullString() throws IOException {
\r
55 String jsonStr = "{\"key\":\"value\"}";
\r
56 ObjectMapper mapper = new ObjectMapper();
\r
57 JsonNode node = mapper.readTree(jsonStr);
\r
58 String valueNode = NodeUtils.extractFieldValueFromObject(node, "key");
\r
59 Assert.assertEquals(valueNode, "value");
\r
63 public void testConvertJsonStrToJsonNode_NullJsonStr() throws IOException {
\r
64 String jsonStr = null;
\r
65 JsonNode jsonNode = NodeUtils.convertJsonStrToJsonNode(jsonStr);
\r
66 Assert.assertNull(jsonNode);
\r
70 public void testExtractObjectsByKey() throws IOException {
\r
71 String jsonStr = "{\n" +
\r
74 " \"first\" : \"user\",\n" +
\r
75 " \"last\" : \"name\"\n" +
\r
77 " \"contact\" : [\n" +
\r
78 " { \"type\" : \"phone/home\", \"ref\" : \"111-111-1234\"},\n" +
\r
79 " { \"type\" : \"phone/work\", \"ref\" : \"222-222-2222\"}\n" +
\r
82 ObjectMapper mapper = new ObjectMapper();
\r
83 JsonNode node = mapper.readTree(jsonStr);
\r
84 List arrList = new ArrayList();
\r
85 NodeUtils.extractObjectsByKey(node, "name", arrList);
\r
89 public void testConvertObjectToJson() throws IOException {
\r
90 String jsonStr = "{\"key\":\"value\"}";
\r
91 ObjectMapper mapper = new ObjectMapper();
\r
92 Object node = mapper.readTree(jsonStr);
\r
93 String retNode1 = NodeUtils.convertObjectToJson(node, false);
\r
94 Assert.assertNotNull(retNode1);
\r
95 String retNode2 = NodeUtils.convertObjectToJson(node, true);
\r
96 Assert.assertNotNull(retNode2);
\r