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 org.junit.Assert;
\r
26 import org.junit.Test;
\r
28 import javax.json.Json;
\r
29 import java.io.IOException;
\r
30 import java.util.ArrayList;
\r
31 import java.util.List;
\r
32 import java.util.Map;
\r
34 public class RouterServiceUtilTest {
\r
37 public void testParseJsonPayloadIntoMap(){
\r
38 String jsonStr = "{\"key1\":\"value1\", \"key2\":\"value2\", \"key3\":\"value3\"}";
\r
39 Map<String, String> retMap = RouterServiceUtil.parseJsonPayloadIntoMap(jsonStr);
\r
40 Assert.assertNotNull(retMap);
\r
41 Assert.assertEquals(retMap.get("key1"), "value1");
\r
45 public void testGetNodeFieldAsText() throws IOException {
\r
46 String jsonStr = "{\"key\":\"value\"}";
\r
47 ObjectMapper mapper = new ObjectMapper();
\r
48 JsonNode node = mapper.readTree(jsonStr);
\r
49 String field = RouterServiceUtil.getNodeFieldAsText(node, "key");
\r
53 public void testConcatArray_NullList(){
\r
54 List<String> strList = null;
\r
55 String result = RouterServiceUtil.concatArray(strList);
\r
56 Assert.assertTrue(result.isEmpty());
\r
59 /*Method in RouterServiceUtil is wrong. It should be corrected
\r
61 public void testConcatArray_NotNullList(){
\r
62 List<String> strList = new ArrayList<String>();
\r
63 strList.add("str1");
\r
64 strList.add("str2");
\r
65 strList.add("str3");
\r
66 String result = RouterServiceUtil.concatArray(strList);
\r
67 Assert.assertEquals(result, "str1 str2 str3");
\r
71 public void testConcatArray_NullArray(){
\r
72 String[] values = new String[]{};
\r
73 //String[] values = new String[]{"value1", "value2", "value3"};
\r
74 String result = RouterServiceUtil.concatArray(values);
\r
75 Assert.assertTrue(result.isEmpty());
\r
79 public void testConcatArray_NotNullArray(){
\r
80 String[] values = new String[]{"value1", "value2", "value3"};
\r
81 String result = RouterServiceUtil.concatArray(values);
\r
82 Assert.assertEquals(result, "value1.value2.value3");
\r
86 public void testRecursivelyLookupJsonPayload_JsonArray() throws IOException {
\r
87 String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]";
\r
88 ObjectMapper mapper = new ObjectMapper();
\r
89 JsonNode node = mapper.readTree(jsonStr);
\r
90 String result = RouterServiceUtil.recursivelyLookupJsonPayload(node, "key1");
\r
91 Assert.assertEquals(result, "value1");
\r
95 public void testExtractObjectsByKey() throws IOException {
\r
96 String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]";
\r
97 ObjectMapper mapper = new ObjectMapper();
\r
98 JsonNode node = mapper.readTree(jsonStr);
\r
99 List<JsonNode> nodeList = new ArrayList<JsonNode>();
\r
100 RouterServiceUtil.extractObjectsByKey(node, "key1", nodeList);
\r
101 Assert.assertTrue(nodeList.size() == 1);
\r
105 public void testConvertArrayIntoList() throws IOException {
\r
106 String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]";
\r
107 ObjectMapper mapper = new ObjectMapper();
\r
108 JsonNode node = mapper.readTree(jsonStr);
\r
109 List<JsonNode> nodeList = new ArrayList<JsonNode>();
\r
110 RouterServiceUtil.convertArrayIntoList(node, nodeList);
\r
111 Assert.assertTrue(nodeList.size() == 2);
\r
115 public void testExtractFieldValueFromObject() throws IOException {
\r
116 String jsonStr = "{\"key1\":\"value1\"}";
\r
117 ObjectMapper mapper = new ObjectMapper();
\r
118 JsonNode node = mapper.readTree(jsonStr);
\r
119 List<String> attrList = new ArrayList<String>();
\r
120 attrList.add("key1");
\r
121 List<String> valueList = new ArrayList<String>();
\r
122 RouterServiceUtil.extractFieldValuesFromObject(node,attrList,valueList);
\r
123 Assert.assertTrue(valueList.size()==1);
\r
124 Assert.assertTrue(valueList.get(0).equals("value1"));
\r
128 public void testObjToJson() throws IOException {
\r
129 String jsonStr = "{\"key1\":\"value1\"}";
\r
130 ObjectMapper mapper = new ObjectMapper();
\r
131 Object node = mapper.readTree(jsonStr);
\r
132 String retJson = RouterServiceUtil.objToJson(node);
\r
133 Assert.assertNotNull("localhost/aai/data-router/");
\r
137 public void testConcatSubUri(){
\r
138 String finalSubUri = RouterServiceUtil.concatSubUri("localhost/","aai/","data-router");
\r
139 Assert.assertEquals(finalSubUri, "localhost/aai/data-router/");
\r