b3258f5791b2e4b3b20c21ae5b2ea6e0ae8049de
[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.response;
18
19 import static org.junit.Assert.*;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Modifier;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.Test;
27
28 import net.sf.json.JSONObject;
29
30 public class ResponseUtilTest {
31
32     @Test
33     public void TestGenHttpResponseWithTwoParam() {
34         int retCode1 = -1;
35         String msg1 = "123";
36         JSONObject result = ResponseUtil.genHttpResponse(retCode1, msg1);
37         JSONObject expectedResult = new JSONObject();
38         expectedResult.put("msg", "123");
39         assertEquals(result.toString(), expectedResult.toString());
40         ;
41     }
42
43     @Test
44     public void TestGenHttpResponseWithThreeParam() {
45         int retCode1 = -1;
46         String msg1 = "123";
47         JSONObject result = ResponseUtil.genHttpResponse(retCode1, msg1, null);
48         JSONObject expectedResult = new JSONObject();
49         expectedResult.put("msg", "123");
50         assertEquals(result.toString(), expectedResult.toString());
51         ;
52     }
53
54     @Test
55     public void TestGenHttpResponseWithFourParam1() {
56         int httpStatusCode = -1;
57         int retCode1 = -1;
58         String msg1 = "123";
59         JSONObject result = ResponseUtil.genHttpResponse(null, httpStatusCode, retCode1, msg1);
60         JSONObject expectedResult = new JSONObject();
61         expectedResult.put("msg", "123");
62         assertEquals(result.toString(), expectedResult.toString());
63         ;
64     }
65
66     @Test
67     public void TestGenHttpResponseWithFourParam2() {
68         Map<String, Integer> codeMap = new HashMap<String, Integer>(5);
69         codeMap.put("httpStatusCode", -1);
70         codeMap.put("retCode", 1);
71         Map<String, Object> map = new HashMap<String, Object>(5);
72         map.put("a", -1);
73         map.put("b", 1);
74         String msg1 = "123";
75         JSONObject result = ResponseUtil.genHttpResponse(null, codeMap, msg1, map);
76         JSONObject expectedResult = new JSONObject();
77         expectedResult.put("msg", "123");
78         expectedResult.put("a", "-1");
79         assertEquals(result.toString(), expectedResult.toString());
80         ;
81     }
82     @Test
83     public void testPrivateConstructor() throws Exception {
84         Constructor constructor = ResponseUtil.class.getDeclaredConstructor();
85         assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
86
87         constructor.setAccessible(true);
88         constructor.newInstance();
89     }
90 }