2 * ============LICENSE_START=======================================================
\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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 package org.onap.aai.datarouter.util;
\r
25 import com.fasterxml.jackson.databind.JsonNode;
\r
26 import com.fasterxml.jackson.databind.ObjectMapper;
\r
27 import org.junit.Assert;
\r
28 import org.junit.Test;
\r
30 import javax.json.Json;
\r
31 import java.io.IOException;
\r
32 import java.util.ArrayList;
\r
33 import java.util.List;
\r
34 import java.util.Map;
\r
36 public class RouterServiceUtilTest {
\r
39 public void testParseJsonPayloadIntoMap(){
\r
40 String jsonStr = "{\"key1\":\"value1\", \"key2\":\"value2\", \"key3\":\"value3\"}";
\r
41 Map<String, String> retMap = RouterServiceUtil.parseJsonPayloadIntoMap(jsonStr);
\r
42 Assert.assertNotNull(retMap);
\r
43 Assert.assertEquals(retMap.get("key1"), "value1");
\r
47 public void testGetNodeFieldAsText() throws IOException {
\r
48 String jsonStr = "{\"key\":\"value\"}";
\r
49 ObjectMapper mapper = new ObjectMapper();
\r
50 JsonNode node = mapper.readTree(jsonStr);
\r
51 String field = RouterServiceUtil.getNodeFieldAsText(node, "key");
\r
55 public void testConcatArray_NullList(){
\r
56 List<String> strList = null;
\r
57 String result = RouterServiceUtil.concatArray(strList);
\r
58 Assert.assertTrue(result.isEmpty());
\r
61 /*Method in RouterServiceUtil is wrong. It should be corrected
\r
63 public void testConcatArray_NotNullList(){
\r
64 List<String> strList = new ArrayList<String>();
\r
65 strList.add("str1");
\r
66 strList.add("str2");
\r
67 strList.add("str3");
\r
68 String result = RouterServiceUtil.concatArray(strList);
\r
69 Assert.assertEquals(result, "str1 str2 str3");
\r
73 public void testConcatArray_NullArray(){
\r
74 String[] values = new String[]{};
\r
75 //String[] values = new String[]{"value1", "value2", "value3"};
\r
76 String result = RouterServiceUtil.concatArray(values);
\r
77 Assert.assertTrue(result.isEmpty());
\r
81 public void testConcatArray_NotNullArray(){
\r
82 String[] values = new String[]{"value1", "value2", "value3"};
\r
83 String result = RouterServiceUtil.concatArray(values);
\r
84 Assert.assertEquals(result, "value1.value2.value3");
\r
88 public void testRecursivelyLookupJsonPayload_JsonArray() throws IOException {
\r
89 String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]";
\r
90 ObjectMapper mapper = new ObjectMapper();
\r
91 JsonNode node = mapper.readTree(jsonStr);
\r
92 String result = RouterServiceUtil.recursivelyLookupJsonPayload(node, "key1");
\r
93 Assert.assertEquals(result, "value1");
\r
97 public void testExtractObjectsByKey() throws IOException {
\r
98 String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]";
\r
99 ObjectMapper mapper = new ObjectMapper();
\r
100 JsonNode node = mapper.readTree(jsonStr);
\r
101 List<JsonNode> nodeList = new ArrayList<JsonNode>();
\r
102 RouterServiceUtil.extractObjectsByKey(node, "key1", nodeList);
\r
103 Assert.assertTrue(nodeList.size() == 1);
\r
107 public void testConvertArrayIntoList() throws IOException {
\r
108 String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]";
\r
109 ObjectMapper mapper = new ObjectMapper();
\r
110 JsonNode node = mapper.readTree(jsonStr);
\r
111 List<JsonNode> nodeList = new ArrayList<JsonNode>();
\r
112 RouterServiceUtil.convertArrayIntoList(node, nodeList);
\r
113 Assert.assertTrue(nodeList.size() == 2);
\r
117 public void testExtractFieldValueFromObject() throws IOException {
\r
118 String jsonStr = "{\"key1\":\"value1\"}";
\r
119 ObjectMapper mapper = new ObjectMapper();
\r
120 JsonNode node = mapper.readTree(jsonStr);
\r
121 List<String> attrList = new ArrayList<String>();
\r
122 attrList.add("key1");
\r
123 List<String> valueList = new ArrayList<String>();
\r
124 RouterServiceUtil.extractFieldValuesFromObject(node,attrList,valueList);
\r
125 Assert.assertTrue(valueList.size()==1);
\r
126 Assert.assertTrue(valueList.get(0).equals("value1"));
\r
130 public void testObjToJson() throws IOException {
\r
131 String jsonStr = "{\"key1\":\"value1\"}";
\r
132 ObjectMapper mapper = new ObjectMapper();
\r
133 Object node = mapper.readTree(jsonStr);
\r
134 String retJson = RouterServiceUtil.objToJson(node);
\r
135 Assert.assertNotNull("localhost/aai/data-router/");
\r
139 public void testConcatSubUri(){
\r
140 String finalSubUri = RouterServiceUtil.concatSubUri("localhost/","aai/","data-router");
\r
141 Assert.assertEquals(finalSubUri, "localhost/aai/data-router/");
\r