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.onap.vfc.nfvo.vnfm.gvnfm.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.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.JujuVnfmRestfulUtil;
28 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VNFRestfulUtil;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
30 import org.openo.baseservice.remoteservice.exception.ServiceException;
31 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
32 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
36 import net.sf.json.JSONObject;
44 * @version NFVO 0.5 Aug 10, 2016
46 public class JujuVnfmRestfulUtilTest {
50 public void testGetRemoteResponseByVnfmInfoNull() {
51 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(new HashMap(), null);
52 assertEquals(null, result);
56 public void testGetRemoteResponseByPost() {
57 Map testMap = new HashMap();
58 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
59 testMap.put("methodType","post");
60 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
62 assertEquals(null, result);
66 public void testGetRemoteResponseByVnfmInfo() {
67 Map testMap = new HashMap();
68 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
69 testMap.put("methodType","get");
70 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
72 assertEquals(null, result);
76 public void testGetRemoteResponseByVnfmInfoPut() {
77 Map testMap = new HashMap();
78 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
79 testMap.put("methodType","put");
80 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
82 assertEquals(null, result);
86 public void testGetRemoteResponseByVnfmInfoDelete() {
87 Map testMap = new HashMap();
88 testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
89 testMap.put("methodType","delete");
90 RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
92 assertEquals(null, result);
96 public void getVimResponseContentGetInvalid(){
97 RestfulParametes restParametes = new RestfulParametes();
98 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "put");
99 assertTrue(result.isEmpty());
102 public void getVimResponseContentAddInvalid(){
103 RestfulParametes restParametes = new RestfulParametes();
104 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "add");
105 assertTrue(result.isEmpty());
109 public void getVimResponseContentPutInvalid(){
110 RestfulParametes restParametes = new RestfulParametes();
111 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "put");
112 assertTrue(result.isEmpty());
115 public void getVimResponseContentDeleteInvalid(){
116 RestfulParametes restParametes = new RestfulParametes();
117 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "delete");
118 assertTrue(result.isEmpty());
122 public void getVimResponseContentPatchInvalid(){
123 RestfulParametes restParametes = new RestfulParametes();
124 Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "patch");
125 assertTrue(result.isEmpty());
129 public void testGetResultToVnfmByVnfmInfoErrorMsg() {
130 JSONObject vnfmInfo = new JSONObject();
131 vnfmInfo.put("retCode", Constant.REST_FAIL);
132 vnfmInfo.put("msg", "ErrorMsg");
133 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
135 JSONObject retJson = new JSONObject();
136 retJson.put("retCode", Constant.REST_FAIL);
137 retJson.put("data", "ErrorMsg");
138 assertEquals(retJson, result);
142 public void testGetResultToVnfmByVnfmInfoError() {
143 JSONObject vnfmInfo = new JSONObject();
144 vnfmInfo.put("retCode", Constant.REST_FAIL);
145 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
147 JSONObject retJson = new JSONObject();
148 retJson.put("retCode", Constant.REST_FAIL);
149 assertEquals(retJson, result);
153 public void vimRestfulResponseTestGetInvalid(){
154 RestfulParametes restParametes = new RestfulParametes();
155 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "get");
156 assertTrue(resp == null);
159 public void vimRestfulResponseTestAddInvalid(){
160 RestfulParametes restParametes = new RestfulParametes();
161 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "add");
162 assertTrue(resp == null);
166 public void vimRestfulResponseTestPutInvalid(){
167 RestfulParametes restParametes = new RestfulParametes();
168 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "put");
169 assertTrue(resp == null);
173 public void vimRestfulResponseTestDeleteInvalid(){
174 RestfulParametes restParametes = new RestfulParametes();
175 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "delete");
176 assertTrue(resp == null);
180 public void vimRestfulResponseTestPatchInvalid(){
181 RestfulParametes restParametes = new RestfulParametes();
182 RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "patch");
183 assertTrue(resp == null);
187 public void getRemoteResponseTestGetInvalid(){
188 Map<String,String> paramsMap = new HashMap<>();
189 paramsMap.put("path", "http://localhost:8080");
190 paramsMap.put("methodType","get");
191 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
197 public void getRemoteResponseTestGetHttpsInvalid(){
198 Map<String,String> paramsMap = new HashMap<>();
199 paramsMap.put("path", "http://localhost:8080");
200 paramsMap.put("methodType","get");
201 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", true);
207 public void getRemoteResponseTestPostInvalid(){
208 Map<String,String> paramsMap = new HashMap<>();
209 paramsMap.put("path", "http://localhost:8080");
210 paramsMap.put("methodType","post");
211 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
217 public void getRemoteResponseTestPutInvalid(){
218 Map<String,String> paramsMap = new HashMap<>();
219 paramsMap.put("path", "http://localhost:8080");
220 paramsMap.put("methodType","put");
221 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
227 public void getRemoteResponseTestDeleteInvalid(){
228 Map<String,String> paramsMap = new HashMap<>();
229 paramsMap.put("path", "http://localhost:8080");
230 paramsMap.put("methodType","delete");
231 RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
236 public void generateParametesMapTest(){
237 Map<String, String> paramsMap = JujuVnfmRestfulUtil.generateParametesMap("http://localhost:8080", "get", "openoapi/test", "test");
238 assertTrue("http://localhost:8080".equals(paramsMap.get("url")) && "get".equals(paramsMap.get("methodType"))
239 && "openoapi/test".equals(paramsMap.get("path")) && "test".equals(paramsMap.get("authMode")));
242 public void testPrivateConstructor() throws Exception {
243 Constructor constructor = JujuVnfmRestfulUtil.class.getDeclaredConstructor();
244 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
245 constructor.setAccessible(true);
246 constructor.newInstance();