69bfd74173449e22a752ac56dd13029b9b1918e9
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 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.assertTrue;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.junit.Test;
26 import org.openo.baseservice.remoteservice.exception.ServiceException;
27 import org.openo.baseservice.roa.util.restclient.RestfulOptions;
28 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.nfvo.resmanagement.common.constant.Constant;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONArray;
35 import net.sf.json.JSONException;
36 import net.sf.json.JSONObject;
37
38 public class RestfulUtilTest {
39
40     @Test
41     public void testGetResponseObjWithTwoParams() {
42         JSONObject result = RestfulUtil.getResponseObj(null, null);
43         JSONObject expectedResult = null;
44         assertEquals(expectedResult, result);
45     }
46
47     @Test
48     public void testGetResponseObjWithThreeParams() {
49         new MockUp<RestfulUtil>() {
50
51             @Mock
52             public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
53                     String type) {
54                 return "{\"ResponseContent\":\"123\"}";
55             }
56         };
57         JSONObject result = RestfulUtil.getResponseObj(null, null, null);
58         @SuppressWarnings("static-access")
59         JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}");
60         assertEquals(expectedResult, result);
61     }
62
63     @Test
64     public void testGetResponseObjExpections() {
65         new MockUp<RestfulUtil>() {
66
67             @Mock
68             public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
69                     String type) {
70                 throw new JSONException();
71             }
72         };
73         JSONObject result = RestfulUtil.getResponseObj(null, null, null);
74         JSONObject expectedResult = null;
75         assertEquals(expectedResult, result);
76     }
77
78     @Test
79     public void testGetResponseContent() {
80         String result = RestfulUtil.getResponseContent(null, null, null);
81         String expectedResult = null;
82         assertEquals(expectedResult, result);
83     }
84
85     @Test
86     public void testGetResponseMap() {
87         Map<String, Object> result = RestfulUtil.getResponseMap(null, null, null, null);
88         Map<String, Object> expectedResult = new HashMap<String, Object>(10);
89         expectedResult.put(Constant.RESPONSE_CONTENT, null);
90         expectedResult.put(Constant.STATUS_CODE, -1);
91         assertEquals(expectedResult, result);
92     }
93
94     @Test
95     public void testGetResponseContentMap() {
96         Map<String, Object> result = RestfulUtil.getResponseContentMap(null, null);
97         Map<String, Object> expectedResult = new HashMap<String, Object>(10);
98         expectedResult.put(Constant.RESPONSE_CONTENT, null);
99         expectedResult.put(Constant.STATUS_CODE, -1);
100         assertEquals(expectedResult, result);
101     }
102
103     @Test
104     public void testGetResponseContentWithFourParams() {
105         new MockUp<RestfulResponse>() {
106
107             @Mock
108             public int getStatus() {
109                 return 200;
110             }
111         };
112         String result = RestfulUtil.getResponseContent(null, null, null);
113         String expectedResult = null;
114         assertEquals(expectedResult, result);
115     }
116
117     @Test
118     public void testGetRestfulResponse() {
119         RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, null);
120         RestfulResponse expectedResult = new RestfulResponse();
121         assertEquals(expectedResult.getStatus(), result.getStatus());
122     }
123
124     @Test
125     public void testRestfulResponse() {
126         RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "get");
127         RestfulResponse expectedResult = new RestfulResponse();
128         assertEquals(expectedResult.getStatus(), result.getStatus());
129     }
130
131     @Test
132     public void testRestfulResponse1() {
133         RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "add");
134         RestfulResponse expectedResult = new RestfulResponse();
135         assertEquals(expectedResult.getStatus(), result.getStatus());
136     }
137
138     @Test
139     public void testRestfulResponse2() {
140         RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "put");
141         RestfulResponse expectedResult = new RestfulResponse();
142         assertEquals(expectedResult.getStatus(), result.getStatus());
143     }
144
145     @Test
146     public void testRestfulResponse3() {
147         RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "delete");
148         RestfulResponse expectedResult = new RestfulResponse();
149         assertEquals(expectedResult.getStatus(), result.getStatus());
150     }
151
152     @Test
153     public void testGetRestResObjectsIsNull() {
154         RestfulResponse result = RestfulUtil.getRestRes(null, null);
155         RestfulResponse expectedResult = null;
156         assertEquals(expectedResult, result);
157     }
158
159     @Test
160     public void testGetRestResReflectiveOperationException() {
161         RestfulResponse result = RestfulUtil.getRestRes("123", "get");
162         RestfulResponse expectedResult = null;
163         assertEquals(expectedResult, result);
164     }
165
166     @Test
167     public void testGetRestRes() {
168         RestfulResponse result = RestfulUtil.getRestRes("async123", new RestfulResponse());
169         RestfulResponse expectedResult = null;
170         assertEquals(expectedResult, result);
171     }
172
173     @Test
174     public void testGetResponseResResultIsNull() throws ServiceException {
175         try {
176             RestfulUtil.getResponseRes(null, null);
177         } catch (ServiceException e) {
178             assertTrue(true);
179         }
180     }
181
182     @Test
183     public void testGetResponse() throws ServiceException {
184         new MockUp<RestfulUtil>() {
185
186             @Mock
187             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
188                 return "{\"ResponseContent\":\"123\",\"data\":[\"datas\"]}";
189             }
190         };
191         JSONArray result = RestfulUtil.getResponseRes(null, null);
192         JSONArray expectedResult = JSONArray.fromObject("[\"datas\"]");
193         assertEquals(expectedResult, result);
194     }
195
196     @Test
197     public void testGetResponseExceptions() throws ServiceException {
198         new MockUp<RestfulUtil>() {
199
200             @Mock
201             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
202                 return "{\"ResponseContent\":\"123\",}";
203             }
204         };
205         try {
206             RestfulUtil.getResponseRes(null, null);
207         } catch (ServiceException e) {
208             assertTrue(true);
209         }
210     }
211
212     @Test
213     public void testGgetResponseRes() throws ServiceException {
214         new MockUp<RestfulUtil>() {
215
216             @Mock
217             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
218                 return "{\"ResponseContent\":\"123\",}";
219             }
220         };
221         try {
222             RestfulUtil.getResponseRes(null, null, null);
223         } catch (ServiceException e) {
224             assertTrue(true);
225         }
226     }
227
228     @Test
229     public void testGgetResponseResException() throws ServiceException {
230         new MockUp<RestfulUtil>() {
231
232             @Mock
233             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
234                 return null;
235             }
236         };
237         try {
238             RestfulUtil.getResponseRes(null, null, null);
239         } catch (ServiceException e) {
240             assertTrue(true);
241         }
242     }
243
244 }