92b499deec4f2729219a254e0732c9975acd0550
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.nfvo.resmanagement.common.util;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
25
26 import org.junit.Test;
27
28 import mockit.Mock;
29 import mockit.MockUp;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
32
33 public class JsonUtilTest {
34
35     @Test
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);
44
45     }
46
47     @Test
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);
56
57     }
58
59     @Test
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);
68
69     }
70
71     @Test
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);
80
81     }
82
83     @Test
84     public void testGetJsonFieldLong() {
85         JSONObject jsonObj = new JSONObject();
86         String fieldName = "a";
87         jsonObj.put("a", 1);
88         jsonObj.put("b", 2);
89         Long result = JsonUtil.getJsonFieldLong(jsonObj, fieldName);
90         Long expectedResult = new Long(1);
91         assertEquals(expectedResult, result);
92
93     }
94
95     @Test
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);
104
105     }
106
107     @Test
108     public void testIsNullJson1() {
109         JSONObject jsonObj = new JSONObject();
110         assertTrue(JsonUtil.isNullJson(jsonObj));
111     }
112
113     @Test
114     public void testIsNullJson2() {
115         assertTrue(JsonUtil.isNullJson(null));
116     }
117
118     @Test
119     public void testIsNullJson3() {
120         JSONObject jsonObj = new JSONObject();
121         jsonObj.put("a", "1");
122         assertFalse(JsonUtil.isNullJson(jsonObj));
123     }
124
125     @Test
126     public void testGetStrValueByjsonNULL() {
127         JSONObject jsonObj = new JSONObject();
128         String key = "a";
129         String result = JsonUtil.getStrValueByjson(jsonObj, key);
130         String expectedResult = null;
131         assertEquals(expectedResult, result);
132
133     }
134
135     @Test
136     public void testGetStrValueByjson() {
137         JSONObject jsonObj = new JSONObject();
138         String key = "a";
139         jsonObj.put("a", "1");
140         jsonObj.put("b", "2");
141         new MockUp<JSONObject>() {
142
143             @SuppressWarnings("static-access")
144             @Mock
145             public JSONObject optJSONObject(String key) {
146                 return new JSONObject().fromObject("{\"a\":\"1\"}");
147             }
148
149             @Mock
150             public JSONObject getJSONObject(String key) {
151                 return new JSONObject();
152             }
153         };
154         String result = JsonUtil.getStrValueByjson(jsonObj, key);
155         String expectedResult = "1";
156         assertEquals(expectedResult, result);
157
158     }
159
160     @Test
161     public void testGetStrValueByjson1() {
162         JSONObject jsonObj = new JSONObject();
163         String key = "a";
164         jsonObj.put("a", "1");
165         jsonObj.put("b", "2");
166         new MockUp<JSONObject>() {
167
168             @Mock
169             public JSONObject optJSONObject(String key) {
170                 return null;
171             }
172
173             @SuppressWarnings("static-access")
174             @Mock
175             public JSONArray optJSONArray(String key) {
176                 return new JSONArray().fromObject("[\"a\",\"1\"]");
177             }
178
179             @Mock
180             public JSONArray getJSONArray(String key) {
181                 return new JSONArray();
182             }
183         };
184         String result = JsonUtil.getStrValueByjson(jsonObj, key);
185         String expectedResult = "1";
186         assertEquals(expectedResult, result);
187
188     }
189
190     @Test
191     public void testGetStrValueByjson2() {
192         JSONObject jsonObj = new JSONObject();
193         String key = "a";
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);
199
200     }
201
202     @Test
203     public void testGetStrValueByJArray() {
204         JSONObject jsonObj = new JSONObject();
205         String key = "a";
206         jsonObj.put("a", "1");
207         jsonObj.put("b", "2");
208         new MockUp<JSONObject>() {
209
210             @Mock
211             public JSONObject optJSONObject(String key) {
212                return null;
213             }
214
215             @SuppressWarnings("static-access")
216             @Mock
217             public JSONArray optJSONArray(String key) {
218                 return new JSONArray().fromObject("[\"a\",\"1\"]");
219             }
220
221             @SuppressWarnings("static-access")
222             @Mock
223             public JSONArray getJSONArray(String key) {
224                 return new JSONArray().fromObject("[\"a\",\"1\"]");
225             }
226
227
228         };
229
230         String result = JsonUtil.getStrValueByjson(jsonObj, key);
231
232     }
233
234     @Test
235     public void testGetStrValueByJArray1() {
236         JSONObject jsonObj = new JSONObject();
237         String key = "a";
238         jsonObj.put("a", "1");
239         jsonObj.put("b", "2");
240         new MockUp<JSONObject>() {
241
242             int count = 1;
243
244             @SuppressWarnings("static-access")
245             @Mock
246             public JSONObject optJSONObject(String key) {
247                 if (count == 1) {
248                     count += 1;
249                     return null;
250                 } else
251                     return new JSONObject().fromObject("{\"a\":\"1\"}");
252             }
253
254             @SuppressWarnings("static-access")
255             @Mock
256             public JSONArray optJSONArray(String key) {
257                 return new JSONArray().fromObject("[\"a\",\"1\"]");
258             }
259
260             @SuppressWarnings("static-access")
261             @Mock
262             public JSONArray getJSONArray(String key) {
263                 return new JSONArray().fromObject("[\"a\",\"1\"]");
264             }
265         };
266         String result = JsonUtil.getStrValueByjson(jsonObj, key);
267         String expectedResult = "1";
268         assertEquals(expectedResult, result);
269
270     }
271
272     @Test
273     public void testGetJsonValueByjson() {
274         JSONObject jsonObj = new JSONObject();
275         String key = "a";
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);
281     }
282
283     @Test
284     public void testGetJsonValueByjsonResultIsNull() {
285         JSONObject jsonObj = new JSONObject();
286         String key = "c";
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);
292     }
293
294     @Test
295     public void testGetStrValueByJsonParentKeyIsNull() {
296         JSONObject jsonObj = new JSONObject();
297         String key = "a";
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);
304     }
305
306     @Test
307     public void testGetStrValueByJsonParentJsonIsNull() {
308         JSONObject jsonObj = new JSONObject();
309         String key = "a";
310         jsonObj.put("a", "1");
311         jsonObj.put("b", "2");
312         String parentKey = "b";
313         new MockUp<JsonUtil>() {
314
315             @Mock
316             public JSONObject getJsonValueByjson(JSONObject json, String key) {
317                 return new JSONObject();
318             }
319         };
320         String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
321         String expectedResult = null;
322         assertEquals(expectedResult, result);
323     }
324
325     @Test
326     public void testGetStrValueByJson() {
327         JSONObject jsonObj = new JSONObject();
328         String key = "a";
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);
335     }
336
337     @Test
338     public void testGetResponseDataRetcodeError1() {
339         new MockUp<JsonUtil>() {
340
341             @Mock
342             public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
343                 return null;
344             }
345         };
346         JSONObject result = JsonUtil.getResponseData(null);
347         String expectedResult = null;
348         assertEquals(expectedResult, result);
349     }
350
351     @Test
352     public void testGetResponseDataRetcodeError2() {
353         new MockUp<JsonUtil>() {
354
355             @Mock
356             public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
357                 return -1;
358             }
359         };
360         JSONObject result = JsonUtil.getResponseData(null);
361         String expectedResult = null;
362         assertEquals(expectedResult, result);
363     }
364
365     @Test
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);
373     }
374
375     @Test
376     public void testGetResponseData() {
377         JSONObject obj = new JSONObject();
378         obj.put("data", new JSONObject());
379         obj.put("retCode", "1");
380         new MockUp<JSONObject>() {
381
382             @SuppressWarnings("static-access")
383             @Mock
384             public JSONObject optJSONObject(String key) {
385                 return new JSONObject().fromObject("{\"a\":\"1\"}");
386             }
387         };
388         JSONObject result = JsonUtil.getResponseData(obj);
389         String expectedResult = null;
390         assertEquals(expectedResult, result);
391     }
392
393     @Test
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>() {
399
400             @Mock
401             public JSONObject optJSONObject(String key) {
402                 return null;
403             }
404
405             @SuppressWarnings("static-access")
406             @Mock
407             public JSONArray optJSONArray(String key) {
408                 return new JSONArray().fromObject("[\"a\",\"1\"]");
409             }
410         };
411         JSONObject result = JsonUtil.getResponseData(obj);
412         String expectedResult = "{\"a\":\"1\"}";
413         assertEquals(expectedResult, result.toString());
414     }
415
416     @Test
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);
426     }
427     @Test
428     public void testPrivateConstructor() throws Exception {
429         Constructor constructor = JsonUtil.class.getDeclaredConstructor();
430         assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
431
432         constructor.setAccessible(true);
433         constructor.newInstance();
434     }
435 }