2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.vnfmadapter.common.servicetoken;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
23 import java.io.IOException;
24 import java.util.HashMap;
27 import org.junit.Test;
28 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
29 import org.openo.nfvo.vnfmadapter.testutils.JsonUtil;
33 import net.sf.json.JSONObject;
41 * @version NFVO 0.5 Aug 10, 2016
43 public class VNFRestfulUtilTest {
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);
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);
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);
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);
84 public void sendReqToAppTest(){
85 new MockUp<VNFRestfulUtil>(){
87 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
88 RestfulResponse resp = new RestfulResponse();
90 Map<String,Object> map = new HashMap<>();
91 map.put("retCode", 1);
92 resp.setResponseJson(toJson(map));
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);
105 public void sendReqToAppTest2(){
106 new MockUp<VNFRestfulUtil>(){
108 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
109 RestfulResponse resp = new RestfulResponse();
111 Map<String,Object> map = new HashMap<>();
112 map.put("retCode", -1);
113 resp.setResponseJson(toJson(map));
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);
125 public void sendReqToAppTest3(){
126 new MockUp<VNFRestfulUtil>(){
128 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
129 RestfulResponse resp = new RestfulResponse();
131 Map<String,Object> map = new HashMap<>();
132 map.put("retCode", -1);
133 resp.setResponseJson(toJson(map));
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);
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");
153 boolean isNfvoApp = false;
154 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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");
166 boolean isNfvoApp = false;
167 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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");
179 boolean isNfvoApp = false;
180 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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");
191 boolean isNfvoApp = false;
192 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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);
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);
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, "");
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");
236 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
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");
247 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
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");
258 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
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");
269 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
274 public void getRemoteResponseNullTest(){
276 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(null, "");
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"));
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"));
298 public void getResultToVnfmTest(){
299 JSONObject vnfmInfo= new JSONObject();
300 vnfmInfo.put("retCode", 1);
302 JSONObject res = VNFRestfulUtil.getResultToVnfm(vnfmInfo, vnfmId);
307 public void getResultToVnfm2Test(){
308 JSONObject vnfmInfo= new JSONObject();
309 vnfmInfo.put("retCode", -1);
311 JSONObject res = VNFRestfulUtil.getResultToVnfm(vnfmInfo, vnfmId);
315 public static String toJson(Map o) {
317 return JsonUtil.marshal(o);
318 } catch (IOException e) {