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