2 * Copyright 2016-2017 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.jujuvnfmadapter.common.servicetoken;
19 import static org.junit.Assert.*;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Modifier;
23 import java.util.HashMap;
26 import org.junit.Test;
27 import org.openo.baseservice.remoteservice.exception.ServiceException;
28 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.VNFRestfulUtil;
31 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
35 import net.sf.json.JSONObject;
43 * @version NFVO 0.5 Aug 10, 2016
45 public class JujuVnfmRestfulUtilTest {
49 public void testGetRemoteResponseByVnfmInfoNull() {
50 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(new HashMap(), null);
51 assertEquals(null, result);
55 public void testGetRemoteResponseByPost() {
56 Map testMap = new HashMap();
57 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
58 testMap.put("methodType","post");
59 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
61 assertEquals(null, result);
65 public void testGetRemoteResponseByVnfmInfo() {
66 Map testMap = new HashMap();
67 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
68 testMap.put("methodType","get");
69 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
71 assertEquals(null, result);
75 public void testGetRemoteResponseByVnfmInfoPut() {
76 Map testMap = new HashMap();
77 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
78 testMap.put("methodType","put");
79 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
81 assertEquals(null, result);
85 public void testGetRemoteResponseByVnfmInfoDelete() {
86 Map testMap = new HashMap();
87 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
88 testMap.put("methodType","delete");
89 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
91 assertEquals(null, result);
95 public void getVimResponseContentGetInvalid(){
96 RestfulParametes restParametes = new RestfulParametes();
97 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "put");
98 assertTrue(result.isEmpty());
101 public void getVimResponseContentAddInvalid(){
102 RestfulParametes restParametes = new RestfulParametes();
103 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "add");
104 assertTrue(result.isEmpty());
108 public void getVimResponseContentPutInvalid(){
109 RestfulParametes restParametes = new RestfulParametes();
110 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "put");
111 assertTrue(result.isEmpty());
114 public void getVimResponseContentDeleteInvalid(){
115 RestfulParametes restParametes = new RestfulParametes();
116 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "delete");
117 assertTrue(result.isEmpty());
121 public void getVimResponseContentPatchInvalid(){
122 RestfulParametes restParametes = new RestfulParametes();
123 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "patch");
124 assertTrue(result.isEmpty());
128 public void testGetResultToVnfmByVnfmInfoErrorMsg() {
129 JSONObject vnfmInfo = new JSONObject();
130 vnfmInfo.put("retCode", Constant.REST_FAIL);
131 vnfmInfo.put("msg", "ErrorMsg");
132 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
134 JSONObject retJson = new JSONObject();
135 retJson.put("retCode", Constant.REST_FAIL);
136 retJson.put("data", "ErrorMsg");
137 assertEquals(retJson, result);
141 public void testGetResultToVnfmByVnfmInfoError() {
142 JSONObject vnfmInfo = new JSONObject();
143 vnfmInfo.put("retCode", Constant.REST_FAIL);
144 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
146 JSONObject retJson = new JSONObject();
147 retJson.put("retCode", Constant.REST_FAIL);
148 assertEquals(retJson, result);
152 public void vimRestfulResponseTestGetInvalid(){
153 RestfulParametes restParametes = new RestfulParametes();
154 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "get");
155 assertTrue(resp == null);
158 public void vimRestfulResponseTestAddInvalid(){
159 RestfulParametes restParametes = new RestfulParametes();
160 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "add");
161 assertTrue(resp == null);
165 public void vimRestfulResponseTestPutInvalid(){
166 RestfulParametes restParametes = new RestfulParametes();
167 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "put");
168 assertTrue(resp == null);
172 public void vimRestfulResponseTestDeleteInvalid(){
173 RestfulParametes restParametes = new RestfulParametes();
174 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "delete");
175 assertTrue(resp == null);
179 public void vimRestfulResponseTestPatchInvalid(){
180 RestfulParametes restParametes = new RestfulParametes();
181 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "patch");
182 assertTrue(resp == null);
186 public void getRemoteResponseTestGetInvalid(){
187 Map<String,String> paramsMap = new HashMap<>();
188 paramsMap.put("path", "http://localhost:8080");
189 paramsMap.put("methodType","get");
190 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
196 public void getRemoteResponseTestGetHttpsInvalid(){
197 Map<String,String> paramsMap = new HashMap<>();
198 paramsMap.put("path", "http://localhost:8080");
199 paramsMap.put("methodType","get");
200 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", true);
206 public void getRemoteResponseTestPostInvalid(){
207 Map<String,String> paramsMap = new HashMap<>();
208 paramsMap.put("path", "http://localhost:8080");
209 paramsMap.put("methodType","post");
210 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
216 public void getRemoteResponseTestPutInvalid(){
217 Map<String,String> paramsMap = new HashMap<>();
218 paramsMap.put("path", "http://localhost:8080");
219 paramsMap.put("methodType","put");
220 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
226 public void getRemoteResponseTestDeleteInvalid(){
227 Map<String,String> paramsMap = new HashMap<>();
228 paramsMap.put("path", "http://localhost:8080");
229 paramsMap.put("methodType","delete");
230 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
235 public void generateParametesMapTest(){
236 Map<String, String> paramsMap = JujuVnfmRestfulUtil.generateParametesMap("http://localhost:8080", "get", "openoapi/test", "test");
237 assertTrue("http://localhost:8080".equals(paramsMap.get("url")) && "get".equals(paramsMap.get("methodType"))
238 && "openoapi/test".equals(paramsMap.get("path")) && "test".equals(paramsMap.get("authMode")));
241 public void testPrivateConstructor() throws Exception {
242 Constructor constructor = JujuVnfmRestfulUtil.class.getDeclaredConstructor();
243 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
244 constructor.setAccessible(true);
245 constructor.newInstance();