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.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.openo.baseservice.roa.util.restclient.RestfulResponse;
31 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
33 import net.sf.json.JSONObject;
41 * @version NFVO 0.5 Aug 10, 2016
43 public class VNFRestfulUtilTest {
46 public void testGetRestResByDefaultByNull() {
47 RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
52 public void testGetRestResByDefaultByGet() {
53 RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
54 assertNotNull(result);
58 public void testGetRestResByDefaultByPut() {
59 RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
60 assertNotNull(result);
64 public void testSendReqToApp() {
65 new MockUp<VNFRestfulUtil>() {
68 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
69 RestfulResponse restfulResponse = new RestfulResponse();
70 restfulResponse.setStatus(Constant.HTTP_OK);
71 String responseString = "{\"retCode\":1,\"data\":\"success\"}";
72 restfulResponse.setResponseJson(responseString);
73 return restfulResponse;
76 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
77 assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
80 /*@Test(expected = ExceptionInInitializerError.class)
81 public void testSendReqToAppByErrorMsg() {
82 new MockUp<VNFRestfulUtil>() {
85 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
86 RestfulResponse restfulResponse = new RestfulResponse();
87 restfulResponse.setStatus(Constant.HTTP_OK);
88 String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
89 restfulResponse.setResponseJson(responseString);
90 return restfulResponse;
93 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
94 assertEquals(Constant.REST_FAIL, result.get("retCode"));
98 public void testSendReqToAppByError() {
99 new MockUp<VNFRestfulUtil>() {
102 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
103 RestfulResponse restfulResponse = new RestfulResponse();
104 restfulResponse.setStatus(Constant.HTTP_OK);
105 String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
106 restfulResponse.setResponseJson(responseString);
107 return restfulResponse;
110 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
111 assertEquals(Constant.REST_FAIL, result.get("retCode"));
115 public void testSendReqToAppByFail() {
116 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
117 assertEquals(Constant.REST_FAIL, result.get("retCode"));
121 public void testSendReqToAppByVnfmInfoPut() {
122 JSONObject paraJson = new JSONObject();
123 JSONObject vnfmObj = new JSONObject();
124 vnfmObj.put("id", "id");
125 paraJson.put("vnfmInfo", vnfmObj);
126 JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", paraJson);
127 assertEquals(Constant.REST_FAIL, result.get("retCode"));
131 public void testGenerateParamsMap() {
132 Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path");
133 Map<String, String> paramsMap = new HashMap<String, String>(6);
134 paramsMap.put("url", "url");
135 paramsMap.put("methodType", "methodType");
136 paramsMap.put("path", "path");
137 paramsMap.put("authMode", "Certificate");
138 assertEquals(paramsMap, result);
142 public void testGenerateParamsMap2() {
143 Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
144 Map<String, String> paramsMap = new HashMap<String, String>(6);
145 paramsMap.put("url", "url");
146 paramsMap.put("methodType", "methodType");
147 paramsMap.put("path", "path");
148 paramsMap.put("authMode", "authMode");
149 assertEquals(paramsMap, result);
153 public void testGetResultToVnfmByVnfmInfoNull() {
154 JSONObject result = VNFRestfulUtil.getResultToVnfm(null, null);
156 JSONObject retJson = new JSONObject();
157 retJson.put("retCode", Constant.REST_FAIL);
158 retJson.put("data", "get null result");
159 assertEquals(retJson, result);
163 public void testGetResultToVnfmByVnfmInfoErrorMsg() {
164 JSONObject vnfmInfo = new JSONObject();
165 vnfmInfo.put("retCode", Constant.REST_FAIL);
166 vnfmInfo.put("msg", "ErrorMsg");
167 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
169 JSONObject retJson = new JSONObject();
170 retJson.put("retCode", Constant.REST_FAIL);
171 retJson.put("data", "ErrorMsg");
172 assertEquals(retJson, result);
176 public void testGetResultToVnfmByVnfmInfoError() {
177 JSONObject vnfmInfo = new JSONObject();
178 vnfmInfo.put("retCode", Constant.REST_FAIL);
179 JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
181 JSONObject retJson = new JSONObject();
182 retJson.put("retCode", Constant.REST_FAIL);
183 assertEquals(retJson, result);
186 public void getRemoteResponseTestInvalidGet(){
187 Map<String,String> paramsMap = new HashMap<>();
188 paramsMap.put("path", "http://localhost:8080");
189 paramsMap.put("url", "/openoapi/test");
190 paramsMap.put("methodType","get");
191 paramsMap.put("authMode","test");
192 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
193 assertTrue(resp == null);
197 public void getRemoteResponseTestInvalidPut(){
198 Map<String,String> paramsMap = new HashMap<>();
199 paramsMap.put("path", "http://localhost:8080");
200 paramsMap.put("url", "/openoapi/test");
201 paramsMap.put("methodType","put");
202 paramsMap.put("authMode","test");
203 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
204 assertTrue(resp == null);
208 public void getRemoteResponseTestInvalidPost(){
209 Map<String,String> paramsMap = new HashMap<>();
210 paramsMap.put("path", "http://localhost:8080");
211 paramsMap.put("url", "/openoapi/test");
212 paramsMap.put("methodType","post");
213 paramsMap.put("authMode","test");
214 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
215 assertTrue(resp == null);
218 public void getRemoteResponseTestInvalidDelete(){
219 Map<String,String> paramsMap = new HashMap<>();
220 paramsMap.put("path", "http://localhost:8080");
221 paramsMap.put("url", "/openoapi/test");
222 paramsMap.put("methodType","delete");
223 paramsMap.put("authMode","test");
224 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
225 assertTrue(resp == null);
229 public void getRemoteResponseTestFalseNfvoApp(){
230 Map<String,String> paramsMap = new HashMap<>();
231 paramsMap.put("path", "http://localhost:8080");
232 paramsMap.put("url", "/openoapi/test");
233 paramsMap.put("methodType","delete");
234 paramsMap.put("authMode","test");
235 RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", false);
236 assertTrue(resp == null);
240 public void sentEvtByRestTest(){
241 VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
245 public void sentEvtByRestTest2(){
246 JSONObject bodyParam = new JSONObject();
247 VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
251 public void testPrivateConstructor() throws Exception {
252 Constructor constructor = VNFRestfulUtil.class.getDeclaredConstructor();
253 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
254 constructor.setAccessible(true);
255 constructor.newInstance();