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.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Modifier;
26 import java.util.HashMap;
29 import org.junit.Test;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VNFRestfulUtil;
31 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
32 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
34 import net.sf.json.JSONObject;
42 * @version NFVO 0.5 Aug 10, 2016
44 public class VNFRestfulUtilTest {
47 public void testGetRestResByDefaultByNull() {
48 RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
53 public void testGetRestResByDefaultByGet() {
54 RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
55 assertNotNull(result);
59 public void testGetRestResByDefaultByPut() {
60 RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
61 assertNotNull(result);
65 public void testSendReqToApp() {
66 new MockUp<VNFRestfulUtil>() {
69 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
70 RestfulResponse restfulResponse = new RestfulResponse();
71 restfulResponse.setStatus(Constant.HTTP_OK);
72 String responseString = "{\"retCode\":1,\"data\":\"success\"}";
73 restfulResponse.setResponseJson(responseString);
74 return restfulResponse;
77 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
78 assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
81 /*@Test(expected = ExceptionInInitializerError.class)
82 public void testSendReqToAppByErrorMsg() {
83 new MockUp<VNFRestfulUtil>() {
86 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
87 RestfulResponse restfulResponse = new RestfulResponse();
88 restfulResponse.setStatus(Constant.HTTP_OK);
89 String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
90 restfulResponse.setResponseJson(responseString);
91 return restfulResponse;
94 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
95 assertEquals(Constant.REST_FAIL, result.get("retCode"));
99 public void testSendReqToAppByError() {
100 new MockUp<VNFRestfulUtil>() {
103 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
104 RestfulResponse restfulResponse = new RestfulResponse();
105 restfulResponse.setStatus(Constant.HTTP_OK);
106 String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
107 restfulResponse.setResponseJson(responseString);
108 return restfulResponse;
111 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
112 assertEquals(Constant.REST_FAIL, result.get("retCode"));
116 public void testSendReqToAppByFail() {
117 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
118 assertEquals(Constant.REST_FAIL, result.get("retCode"));
122 public void testSendReqToAppByVnfmInfoPut() {
123 JSONObject paraJson = new JSONObject();
124 JSONObject vnfmObj = new JSONObject();
125 vnfmObj.put("id", "id");
126 paraJson.put("vnfmInfo", vnfmObj);
127 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", paraJson);
128 assertEquals(Constant.REST_FAIL, result.get("retCode"));
132 public void testGenerateParamsMap() {
133 Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path");
134 Map<String, String> paramsMap = new HashMap<String, String>(6);
135 paramsMap.put("url", "url");
136 paramsMap.put("methodType", "methodType");
137 paramsMap.put("path", "path");
138 paramsMap.put("authMode", "Certificate");
139 assertEquals(paramsMap, result);
143 public void testGenerateParamsMap2() {
144 Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
145 Map<String, String> paramsMap = new HashMap<String, String>(6);
146 paramsMap.put("url", "url");
147 paramsMap.put("methodType", "methodType");
148 paramsMap.put("path", "path");
149 paramsMap.put("authMode", "authMode");
150 assertEquals(paramsMap, result);
154 public void testGetResultToVnfmByVnfmInfoNull() {
155 JSONObject result = VNFRestfulUtil.getResultToVnfm(null, null);
157 JSONObject retJson = new JSONObject();
158 retJson.put("retCode", Constant.REST_FAIL);
159 retJson.put("data", "get null result");
160 assertEquals(retJson, result);
164 public void testGetResultToVnfmByVnfmInfoErrorMsg() {
165 JSONObject vnfmInfo = new JSONObject();
166 vnfmInfo.put("retCode", Constant.REST_FAIL);
167 vnfmInfo.put("msg", "ErrorMsg");
168 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
170 JSONObject retJson = new JSONObject();
171 retJson.put("retCode", Constant.REST_FAIL);
172 retJson.put("data", "ErrorMsg");
173 assertEquals(retJson, result);
177 public void testGetResultToVnfmByVnfmInfoError() {
178 JSONObject vnfmInfo = new JSONObject();
179 vnfmInfo.put("retCode", Constant.REST_FAIL);
180 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
182 JSONObject retJson = new JSONObject();
183 retJson.put("retCode", Constant.REST_FAIL);
184 assertEquals(retJson, result);
187 public void getRemoteResponseTestInvalidGet(){
188 Map<String,String> paramsMap = new HashMap<>();
189 paramsMap.put("path", "http://localhost:8080");
190 paramsMap.put("url", "/openoapi/test");
191 paramsMap.put("methodType","get");
192 paramsMap.put("authMode","test");
193 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
194 assertTrue(resp == null);
198 public void getRemoteResponseTestInvalidPut(){
199 Map<String,String> paramsMap = new HashMap<>();
200 paramsMap.put("path", "http://localhost:8080");
201 paramsMap.put("url", "/openoapi/test");
202 paramsMap.put("methodType","put");
203 paramsMap.put("authMode","test");
204 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
205 assertTrue(resp == null);
209 public void getRemoteResponseTestInvalidPost(){
210 Map<String,String> paramsMap = new HashMap<>();
211 paramsMap.put("path", "http://localhost:8080");
212 paramsMap.put("url", "/openoapi/test");
213 paramsMap.put("methodType","post");
214 paramsMap.put("authMode","test");
215 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
216 assertTrue(resp == null);
219 public void getRemoteResponseTestInvalidDelete(){
220 Map<String,String> paramsMap = new HashMap<>();
221 paramsMap.put("path", "http://localhost:8080");
222 paramsMap.put("url", "/openoapi/test");
223 paramsMap.put("methodType","delete");
224 paramsMap.put("authMode","test");
225 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
226 assertTrue(resp == null);
230 public void getRemoteResponseTestFalseNfvoApp(){
231 Map<String,String> paramsMap = new HashMap<>();
232 paramsMap.put("path", "http://localhost:8080");
233 paramsMap.put("url", "/openoapi/test");
234 paramsMap.put("methodType","delete");
235 paramsMap.put("authMode","test");
236 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", false);
237 assertTrue(resp == null);
241 public void sentEvtByRestTest(){
242 VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
246 public void sentEvtByRestTest2(){
247 JSONObject bodyParam = new JSONObject();
248 VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
252 public void testPrivateConstructor() throws Exception {
253 Constructor constructor = VNFRestfulUtil.class.getDeclaredConstructor();
254 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
255 constructor.setAccessible(true);
256 constructor.newInstance();