2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.resmanagement.common.util;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
26 import org.junit.Test;
27 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
31 import net.sf.json.JSONArray;
32 import net.sf.json.JSONObject;
34 public class JsonUtilTest {
37 public void testGetJsonFieldStr() {
38 JSONObject jsonObj = new JSONObject();
39 String fieldName = "a";
40 jsonObj.put("a", "1");
41 jsonObj.put("b", "2");
42 String result = JsonUtil.getJsonFieldStr(jsonObj, fieldName);
43 String expectedResult = "1";
44 assertEquals(expectedResult, result);
49 public void testGetJsonFieldInt() {
50 JSONObject jsonObj = new JSONObject();
51 String fieldName = "a";
52 jsonObj.put("a", "1");
53 jsonObj.put("b", "2");
54 int result = JsonUtil.getJsonFieldInt(jsonObj, fieldName);
55 int expectedResult = 1;
56 assertEquals(expectedResult, result);
61 public void testGetJsonFieldArr() {
62 JSONObject jsonObj = new JSONObject();
63 String fieldName = "a";
64 jsonObj.put("a", new JSONArray());
65 jsonObj.put("b", "2");
66 JSONArray result = JsonUtil.getJsonFieldArr(jsonObj, fieldName);
67 JSONArray expectedResult = new JSONArray();
68 assertEquals(expectedResult, result);
73 public void testGetJsonFieldJson() {
74 JSONObject jsonObj = new JSONObject();
75 String fieldName = "a";
76 jsonObj.put("a", new JSONObject());
77 jsonObj.put("b", "2");
78 JSONObject result = JsonUtil.getJsonFieldJson(jsonObj, fieldName);
79 JSONObject expectedResult = new JSONObject();
80 assertEquals(expectedResult, result);
85 public void testGetJsonFieldLong() {
86 JSONObject jsonObj = new JSONObject();
87 String fieldName = "a";
90 Long result = JsonUtil.getJsonFieldLong(jsonObj, fieldName);
91 Long expectedResult = new Long(1);
92 assertEquals(expectedResult, result);
97 public void testGetJsonFieldObjectException() {
98 JSONObject jsonObj = new JSONObject();
99 String fieldName = "a";
100 jsonObj.put("a", "1");
101 jsonObj.put("b", "2");
102 JSONObject result = JsonUtil.getJsonFieldJson(jsonObj, fieldName);
103 JSONObject expectedResult = null;
104 assertEquals(expectedResult, result);
109 public void testIsNullJson1() {
110 JSONObject jsonObj = new JSONObject();
111 assertTrue(JsonUtil.isNullJson(jsonObj));
115 public void testIsNullJson2() {
116 assertTrue(JsonUtil.isNullJson(null));
120 public void testIsNullJson3() {
121 JSONObject jsonObj = new JSONObject();
122 jsonObj.put("a", "1");
123 assertFalse(JsonUtil.isNullJson(jsonObj));
127 public void testGetStrValueByjsonNULL() {
128 JSONObject jsonObj = new JSONObject();
130 String result = JsonUtil.getStrValueByjson(jsonObj, key);
131 String expectedResult = null;
132 assertEquals(expectedResult, result);
137 public void testGetStrValueByjson() {
138 JSONObject jsonObj = new JSONObject();
140 jsonObj.put("a", "1");
141 jsonObj.put("b", "2");
142 new MockUp<JSONObject>() {
144 @SuppressWarnings("static-access")
146 public JSONObject optJSONObject(String key) {
147 return new JSONObject().fromObject("{\"a\":\"1\"}");
151 public JSONObject getJSONObject(String key) {
152 return new JSONObject();
155 String result = JsonUtil.getStrValueByjson(jsonObj, key);
156 String expectedResult = "1";
157 assertEquals(expectedResult, result);
162 public void testGetStrValueByjson1() {
163 JSONObject jsonObj = new JSONObject();
165 jsonObj.put("a", "1");
166 jsonObj.put("b", "2");
167 new MockUp<JSONObject>() {
170 public JSONObject optJSONObject(String key) {
174 @SuppressWarnings("static-access")
176 public JSONArray optJSONArray(String key) {
177 return new JSONArray().fromObject("[\"a\",\"1\"]");
181 public JSONArray getJSONArray(String key) {
182 return new JSONArray();
185 String result = JsonUtil.getStrValueByjson(jsonObj, key);
186 String expectedResult = "1";
187 assertEquals(expectedResult, result);
192 public void testGetStrValueByjson2() {
193 JSONObject jsonObj = new JSONObject();
195 jsonObj.put("a", "1");
196 jsonObj.put("b", "2");
197 String result = JsonUtil.getStrValueByjson(jsonObj, key);
198 String expectedResult = "1";
199 assertEquals(expectedResult, result);
204 public void testGetStrValueByJArray() {
205 JSONObject jsonObj = new JSONObject();
207 jsonObj.put("a", "1");
208 jsonObj.put("b", "2");
209 new MockUp<JSONObject>() {
212 public JSONObject optJSONObject(String key) {
216 @SuppressWarnings("static-access")
218 public JSONArray optJSONArray(String key) {
219 return new JSONArray().fromObject("[\"a\",\"1\"]");
222 @SuppressWarnings("static-access")
224 public JSONArray getJSONArray(String key) {
225 return new JSONArray().fromObject("[\"a\",\"1\"]");
231 String result = JsonUtil.getStrValueByjson(jsonObj, key);
236 public void testGetStrValueByJArray1() {
237 JSONObject jsonObj = new JSONObject();
239 jsonObj.put("a", "1");
240 jsonObj.put("b", "2");
241 new MockUp<JSONObject>() {
245 @SuppressWarnings("static-access")
247 public JSONObject optJSONObject(String key) {
252 return new JSONObject().fromObject("{\"a\":\"1\"}");
255 @SuppressWarnings("static-access")
257 public JSONArray optJSONArray(String key) {
258 return new JSONArray().fromObject("[\"a\",\"1\"]");
261 @SuppressWarnings("static-access")
263 public JSONArray getJSONArray(String key) {
264 return new JSONArray().fromObject("[\"a\",\"1\"]");
267 String result = JsonUtil.getStrValueByjson(jsonObj, key);
268 String expectedResult = "1";
269 assertEquals(expectedResult, result);
274 public void testGetJsonValueByjson() {
275 JSONObject jsonObj = new JSONObject();
277 jsonObj.put("a", "1");
278 jsonObj.put("b", "2");
279 String result = JsonUtil.getJsonValueByjson(jsonObj, key).toString();
280 String expectedResult = "{\"a\":\"1\"}";
281 assertEquals(expectedResult, result);
285 public void testGetJsonValueByjsonResultIsNull() {
286 JSONObject jsonObj = new JSONObject();
288 jsonObj.put("a", "1");
289 jsonObj.put("b", "2");
290 JSONObject result = JsonUtil.getJsonValueByjson(jsonObj, key);
291 String expectedResult = null;
292 assertEquals(expectedResult, result);
296 public void testGetStrValueByJsonParentKeyIsNull() {
297 JSONObject jsonObj = new JSONObject();
299 jsonObj.put("a", "1");
300 jsonObj.put("b", "2");
301 String parentKey = "";
302 String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
303 String expectedResult = "1";
304 assertEquals(expectedResult, result);
308 public void testGetStrValueByJsonParentJsonIsNull() {
309 JSONObject jsonObj = new JSONObject();
311 jsonObj.put("a", "1");
312 jsonObj.put("b", "2");
313 String parentKey = "b";
314 new MockUp<JsonUtil>() {
317 public JSONObject getJsonValueByjson(JSONObject json, String key) {
318 return new JSONObject();
321 String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
322 String expectedResult = null;
323 assertEquals(expectedResult, result);
327 public void testGetStrValueByJson() {
328 JSONObject jsonObj = new JSONObject();
330 jsonObj.put("a", "1");
331 jsonObj.put("b", "2");
332 String parentKey = "b";
333 String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
334 String expectedResult = null;
335 assertEquals(expectedResult, result);
339 public void testGetResponseDataRetcodeError1() {
340 new MockUp<JsonUtil>() {
343 public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
347 JSONObject result = JsonUtil.getResponseData(null);
348 String expectedResult = null;
349 assertEquals(expectedResult, result);
353 public void testGetResponseDataRetcodeError2() {
354 new MockUp<JsonUtil>() {
357 public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
361 JSONObject result = JsonUtil.getResponseData(null);
362 String expectedResult = null;
363 assertEquals(expectedResult, result);
367 public void testGetResponseDataResultIsEmpty() {
368 JSONObject obj = new JSONObject();
369 obj.put("data", "1");
370 obj.put("retCode", "1");
371 JSONObject result = JsonUtil.getResponseData(obj);
372 String expectedResult = null;
373 assertEquals(expectedResult, result);
377 public void testGetResponseData() {
378 JSONObject obj = new JSONObject();
379 obj.put("data", new JSONObject());
380 obj.put("retCode", "1");
381 new MockUp<JSONObject>() {
383 @SuppressWarnings("static-access")
385 public JSONObject optJSONObject(String key) {
386 return new JSONObject().fromObject("{\"a\":\"1\"}");
389 JSONObject result = JsonUtil.getResponseData(obj);
390 String expectedResult = null;
391 assertEquals(expectedResult, result);
395 public void testGetResponseData1() {
396 JSONObject obj = new JSONObject();
397 obj.put("data", JSONArray.fromObject("[{\"a\":\"1\"},\"1\"]"));
398 obj.put("retCode", "1");
399 new MockUp<JSONObject>() {
402 public JSONObject optJSONObject(String key) {
406 @SuppressWarnings("static-access")
408 public JSONArray optJSONArray(String key) {
409 return new JSONArray().fromObject("[\"a\",\"1\"]");
412 JSONObject result = JsonUtil.getResponseData(obj);
413 String expectedResult = "{\"a\":\"1\"}";
414 assertEquals(expectedResult, result.toString());
418 public void testGetResponseData2() {
419 JSONObject obj = new JSONObject();
420 JSONObject json = new JSONObject();
421 json.put("retCode", "1");
422 obj.put("data", json);
423 obj.put("retCode", "1");
424 JSONObject result = JsonUtil.getResponseData(obj);
425 String expectedResult = null;
426 assertEquals(expectedResult, result);
429 public void testPrivateConstructor() throws Exception {
430 Constructor constructor = JsonUtil.class.getDeclaredConstructor();
431 assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
433 constructor.setAccessible(true);
434 constructor.newInstance();