19af9eb1b1f82c6caffb3fe838c1982a58ef061f
[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 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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  */\r
23 package org.onap.aai.datarouter.util;\r
24 \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
30 \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
35 \r
36 import static org.junit.Assert.*;\r
37 \r
38 public class NodeUtilsTest {\r
39 \r
40     @Test\r
41     public void testGenerateUniqueShaDigest_NullParam(){\r
42         NodeUtils utils = new NodeUtils();\r
43         String keys=null;\r
44         String hashdId = NodeUtils.generateUniqueShaDigest();\r
45         Assert.assertNull(hashdId);\r
46     }\r
47 \r
48     @Test\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
53     }\r
54 \r
55     @Test\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
62     }\r
63 \r
64     @Test\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
69     }\r
70 \r
71     @Test\r
72     public void testExtractObjectsByKey() throws IOException {\r
73         String jsonStr = "{\n" +\r
74                 "  \"id\"   : 1,\n" +\r
75                 "  \"name\" : {\n" +\r
76                 "    \"first\" : \"user\",\n" +\r
77                 "    \"last\" : \"name\"\n" +\r
78                 "  },\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
82                 "  ]\n" +\r
83                 "}";\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
88     }\r
89 \r
90     @Test\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
99     }\r
100 }