Junit coverage increased
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / servicetoken / VNFRestfulUtilTest.java
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.vnfm.svnfm.vnfmadapter.common.servicetoken;
18
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.junit.Test;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VNFRestfulUtil;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.testutils.JsonUtil;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 /**
37  * <br/>
38  * <p>
39  * </p>
40  *
41  * @author
42  * @version VFC 1.0 Aug 10, 2016
43  */
44 public class VNFRestfulUtilTest {
45
46     @Test
47     public void getRestResByDefaultTestGet(){
48         String path="http://localhost:8080";
49         String methodNames = "get";
50         JSONObject bodyParam = new JSONObject();
51         bodyParam.put("id", "1234");
52         RestfulResponse resp = VNFRestfulUtil.getRestResByDefault(path, methodNames, bodyParam);
53         assertNotNull(resp);
54     }
55
56     @Test
57     public void getRestResByDefaultTestDelete(){
58         String path="http://localhost:8080";
59         String methodNames = "delete";
60         JSONObject bodyParam = new JSONObject();
61         bodyParam.put("id", "1234");
62         RestfulResponse resp = VNFRestfulUtil.getRestResByDefault(path, methodNames, bodyParam);
63         assertNotNull(resp);
64     }
65     @Test
66     public void getRestResByDefaultTestPost(){
67         String path="http://localhost:8080";
68         String methodNames = "post";
69         JSONObject bodyParam = new JSONObject();
70         bodyParam.put("id", "1234");
71         RestfulResponse resp = VNFRestfulUtil.getRestResByDefault(path, methodNames, bodyParam);
72         assertNotNull(resp);
73     }
74
75     @Test
76     public void sendReqToAppTestNullResp(){
77         String path="http://localhost:8080";
78         String methodNames = "get";
79         JSONObject bodyParam = new JSONObject();
80         bodyParam.put("id", "1234");
81         JSONObject resp = VNFRestfulUtil.sendReqToApp(path, methodNames, bodyParam);
82         assertNotNull(resp);
83     }
84     @Test
85     public void sendReqToAppTest(){
86 //        new MockUp<VNFRestfulUtil>(){
87 //            @Mock
88 //            public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
89 //                RestfulResponse resp = new RestfulResponse();
90 //                resp.setStatus(200);
91 //                Map<String,Object> map = new HashMap<>();
92 //                map.put("retCode", 1);
93 //                resp.setResponseJson(toJson(map));
94 //                return resp;
95 //            }
96 //        };
97         String path="http://localhost:8080/vnfdmgr/v1";
98         String methodNames = "get";
99         JSONObject bodyParam = new JSONObject();
100         bodyParam.put("vnfmInfo", new JSONObject().put("id", "6775"));
101         JSONObject resp = VNFRestfulUtil.sendReqToApp(path, methodNames, bodyParam);
102         assertNotNull(resp);
103     }
104
105     @Test
106     public void sendReqToAppTest2(){
107 //        new MockUp<VNFRestfulUtil>(){
108 //            @Mock
109 //            public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
110 //                RestfulResponse resp = new RestfulResponse();
111 //                resp.setStatus(200);
112 //                Map<String,Object> map = new HashMap<>();
113 //                map.put("retCode", -1);
114 //                resp.setResponseJson(toJson(map));
115 //                return resp;
116 //            }
117 //        };
118         String path="http://localhost:8080/vnfdmgr/v1";
119         String methodNames = "get";
120         JSONObject bodyParam = new JSONObject();
121         bodyParam.put("vnfmInfo", new JSONObject().put("id", "6775"));
122         JSONObject resp = VNFRestfulUtil.sendReqToApp(path, methodNames, bodyParam);
123         assertNotNull(resp);
124     }
125     @Test
126     public void sendReqToAppTest3(){
127 //        new MockUp<VNFRestfulUtil>(){
128 //            @Mock
129 //            public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
130 //                RestfulResponse resp = new RestfulResponse();
131 //                resp.setStatus(500);
132 //                Map<String,Object> map = new HashMap<>();
133 //                map.put("retCode", -1);
134 //                resp.setResponseJson(toJson(map));
135 //                return resp;
136 //            }
137 //        };
138         String path="http://localhost:8080/vnfdmgr/v1";
139         String methodNames = "get";
140         JSONObject bodyParam = new JSONObject();
141         bodyParam.put("vnfmInfo", new JSONObject().put("id", "6775"));
142         JSONObject resp = VNFRestfulUtil.sendReqToApp(path, methodNames, bodyParam);
143         assertNotNull(resp);
144     }
145
146     @Test
147     public void getRemoteResponseTest(){
148         Map<String, String> paramsMap = new HashMap<>();
149         paramsMap.put("url", "/test/123");
150         paramsMap.put("methodType", "delete");
151         paramsMap.put("path", "http://localhost:8080");
152         paramsMap.put("authMode", "test");
153
154         boolean isNfvoApp = false;
155         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
156         assertNull(resp);
157     }
158
159     @Test
160     public void getRemoteResponse2Test(){
161         Map<String, String> paramsMap = new HashMap<>();
162         paramsMap.put("url", "/test/123");
163         paramsMap.put("methodType", "get");
164         paramsMap.put("path", "http://localhost:8080");
165         paramsMap.put("authMode", "test");
166
167         boolean isNfvoApp = false;
168         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
169         assertNull(resp);
170     }
171
172     @Test
173     public void getRemoteResponse3Test(){
174         Map<String, String> paramsMap = new HashMap<>();
175         paramsMap.put("url", "/test/123");
176         paramsMap.put("methodType", "post");
177         paramsMap.put("path", "http://localhost:8080");
178         paramsMap.put("authMode", "test");
179
180         boolean isNfvoApp = false;
181         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
182         assertNull(resp);
183     }
184     @Test
185     public void getRemoteResponse4Test(){
186         Map<String, String> paramsMap = new HashMap<>();
187         paramsMap.put("url", "/test/123");
188         paramsMap.put("methodType", "put");
189         paramsMap.put("path", "http://localhost:8080");
190         paramsMap.put("authMode", "test");
191
192         boolean isNfvoApp = false;
193         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
194         assertNull(resp);
195     }
196     @Test
197     public void getRemoteResponse5Test(){
198         Map<String, String> paramsMap = new HashMap<>();
199         paramsMap.put("url", "/test/123");
200         paramsMap.put("methodType", "patch");
201         paramsMap.put("path", "http://localhost:8080");
202         paramsMap.put("authMode", "test");
203         boolean isNfvoApp = false;
204         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
205         assertNull(resp);
206     }
207     @Test
208     public void getRemoteResponseTrueTest(){
209         Map<String, String> paramsMap = new HashMap<>();
210         paramsMap.put("url", "/test/123");
211         paramsMap.put("methodType", "patch");
212         paramsMap.put("path", "http://localhost:8080");
213         paramsMap.put("authMode", "test");
214         boolean isNfvoApp = true;
215         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
216         assertNull(resp);
217     }
218
219     @Test
220     public void getRemoteResponseDeleteTest(){
221         Map<String, String> paramsMap = new HashMap<>();
222         paramsMap.put("url", "/test/123");
223         paramsMap.put("methodType", "delete");
224         paramsMap.put("path", "http://localhost:8080");
225         paramsMap.put("authMode", "test");
226         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
227         assertNull(resp);
228     }
229     @Test
230     public void getRemoteResponseGetTest(){
231         Map<String, String> paramsMap = new HashMap<>();
232         paramsMap.put("url", "/test/123");
233         paramsMap.put("methodType", "get");
234         paramsMap.put("path", "http://localhost:8080");
235         paramsMap.put("authMode", "test");
236
237         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
238         assertNull(resp);
239     }
240     @Test
241     public void getRemoteResponsePostTest(){
242         Map<String, String> paramsMap = new HashMap<>();
243         paramsMap.put("url", "/test/123");
244         paramsMap.put("methodType", "post");
245         paramsMap.put("path", "http://localhost:8080");
246         paramsMap.put("authMode", "test");
247
248         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
249         assertNull(resp);
250     }
251     @Test
252     public void getRemoteResponsePutTest(){
253         Map<String, String> paramsMap = new HashMap<>();
254         paramsMap.put("url", "/test/123");
255         paramsMap.put("methodType", "put");
256         paramsMap.put("path", "http://localhost:8080");
257         paramsMap.put("authMode", "test");
258
259         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
260         assertNull(resp);
261     }
262     @Test
263     public void getRemoteResponsePatchTest(){
264         Map<String, String> paramsMap = new HashMap<>();
265         paramsMap.put("url", "/test/123");
266         paramsMap.put("methodType", "patch");
267         paramsMap.put("path", "http://localhost:8080");
268         paramsMap.put("authMode", "test");
269
270         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
271         assertNull(resp);
272     }
273
274     @Test
275     public void getRemoteResponseNullTest(){
276
277         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(null, "");
278         assertNull(resp);
279     }
280     @Test
281     public void generateParamsMapTest(){
282         String url = "/test/123";
283         String methodType="get";
284         String path="http://localhost:8080";
285         String authMode="test";
286         Map<String, String> res = VNFRestfulUtil.generateParamsMap(url, methodType, path, authMode);
287         assertTrue(res.get("url").equals("/test/123"));
288     }
289
290     @Test
291     public void generateParams2MapTest(){
292         String url = "/test/123";
293         String methodType="get";
294         String path="http://localhost:8080";
295         Map<String, String> res = VNFRestfulUtil.generateParamsMap(url, methodType, path);
296         assertTrue(res.get("url").equals("/test/123"));
297     }
298     @Test
299     public void getResultToVnfmTest(){
300         JSONObject vnfmInfo= new JSONObject();
301         vnfmInfo.put("retCode", 1);
302         String vnfmId="123";
303         JSONObject res = VNFRestfulUtil.getResultToVnfm(vnfmInfo, vnfmId);
304         assertNotNull(res);
305     }
306
307     @Test
308     public void getResultToVnfm2Test(){
309         JSONObject vnfmInfo= new JSONObject();
310         vnfmInfo.put("retCode", -1);
311         String vnfmId="123";
312         JSONObject res = VNFRestfulUtil.getResultToVnfm(vnfmInfo, vnfmId);
313         assertNotNull(res);
314     }
315
316     public static String toJson(Map o) {
317         try {
318             return JsonUtil.marshal(o);
319         } catch (IOException e) {
320             return "";
321         }
322     }
323 }