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.openo.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;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
33 public class JsonUtilTest {
36 public void testGetJsonFieldStr() {
37 JSONObject jsonObj = new JSONObject();
38 String fieldName = "a";
39 jsonObj.put("a", "1");
40 jsonObj.put("b", "2");
41 String result = JsonUtil.getJsonFieldStr(jsonObj, fieldName);
42 String expectedResult = "1";
43 assertEquals(expectedResult, result);
48 public void testGetJsonFieldInt() {
49 JSONObject jsonObj = new JSONObject();
50 String fieldName = "a";
51 jsonObj.put("a", "1");
52 jsonObj.put("b", "2");
53 int result = JsonUtil.getJsonFieldInt(jsonObj, fieldName);
54 int expectedResult = 1;
55 assertEquals(expectedResult, result);
60 public void testGetJsonFieldArr() {
61 JSONObject jsonObj = new JSONObject();
62 String fieldName = "a";
63 jsonObj.put("a", new JSONArray());
64 jsonObj.put("b", "2");
65 JSONArray result = JsonUtil.getJsonFieldArr(jsonObj, fieldName);
66 JSONArray expectedResult = new JSONArray();
67 assertEquals(expectedResult, result);
72 public void testGetJsonFieldJson() {
73 JSONObject jsonObj = new JSONObject();
74 String fieldName = "a";
75 jsonObj.put("a", new JSONObject());
76 jsonObj.put("b", "2");
77 JSONObject result = JsonUtil.getJsonFieldJson(jsonObj, fieldName);
78 JSONObject expectedResult = new JSONObject();
79 assertEquals(expectedResult, result);
84 public void testGetJsonFieldLong() {
85 JSONObject jsonObj = new JSONObject();
86 String fieldName = "a";
89 Long result = JsonUtil.getJsonFieldLong(jsonObj, fieldName);
90 Long expectedResult = new Long(1);
91 assertEquals(expectedResult, result);
96 public void testGetJsonFieldObjectException() {
97 JSONObject jsonObj = new JSONObject();
98 String fieldName = "a";
99 jsonObj.put("a", "1");
100 jsonObj.put("b", "2");
101 JSONObject result = JsonUtil.getJsonFieldJson(jsonObj, fieldName);
102 JSONObject expectedResult = null;
103 assertEquals(expectedResult, result);
108 public void testIsNullJson1() {
109 JSONObject jsonObj = new JSONObject();
110 assertTrue(JsonUtil.isNullJson(jsonObj));
114 public void testIsNullJson2() {
115 assertTrue(JsonUtil.isNullJson(null));
119 public void testIsNullJson3() {
120 JSONObject jsonObj = new JSONObject();
121 jsonObj.put("a", "1");
122 assertFalse(JsonUtil.isNullJson(jsonObj));
126 public void testGetStrValueByjsonNULL() {
127 JSONObject jsonObj = new JSONObject();
129 String result = JsonUtil.getStrValueByjson(jsonObj, key);
130 String expectedResult = null;
131 assertEquals(expectedResult, result);
136 public void testGetStrValueByjson() {
137 JSONObject jsonObj = new JSONObject();
139 jsonObj.put("a", "1");
140 jsonObj.put("b", "2");
141 new MockUp<JSONObject>() {
143 @SuppressWarnings("static-access")
145 public JSONObject optJSONObject(String key) {
146 return new JSONObject().fromObject("{\"a\":\"1\"}");
150 public JSONObject getJSONObject(String key) {
151 return new JSONObject();
154 String result = JsonUtil.getStrValueByjson(jsonObj, key);
155 String expectedResult = "1";
156 assertEquals(expectedResult, result);
161 public void testGetStrValueByjson1() {
162 JSONObject jsonObj = new JSONObject();
164 jsonObj.put("a", "1");
165 jsonObj.put("b", "2");
166 new MockUp<JSONObject>() {
169 public JSONObject optJSONObject(String key) {
173 @SuppressWarnings("static-access")
175 public JSONArray optJSONArray(String key) {
176 return new JSONArray().fromObject("[\"a\",\"1\"]");
180 public JSONArray getJSONArray(String key) {
181 return new JSONArray();
184 String result = JsonUtil.getStrValueByjson(jsonObj, key);
185 String expectedResult = "1";
186 assertEquals(expectedResult, result);
191 public void testGetStrValueByjson2() {
192 JSONObject jsonObj = new JSONObject();
194 jsonObj.put("a", "1");
195 jsonObj.put("b", "2");
196 String result = JsonUtil.getStrValueByjson(jsonObj, key);
197 String expectedResult = "1";
198 assertEquals(expectedResult, result);
203 public void testGetStrValueByJArray() {
204 JSONObject jsonObj = new JSONObject();
206 jsonObj.put("a", "1");
207 jsonObj.put("b", "2");
208 new MockUp<JSONObject>() {
211 public JSONObject optJSONObject(String key) {
215 @SuppressWarnings("static-access")
217 public JSONArray optJSONArray(String key) {
218 return new JSONArray().fromObject("[\"a\",\"1\"]");
221 @SuppressWarnings("static-access")
223 public JSONArray getJSONArray(String key) {
224 return new JSONArray().fromObject("[\"a\",\"1\"]");
230 String result = JsonUtil.getStrValueByjson(jsonObj, key);
235 public void testGetStrValueByJArray1() {
236 JSONObject jsonObj = new JSONObject();
238 jsonObj.put("a", "1");
239 jsonObj.put("b", "2");
240 new MockUp<JSONObject>() {
244 @SuppressWarnings("static-access")
246 public JSONObject optJSONObject(String key) {
251 return new JSONObject().fromObject("{\"a\":\"1\"}");
254 @SuppressWarnings("static-access")
256 public JSONArray optJSONArray(String key) {
257 return new JSONArray().fromObject("[\"a\",\"1\"]");
260 @SuppressWarnings("static-access")
262 public JSONArray getJSONArray(String key) {
263 return new JSONArray().fromObject("[\"a\",\"1\"]");
266 String result = JsonUtil.getStrValueByjson(jsonObj, key);
267 String expectedResult = "1";
268 assertEquals(expectedResult, result);
273 public void testGetJsonValueByjson() {
274 JSONObject jsonObj = new JSONObject();
276 jsonObj.put("a", "1");
277 jsonObj.put("b", "2");
278 String result = JsonUtil.getJsonValueByjson(jsonObj, key).toString();
279 String expectedResult = "{\"a\":\"1\"}";
280 assertEquals(expectedResult, result);
284 public void testGetJsonValueByjsonResultIsNull() {
285 JSONObject jsonObj = new JSONObject();
287 jsonObj.put("a", "1");
288 jsonObj.put("b", "2");
289 JSONObject result = JsonUtil.getJsonValueByjson(jsonObj, key);
290 String expectedResult = null;
291 assertEquals(expectedResult, result);
295 public void testGetStrValueByJsonParentKeyIsNull() {
296 JSONObject jsonObj = new JSONObject();
298 jsonObj.put("a", "1");
299 jsonObj.put("b", "2");
300 String parentKey = "";
301 String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
302 String expectedResult = "1";
303 assertEquals(expectedResult, result);
307 public void testGetStrValueByJsonParentJsonIsNull() {
308 JSONObject jsonObj = new JSONObject();
310 jsonObj.put("a", "1");
311 jsonObj.put("b", "2");
312 String parentKey = "b";
313 new MockUp<JsonUtil>() {
316 public JSONObject getJsonValueByjson(JSONObject json, String key) {
317 return new JSONObject();
320 String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
321 String expectedResult = null;
322 assertEquals(expectedResult, result);
326 public void testGetStrValueByJson() {
327 JSONObject jsonObj = new JSONObject();
329 jsonObj.put("a", "1");
330 jsonObj.put("b", "2");
331 String parentKey = "b";
332 String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
333 String expectedResult = null;
334 assertEquals(expectedResult, result);
338 public void testGetResponseDataRetcodeError1() {
339 new MockUp<JsonUtil>() {
342 public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
346 JSONObject result = JsonUtil.getResponseData(null);
347 String expectedResult = null;
348 assertEquals(expectedResult, result);
352 public void testGetResponseDataRetcodeError2() {
353 new MockUp<JsonUtil>() {
356 public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
360 JSONObject result = JsonUtil.getResponseData(null);
361 String expectedResult = null;
362 assertEquals(expectedResult, result);
366 public void testGetResponseDataResultIsEmpty() {
367 JSONObject obj = new JSONObject();
368 obj.put("data", "1");
369 obj.put("retCode", "1");
370 JSONObject result = JsonUtil.getResponseData(obj);
371 String expectedResult = null;
372 assertEquals(expectedResult, result);
376 public void testGetResponseData() {
377 JSONObject obj = new JSONObject();
378 obj.put("data", new JSONObject());
379 obj.put("retCode", "1");
380 new MockUp<JSONObject>() {
382 @SuppressWarnings("static-access")
384 public JSONObject optJSONObject(String key) {
385 return new JSONObject().fromObject("{\"a\":\"1\"}");
388 JSONObject result = JsonUtil.getResponseData(obj);
389 String expectedResult = null;
390 assertEquals(expectedResult, result);
394 public void testGetResponseData1() {
395 JSONObject obj = new JSONObject();
396 obj.put("data", JSONArray.fromObject("[{\"a\":\"1\"},\"1\"]"));
397 obj.put("retCode", "1");
398 new MockUp<JSONObject>() {
401 public JSONObject optJSONObject(String key) {
405 @SuppressWarnings("static-access")
407 public JSONArray optJSONArray(String key) {
408 return new JSONArray().fromObject("[\"a\",\"1\"]");
411 JSONObject result = JsonUtil.getResponseData(obj);
412 String expectedResult = "{\"a\":\"1\"}";
413 assertEquals(expectedResult, result.toString());
417 public void testGetResponseData2() {
418 JSONObject obj = new JSONObject();
419 JSONObject json = new JSONObject();
420 json.put("retCode", "1");
421 obj.put("data", json);
422 obj.put("retCode", "1");
423 JSONObject result = JsonUtil.getResponseData(obj);
424 String expectedResult = null;
425 assertEquals(expectedResult, result);
428 public void testPrivateConstructor() throws Exception {
429 Constructor constructor = JsonUtil.class.getDeclaredConstructor();
430 assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
432 constructor.setAccessible(true);
433 constructor.newInstance();