2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.core.json;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.List;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.engine.runtime.Execution;
35 import org.json.JSONArray;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.onap.so.exceptions.ValidationException;
44 public class JsonUtilsTest {
46 @Mock public DelegateExecution execution;
47 @Mock public Execution mockEexecution;
48 private final String fileLocation = "src/test/resources/json-examples/";
51 public ExpectedException expectedException = ExpectedException.none();
54 public void jsonStringToMapTest() throws IOException {
56 JsonUtils utils = new JsonUtils();
57 String response = this.getJson("SDNCServiceResponseExample.json");
58 String entry = JsonUtils.getJsonValue(response, "SDNCServiceResponse.params");
59 Map<String, String> map = utils.jsonStringToMap(execution, entry);
60 assertEquals(map.get("e2e-vpn-key"), "my-key");
64 public void entryArrayToMapTest() throws IOException {
65 JsonUtils utils = new JsonUtils();
66 String response = this.getJson("SNIROExample.json");
67 String entry = JsonUtils.getJsonValue(response, "solutionInfo.placementInfo");
68 JSONArray arr = new JSONArray(entry);
69 JSONObject homingDataJson = arr.getJSONObject(0);
70 JSONArray assignmentInfo = homingDataJson.getJSONArray("assignmentInfo");
71 Map<String, String> map = utils.entryArrayToMap(execution, assignmentInfo.toString(), "variableName", "variableValue");
72 assertEquals(map.get("cloudOwner"), "att-aic");
75 public void entryArrayToMapStringTest() throws IOException {
76 JsonUtils utils = new JsonUtils();
77 String response = this.getJson("SNIROExample.json");
78 String entry = JsonUtils.getJsonValue(response, "solutionInfo.placementInfo");
79 JSONArray arr = new JSONArray(entry);
80 JSONObject homingDataJson = arr.getJSONObject(0);
81 JSONArray assignmentInfo = homingDataJson.getJSONArray("assignmentInfo");
82 Map<String, String> map = utils.entryArrayToMap(assignmentInfo.toString(), "variableName", "variableValue");
83 assertEquals(map.get("cloudOwner"), "att-aic");
86 public void getJsonRootPropertyTest() throws IOException {
87 String response = this.getJson("SDNCServiceResponseExample.json");
88 assertEquals("SDNCServiceResponse",JsonUtils.getJsonRootProperty(response));
91 public void getJsonRootProperty_ExceptionTest() throws IOException {
92 expectedException.expect(JSONException.class);
93 String response = this.getJson("SNIROExample.json");
94 String root = JsonUtils.getJsonRootProperty(response);
97 public void getJsonNodeValueTest() throws IOException {
98 String response = this.getJson("SDNCServiceResponseExample.json");
99 String code = JsonUtils.getJsonNodeValue(response,"SDNCServiceResponse.responseCode");
100 assertEquals("200", code);
103 public void stringArrayToList_jsonStringTest() throws IOException {
104 String response = this.getJson("SNIROExample.json");
105 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
106 JsonUtils utils = new JsonUtils();
107 List<String> listString = utils.StringArrayToList(licenseInfo);
108 assertNotNull(listString.get(0));
111 public void stringArrayToList_JSONArrayTest() throws IOException {
112 String response = this.getJson("SNIROExample.json");
113 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
114 JSONArray jsonArray = new JSONArray(licenseInfo);
115 JsonUtils utils = new JsonUtils();
116 List<String> listString = utils.StringArrayToList(jsonArray);
117 assertNotNull(listString.get(0));
120 public void stringArrayToList_withExecutionTest() throws IOException {
121 String response = this.getJson("SNIROExample.json");
122 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
123 JsonUtils utils = new JsonUtils();
124 List<String> listString = utils.StringArrayToList(mockEexecution, licenseInfo);
125 assertNotNull(listString.get(0));
128 public void jsonElementExist_trueTest() throws IOException {
129 String response = this.getJson("SDNCServiceResponseExample.json");
130 boolean isExist = JsonUtils.jsonElementExist(response,"SDNCServiceResponse.responseCode");
131 assertEquals(true, isExist);
134 public void jsonElementExist_falseTest() throws IOException {
135 String response = this.getJson("SDNCServiceResponseExample.json");
136 boolean isExist = JsonUtils.jsonElementExist(response,"SDNCServiceResponse.responseX");
137 assertEquals(false, isExist);
140 public void jsonElementExist_NullTest() throws IOException {
141 String response = this.getJson("SDNCServiceResponseExample.json");
142 boolean isExist = JsonUtils.jsonElementExist(response, null);
143 assertEquals(true, isExist);
146 public void jsonSchemaValidation_ExceptionTest() throws IOException, ValidationException {
147 expectedException.expect(ValidationException.class);
148 String response = this.getJson("SDNCServiceResponseExample.json");
149 String isExist = JsonUtils.jsonSchemaValidation(response, fileLocation);
152 public void getJsonIntValueTest() throws IOException {
153 String response = this.getJson("SDNCServiceResponseExample.json");
154 int intValue = JsonUtils.getJsonIntValue(response,"SDNCServiceResponse.responseCode");
155 assertEquals(0, intValue);
158 public void getJsonBooleanValueTest() throws IOException {
159 String response = this.getJson("SDNCServiceResponseExample.json");
160 boolean isBoolean = JsonUtils.getJsonBooleanValue(response,"SDNCServiceResponse.responseCode");
161 assertEquals(false, isBoolean);
164 public void prettyJsonTest() throws IOException {
165 String response = this.getJson("SNIROExample.json");
166 assertNotNull(JsonUtils.prettyJson(response));
167 String malformedJson = "{\"name\" \"myName\"}";
168 assertNull(JsonUtils.prettyJson(malformedJson));
171 public void xml2jsonTest() throws IOException {
172 String expectedJson = "{\"name\":\"myName\"}";
173 String xml = "<name>myName</name>";
174 assertEquals(expectedJson, JsonUtils.xml2json(xml,false));
177 public void xml2jsonErrorTest() throws IOException {
178 String malformedXml = "<name>myName<name>";
179 assertNull(JsonUtils.xml2json(malformedXml));
182 public void json2xmlTest() throws IOException {
183 String expectedXml = "<name>myName</name>";
184 String malformedJson = "{\"name\":\"myName\"}";
185 assertEquals(expectedXml, JsonUtils.json2xml(malformedJson, false));
188 public void json2xmlErrorTest() throws IOException {
189 String malformedJson = "{\"name\" \"myName\"}";
190 assertNull(JsonUtils.json2xml(malformedJson));
193 public void getJsonValueErrorTest() throws IOException {
194 String response = this.getJson("SDNCServiceResponseExample.json");
195 assertNull(JsonUtils.getJsonValue(response,null));
198 public void getJsonNodeValueErrorTest() throws IOException {
199 String response = this.getJson("SDNCServiceResponseExample.json");
200 assertNull(JsonUtils.getJsonNodeValue(response,null));
203 public void getJsonIntValueErrorTest() throws IOException {
204 String response = this.getJson("SDNCServiceResponseExample.json");
205 assertEquals(0, JsonUtils.getJsonIntValue(response,null));
208 public void getJsonBooleanValueErrorTest() throws IOException {
209 String response = this.getJson("SDNCServiceResponseExample.json");
210 assertEquals(false, JsonUtils.getJsonBooleanValue(response,null));
213 public void getJsonValueForKeyErrorTest() throws IOException {
214 String malformedJson = "{\"name\" \"myName\"}";
215 assertNull(JsonUtils.getJsonValueForKey(malformedJson, "name"));
218 public void updJsonValueTest() throws IOException {
219 String expectedJson = "{\"name\": \"yourName\"}";
220 String json = "{\"name\":\"myName\"}";
221 assertEquals(expectedJson, JsonUtils.updJsonValue(json, "name", "yourName"));
224 public void updJsonValueErrorTest() throws IOException {
225 String expectedJson = "{\"name\" \"myName\"}";
226 String json = "{\"name\" \"myName\"}";
227 assertEquals(expectedJson, JsonUtils.updJsonValue(json, "name", "yourName"));
230 private String getJson(String filename) throws IOException {
231 return new String(Files.readAllBytes(Paths.get(fileLocation + filename)));