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.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
23 import java.util.HashMap;
26 import org.junit.Test;
27 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
28 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
32 import net.sf.json.JSONObject;
34 public class VnfmRestfulUtilTest {
36 public void testGetRestResByDefaultByNull() {
37 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
42 public void testGetRestResByDefaultByGet() {
43 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
44 assertNotNull(result);
48 public void testGetRestResByDefaultByPut() {
49 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
50 assertNotNull(result);
54 public void testSendReqToApp() {
55 new MockUp<VnfmRestfulUtil>() {
58 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
59 RestfulResponse restfulResponse = new RestfulResponse();
60 restfulResponse.setStatus(Constant.HTTP_OK);
61 String responseString = "{\"retCode\":1,\"data\":\"success\"}";
62 restfulResponse.setResponseJson(responseString);
63 return restfulResponse;
66 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
67 assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
71 public void testSendReqToAppByErrorMsg() {
72 new MockUp<VnfmRestfulUtil>() {
75 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
76 RestfulResponse restfulResponse = new RestfulResponse();
77 restfulResponse.setStatus(Constant.HTTP_OK);
78 String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
79 restfulResponse.setResponseJson(responseString);
80 return restfulResponse;
83 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
84 assertEquals(Constant.REST_FAIL, result.get("retCode"));
88 public void testSendReqToAppByError() {
89 new MockUp<VnfmRestfulUtil>() {
92 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
93 RestfulResponse restfulResponse = new RestfulResponse();
94 restfulResponse.setStatus(Constant.HTTP_OK);
95 String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
96 restfulResponse.setResponseJson(responseString);
97 return restfulResponse;
100 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
101 assertEquals(Constant.REST_FAIL, result.get("retCode"));
105 public void testSendReqToAppByFail() {
106 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
107 assertEquals(Constant.REST_FAIL, result.get("retCode"));
111 public void testSendReqToAppByVnfmInfo() {
112 JSONObject paraJson = new JSONObject();
113 JSONObject vnfmObj = new JSONObject();
114 vnfmObj.put("id", "id");
115 paraJson.put("vnfmInfo", vnfmObj);
116 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
117 assertEquals(Constant.REST_FAIL, result.get("retCode"));
121 public void testGenerateParamsMap2() {
122 Map<String, String> result = VnfmRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
123 Map<String, String> paramsMap = new HashMap<String, String>(6);
124 paramsMap.put("url", "url");
125 paramsMap.put("methodType", "methodType");
126 paramsMap.put("path", "path");
127 paramsMap.put("authMode", "authMode");
128 assertEquals(paramsMap, result);
131 public void getRemoteResponseTestGet(){
132 Map<String, String> paramsMap = new HashMap<>();
133 paramsMap.put("url", "/test/123");
134 paramsMap.put("methodType", "get");
135 paramsMap.put("path", "http://localhost:8080");
136 paramsMap.put("authMode", "test");
137 boolean isNfvoApp = false;
138 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
142 public void getRemoteResponseTestGetTrueNfvo(){
143 Map<String, String> paramsMap = new HashMap<>();
144 paramsMap.put("url", "/test/123");
145 paramsMap.put("methodType", "get");
146 paramsMap.put("path", "http://localhost:8080");
147 paramsMap.put("authMode", "test");
148 boolean isNfvoApp = true;
149 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
153 public void getRemoteResponseTestPost(){
154 Map<String, String> paramsMap = new HashMap<>();
155 paramsMap.put("url", "/test/123");
156 paramsMap.put("methodType", "post");
157 paramsMap.put("path", "http://localhost:8080");
158 paramsMap.put("authMode", "test");
160 boolean isNfvoApp = false;
161 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
165 public void getRemoteResponseTestPut(){
166 Map<String, String> paramsMap = new HashMap<>();
167 paramsMap.put("url", "/test/123");
168 paramsMap.put("methodType", "put");
169 paramsMap.put("path", "http://localhost:8080");
170 paramsMap.put("authMode", "test");
172 boolean isNfvoApp = false;
173 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
178 public void getRemoteResponseTestDelete(){
179 Map<String, String> paramsMap = new HashMap<>();
180 paramsMap.put("url", "/test/123");
181 paramsMap.put("methodType", "delete");
182 paramsMap.put("path", "http://localhost:8080");
183 paramsMap.put("authMode", "test");
185 boolean isNfvoApp = false;
186 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
191 public void getRemoteResponse2TestDelete(){
192 Map<String, String> paramsMap = new HashMap<>();
193 paramsMap.put("url", "/test/123");
194 paramsMap.put("methodType", "delete");
195 paramsMap.put("path", "http://localhost:8080");
196 paramsMap.put("authMode", "test");
197 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
201 public void getRemoteResponse2TestGet(){
202 Map<String, String> paramsMap = new HashMap<>();
203 paramsMap.put("url", "/test/123");
204 paramsMap.put("methodType", "get");
205 paramsMap.put("path", "http://localhost:8080");
206 paramsMap.put("authMode", "test");
207 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
211 public void getRemoteResponse2Testput(){
212 Map<String, String> paramsMap = new HashMap<>();
213 paramsMap.put("url", "/test/123");
214 paramsMap.put("methodType", "put");
215 paramsMap.put("path", "http://localhost:8080");
216 paramsMap.put("authMode", "test");
217 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
221 public void getRemoteResponse2TestPost(){
222 Map<String, String> paramsMap = new HashMap<>();
223 paramsMap.put("url", "/test/123");
224 paramsMap.put("methodType", "put");
225 paramsMap.put("path", "http://localhost:8080");
226 paramsMap.put("authMode", "post");
227 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
231 public void getRemoteResponse2TestPatch(){
232 Map<String, String> paramsMap = new HashMap<>();
233 paramsMap.put("url", "/test/123");
234 paramsMap.put("methodType", "patch");
235 paramsMap.put("path", "http://localhost:8080");
236 paramsMap.put("authMode", "post");
237 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");