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"), "CloudOwner");
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"), "CloudOwner");
86 public void entryArrayToMapStringTestOof() throws IOException {
87 JsonUtils utils = new JsonUtils();
88 String response = this.getJson("OofExample.json");
89 String entry = JsonUtils.getJsonValue(response, "solutions.placementSolutions");
90 JSONArray arr = new JSONArray(entry);
91 JSONArray arr2 = arr.getJSONArray(0);
92 JSONObject homingDataJson = arr2.getJSONObject(0);
93 JSONArray assignmentInfo = homingDataJson.getJSONArray("assignmentInfo");
94 Map<String, String> map = utils.entryArrayToMap(assignmentInfo.toString(), "key", "value");
95 assertEquals(map.get("cloudOwner"), "HPA-cloud");
98 public void getJsonRootPropertyTest() throws IOException {
99 String response = this.getJson("SDNCServiceResponseExample.json");
100 assertEquals("SDNCServiceResponse",JsonUtils.getJsonRootProperty(response));
103 public void getJsonRootProperty_ExceptionTest() throws IOException {
104 expectedException.expect(JSONException.class);
105 String response = this.getJson("SNIROExample.json");
106 String root = JsonUtils.getJsonRootProperty(response);
109 public void getJsonNodeValueTest() throws IOException {
110 String response = this.getJson("SDNCServiceResponseExample.json");
111 String code = JsonUtils.getJsonNodeValue(response,"SDNCServiceResponse.responseCode");
112 assertEquals("200", code);
115 public void stringArrayToList_jsonStringTest() throws IOException {
116 String response = this.getJson("SNIROExample.json");
117 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
118 JsonUtils utils = new JsonUtils();
119 List<String> listString = utils.StringArrayToList(licenseInfo);
120 assertNotNull(listString.get(0));
123 public void stringArrayToList_JSONArrayTest() throws IOException {
124 String response = this.getJson("SNIROExample.json");
125 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
126 JSONArray jsonArray = new JSONArray(licenseInfo);
127 JsonUtils utils = new JsonUtils();
128 List<String> listString = utils.StringArrayToList(jsonArray);
129 assertNotNull(listString.get(0));
132 public void stringArrayToList_withExecutionTest() throws IOException {
133 String response = this.getJson("SNIROExample.json");
134 String licenseInfo = JsonUtils.getJsonNodeValue(response,"solutionInfo.licenseInfo");
135 JsonUtils utils = new JsonUtils();
136 List<String> listString = utils.StringArrayToList(mockEexecution, licenseInfo);
137 assertNotNull(listString.get(0));
140 public void jsonElementExist_trueTest() throws IOException {
141 String response = this.getJson("SDNCServiceResponseExample.json");
142 boolean isExist = JsonUtils.jsonElementExist(response,"SDNCServiceResponse.responseCode");
143 assertEquals(true, isExist);
146 public void jsonElementExist_falseTest() throws IOException {
147 String response = this.getJson("SDNCServiceResponseExample.json");
148 boolean isExist = JsonUtils.jsonElementExist(response,"SDNCServiceResponse.responseX");
149 assertEquals(false, isExist);
152 public void jsonElementExist_NullTest() throws IOException {
153 String response = this.getJson("SDNCServiceResponseExample.json");
154 boolean isExist = JsonUtils.jsonElementExist(response, null);
155 assertEquals(true, isExist);
158 public void jsonSchemaValidation_ExceptionTest() throws IOException, ValidationException {
159 expectedException.expect(ValidationException.class);
160 String response = this.getJson("SDNCServiceResponseExample.json");
161 String isExist = JsonUtils.jsonSchemaValidation(response, fileLocation);
164 public void getJsonIntValueTest() throws IOException {
165 String response = this.getJson("SDNCServiceResponseExample.json");
166 int intValue = JsonUtils.getJsonIntValue(response,"SDNCServiceResponse.responseCode");
167 assertEquals(0, intValue);
170 public void getJsonBooleanValueTest() throws IOException {
171 String response = this.getJson("SDNCServiceResponseExample.json");
172 boolean isBoolean = JsonUtils.getJsonBooleanValue(response,"SDNCServiceResponse.responseCode");
173 assertEquals(false, isBoolean);
176 public void prettyJsonTest() throws IOException {
177 String response = this.getJson("SNIROExample.json");
178 assertNotNull(JsonUtils.prettyJson(response));
179 String malformedJson = "{\"name\" \"myName\"}";
180 assertNull(JsonUtils.prettyJson(malformedJson));
183 public void xml2jsonTest() throws IOException {
184 String expectedJson = "{\"name\":\"myName\"}";
185 String xml = "<name>myName</name>";
186 assertEquals(expectedJson, JsonUtils.xml2json(xml,false));
189 public void xml2jsonErrorTest() throws IOException {
190 String malformedXml = "<name>myName<name>";
191 assertNull(JsonUtils.xml2json(malformedXml));
194 public void json2xmlTest() throws IOException {
195 String expectedXml = "<name>myName</name>";
196 String malformedJson = "{\"name\":\"myName\"}";
197 assertEquals(expectedXml, JsonUtils.json2xml(malformedJson, false));
200 public void json2xmlErrorTest() throws IOException {
201 String malformedJson = "{\"name\" \"myName\"}";
202 assertNull(JsonUtils.json2xml(malformedJson));
205 public void getJsonValueErrorTest() throws IOException {
206 String response = this.getJson("SDNCServiceResponseExample.json");
207 assertNull(JsonUtils.getJsonValue(response,null));
210 public void getJsonNodeValueErrorTest() throws IOException {
211 String response = this.getJson("SDNCServiceResponseExample.json");
212 assertNull(JsonUtils.getJsonNodeValue(response,null));
215 public void getJsonIntValueErrorTest() throws IOException {
216 String response = this.getJson("SDNCServiceResponseExample.json");
217 assertEquals(0, JsonUtils.getJsonIntValue(response,null));
220 public void getJsonBooleanValueErrorTest() throws IOException {
221 String response = this.getJson("SDNCServiceResponseExample.json");
222 assertEquals(false, JsonUtils.getJsonBooleanValue(response,null));
225 public void getJsonValueForKeyErrorTest() throws IOException {
226 String malformedJson = "{\"name\" \"myName\"}";
227 assertNull(JsonUtils.getJsonValueForKey(malformedJson, "name"));
230 public void updJsonValueTest() throws IOException {
231 String expectedJson = "{\"name\": \"yourName\"}";
232 String json = "{\"name\":\"myName\"}";
233 assertEquals(expectedJson, JsonUtils.updJsonValue(json, "name", "yourName"));
236 public void updJsonValueErrorTest() throws IOException {
237 String expectedJson = "{\"name\" \"myName\"}";
238 String json = "{\"name\" \"myName\"}";
239 assertEquals(expectedJson, JsonUtils.updJsonValue(json, "name", "yourName"));
242 private String getJson(String filename) throws IOException {
243 return new String(Files.readAllBytes(Paths.get(fileLocation + filename)));