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.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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
33 import net.sf.json.JSONObject;
35 public class VnfmRestfulUtilTest {
37 public void testGetRestResByDefaultByNull() {
38 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
43 public void testGetRestResByDefaultByGet() {
44 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
45 assertNotNull(result);
49 public void testGetRestResByDefaultByPut() {
50 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
51 assertNotNull(result);
55 public void testSendReqToApp() {
56 new MockUp<VnfmRestfulUtil>() {
59 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
60 RestfulResponse restfulResponse = new RestfulResponse();
61 restfulResponse.setStatus(Constant.HTTP_OK);
62 String responseString = "{\"retCode\":1,\"data\":\"success\"}";
63 restfulResponse.setResponseJson(responseString);
64 return restfulResponse;
67 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
68 assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
72 public void testSendReqToAppByErrorMsg() {
73 new MockUp<VnfmRestfulUtil>() {
76 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
77 RestfulResponse restfulResponse = new RestfulResponse();
78 restfulResponse.setStatus(Constant.HTTP_OK);
79 String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
80 restfulResponse.setResponseJson(responseString);
81 return restfulResponse;
84 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
85 assertEquals(Constant.REST_FAIL, result.get("retCode"));
89 public void testSendReqToAppByError() {
90 new MockUp<VnfmRestfulUtil>() {
93 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
94 RestfulResponse restfulResponse = new RestfulResponse();
95 restfulResponse.setStatus(Constant.HTTP_OK);
96 String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
97 restfulResponse.setResponseJson(responseString);
98 return restfulResponse;
101 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
102 assertEquals(Constant.REST_FAIL, result.get("retCode"));
106 public void testSendReqToAppByFail() {
107 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
108 assertEquals(Constant.REST_FAIL, result.get("retCode"));
112 public void testSendReqToAppByVnfmInfo() {
113 JSONObject paraJson = new JSONObject();
114 JSONObject vnfmObj = new JSONObject();
115 vnfmObj.put("id", "id");
116 paraJson.put("vnfmInfo", vnfmObj);
117 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
118 assertEquals(Constant.REST_FAIL, result.get("retCode"));
122 public void testGenerateParamsMap2() {
123 Map<String, String> result = VnfmRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
124 Map<String, String> paramsMap = new HashMap<String, String>(6);
125 paramsMap.put("url", "url");
126 paramsMap.put("methodType", "methodType");
127 paramsMap.put("path", "path");
128 paramsMap.put("authMode", "authMode");
129 assertEquals(paramsMap, result);
132 public void getRemoteResponseTestGet(){
133 Map<String, String> paramsMap = new HashMap<>();
134 paramsMap.put("url", "/test/123");
135 paramsMap.put("methodType", "get");
136 paramsMap.put("path", "http://localhost:8080");
137 paramsMap.put("authMode", "test");
138 boolean isNfvoApp = false;
139 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
143 public void getRemoteResponseTestGetTrueNfvo(){
144 Map<String, String> paramsMap = new HashMap<>();
145 paramsMap.put("url", "/test/123");
146 paramsMap.put("methodType", "get");
147 paramsMap.put("path", "http://localhost:8080");
148 paramsMap.put("authMode", "test");
149 boolean isNfvoApp = true;
150 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
154 public void getRemoteResponseTestPost(){
155 Map<String, String> paramsMap = new HashMap<>();
156 paramsMap.put("url", "/test/123");
157 paramsMap.put("methodType", "post");
158 paramsMap.put("path", "http://localhost:8080");
159 paramsMap.put("authMode", "test");
161 boolean isNfvoApp = false;
162 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
166 public void getRemoteResponseTestPut(){
167 Map<String, String> paramsMap = new HashMap<>();
168 paramsMap.put("url", "/test/123");
169 paramsMap.put("methodType", "put");
170 paramsMap.put("path", "http://localhost:8080");
171 paramsMap.put("authMode", "test");
173 boolean isNfvoApp = false;
174 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
179 public void getRemoteResponseTestDelete(){
180 Map<String, String> paramsMap = new HashMap<>();
181 paramsMap.put("url", "/test/123");
182 paramsMap.put("methodType", "delete");
183 paramsMap.put("path", "http://localhost:8080");
184 paramsMap.put("authMode", "test");
186 boolean isNfvoApp = false;
187 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
192 public void getRemoteResponse2TestDelete(){
193 Map<String, String> paramsMap = new HashMap<>();
194 paramsMap.put("url", "/test/123");
195 paramsMap.put("methodType", "delete");
196 paramsMap.put("path", "http://localhost:8080");
197 paramsMap.put("authMode", "test");
198 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
202 public void getRemoteResponse2TestGet(){
203 Map<String, String> paramsMap = new HashMap<>();
204 paramsMap.put("url", "/test/123");
205 paramsMap.put("methodType", "get");
206 paramsMap.put("path", "http://localhost:8080");
207 paramsMap.put("authMode", "test");
208 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
212 public void getRemoteResponse2Testput(){
213 Map<String, String> paramsMap = new HashMap<>();
214 paramsMap.put("url", "/test/123");
215 paramsMap.put("methodType", "put");
216 paramsMap.put("path", "http://localhost:8080");
217 paramsMap.put("authMode", "test");
218 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
222 public void getRemoteResponse2TestPost(){
223 Map<String, String> paramsMap = new HashMap<>();
224 paramsMap.put("url", "/test/123");
225 paramsMap.put("methodType", "put");
226 paramsMap.put("path", "http://localhost:8080");
227 paramsMap.put("authMode", "post");
228 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
232 public void getRemoteResponse2TestPatch(){
233 Map<String, String> paramsMap = new HashMap<>();
234 paramsMap.put("url", "/test/123");
235 paramsMap.put("methodType", "patch");
236 paramsMap.put("path", "http://localhost:8080");
237 paramsMap.put("authMode", "post");
238 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");