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