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