Update license date and text
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / util / NodeUtilsTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\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
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
20  */\r
21 package org.onap.aai.datarouter.util;\r
22 \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
28 \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
33 \r
34 import static org.junit.Assert.*;\r
35 \r
36 public class NodeUtilsTest {\r
37 \r
38     @Test\r
39     public void testGenerateUniqueShaDigest_NullParam(){\r
40         NodeUtils utils = new NodeUtils();\r
41         String keys=null;\r
42         String hashdId = NodeUtils.generateUniqueShaDigest();\r
43         Assert.assertNull(hashdId);\r
44     }\r
45 \r
46     @Test\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
51     }\r
52 \r
53     @Test\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
60     }\r
61 \r
62     @Test\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
67     }\r
68 \r
69     @Test\r
70     public void testExtractObjectsByKey() throws IOException {\r
71         String jsonStr = "{\n" +\r
72                 "  \"id\"   : 1,\n" +\r
73                 "  \"name\" : {\n" +\r
74                 "    \"first\" : \"user\",\n" +\r
75                 "    \"last\" : \"name\"\n" +\r
76                 "  },\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
80                 "  ]\n" +\r
81                 "}";\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
86     }\r
87 \r
88     @Test\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
97     }\r
98 }