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.onap.vfc.nfvo.vnfm.svnfm.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.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;
34 import net.sf.json.JSONObject;
42 * @version NFVO 0.5 Aug 10, 2016
44 public class VNFRestfulUtilTest {
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);
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);
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);
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);
85 public void sendReqToAppTest(){
86 new MockUp<VNFRestfulUtil>(){
88 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
89 RestfulResponse resp = new RestfulResponse();
91 Map<String,Object> map = new HashMap<>();
92 map.put("retCode", 1);
93 resp.setResponseJson(toJson(map));
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);
106 public void sendReqToAppTest2(){
107 new MockUp<VNFRestfulUtil>(){
109 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
110 RestfulResponse resp = new RestfulResponse();
112 Map<String,Object> map = new HashMap<>();
113 map.put("retCode", -1);
114 resp.setResponseJson(toJson(map));
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);
126 public void sendReqToAppTest3(){
127 new MockUp<VNFRestfulUtil>(){
129 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
130 RestfulResponse resp = new RestfulResponse();
132 Map<String,Object> map = new HashMap<>();
133 map.put("retCode", -1);
134 resp.setResponseJson(toJson(map));
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);
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");
154 boolean isNfvoApp = false;
155 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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");
167 boolean isNfvoApp = false;
168 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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");
180 boolean isNfvoApp = false;
181 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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");
192 boolean isNfvoApp = false;
193 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
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);
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);
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, "");
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");
237 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
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");
248 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
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");
259 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
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");
270 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
275 public void getRemoteResponseNullTest(){
277 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(null, "");
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"));
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"));
299 public void getResultToVnfmTest(){
300 JSONObject vnfmInfo= new JSONObject();
301 vnfmInfo.put("retCode", 1);
303 JSONObject res = VNFRestfulUtil.getResultToVnfm(vnfmInfo, vnfmId);
308 public void getResultToVnfm2Test(){
309 JSONObject vnfmInfo= new JSONObject();
310 vnfmInfo.put("retCode", -1);
312 JSONObject res = VNFRestfulUtil.getResultToVnfm(vnfmInfo, vnfmId);
316 public static String toJson(Map o) {
318 return JsonUtil.marshal(o);
319 } catch (IOException e) {