Add unit tests to increase sonar coverage
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / util / NodeUtilsTest.java
diff --git a/src/test/java/org/onap/aai/datarouter/util/NodeUtilsTest.java b/src/test/java/org/onap/aai/datarouter/util/NodeUtilsTest.java
new file mode 100644 (file)
index 0000000..19af9eb
--- /dev/null
@@ -0,0 +1,100 @@
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017 Amdocs\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *       http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ */\r
+package org.onap.aai.datarouter.util;\r
+\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.node.ObjectNode;\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+public class NodeUtilsTest {\r
+\r
+    @Test\r
+    public void testGenerateUniqueShaDigest_NullParam(){\r
+        NodeUtils utils = new NodeUtils();\r
+        String keys=null;\r
+        String hashdId = NodeUtils.generateUniqueShaDigest();\r
+        Assert.assertNull(hashdId);\r
+    }\r
+\r
+    @Test\r
+    public void testExtractFieldValueFromObject_NullNode_ExpectNullString(){\r
+        JsonNode node = null;\r
+        String valueNode = NodeUtils.extractFieldValueFromObject(node, "field-1");\r
+        Assert.assertNull(valueNode);\r
+    }\r
+\r
+    @Test\r
+    public void testExtractFieldValueFromObject_NotNullNode_ExpectNotNullString() throws IOException {\r
+        String jsonStr = "{\"key\":\"value\"}";\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        JsonNode node = mapper.readTree(jsonStr);\r
+        String valueNode = NodeUtils.extractFieldValueFromObject(node, "key");\r
+        Assert.assertEquals(valueNode, "value");\r
+    }\r
+\r
+    @Test\r
+    public void testConvertJsonStrToJsonNode_NullJsonStr() throws IOException {\r
+        String jsonStr = null;\r
+        JsonNode jsonNode = NodeUtils.convertJsonStrToJsonNode(jsonStr);\r
+        Assert.assertNull(jsonNode);\r
+    }\r
+\r
+    @Test\r
+    public void testExtractObjectsByKey() throws IOException {\r
+        String jsonStr = "{\n" +\r
+                "  \"id\"   : 1,\n" +\r
+                "  \"name\" : {\n" +\r
+                "    \"first\" : \"user\",\n" +\r
+                "    \"last\" : \"name\"\n" +\r
+                "  },\n" +\r
+                "  \"contact\" : [\n" +\r
+                "    { \"type\" : \"phone/home\", \"ref\" : \"111-111-1234\"},\n" +\r
+                "    { \"type\" : \"phone/work\", \"ref\" : \"222-222-2222\"}\n" +\r
+                "  ]\n" +\r
+                "}";\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        JsonNode node = mapper.readTree(jsonStr);\r
+        List arrList = new ArrayList();\r
+        NodeUtils.extractObjectsByKey(node, "name", arrList);\r
+    }\r
+\r
+    @Test\r
+    public void testConvertObjectToJson() throws IOException {\r
+        String jsonStr = "{\"key\":\"value\"}";\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        Object node = mapper.readTree(jsonStr);\r
+        String retNode1 = NodeUtils.convertObjectToJson(node, false);\r
+        Assert.assertNotNull(retNode1);\r
+        String retNode2 = NodeUtils.convertObjectToJson(node, true);\r
+        Assert.assertNotNull(retNode2);\r
+    }\r
+}
\ No newline at end of file