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.
16 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
25 import java.util.HashMap;
28 import org.junit.Test;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
31 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
35 import net.sf.json.JSONObject;
37 public class VnfmRestfulUtilTest {
40 public void testGetRestResByDefaultByNull() {
41 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
45 public void testGetRestResByDefaultByGet() {
46 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
47 assertNotNull(result);
51 public void testGetRestResByDefaultByPut() {
52 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
53 assertNotNull(result);
58 public void testSendReqToAppByFail() {
59 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
60 assertEquals(Constant.REST_FAIL, result.get("retCode"));
64 public void testSendReqToAppByVnfmInfoPut() {
65 JSONObject paraJson = new JSONObject();
66 JSONObject vnfmObj = new JSONObject();
67 vnfmObj.put("id", "id");
68 paraJson.put("vnfmInfo", vnfmObj);
69 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
70 assertEquals(Constant.REST_FAIL, result.get("retCode"));
74 public void testSendReqToAppByVnfmInfoPutNormal() {
75 new MockUp<VnfmRestfulUtil>(){
77 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
78 RestfulResponse resp = new RestfulResponse();
80 JSONObject obj = new JSONObject();
81 obj.put("retCode", 1);
82 obj.put("data", new JSONObject());
83 resp.setResponseJson(obj.toString());
87 JSONObject paraJson = new JSONObject();
88 JSONObject vnfmObj = new JSONObject();
89 vnfmObj.put("id", "id");
90 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
91 assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
94 public void testSendReqToAppByVnfmInfoPutNormal2() {
95 new MockUp<VnfmRestfulUtil>(){
97 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
98 RestfulResponse resp = new RestfulResponse();
100 JSONObject obj = new JSONObject();
101 obj.put("retCode", -1);
102 obj.put("data", new JSONObject());
103 resp.setResponseJson(obj.toString());
107 JSONObject paraJson = new JSONObject();
108 JSONObject vnfmObj = new JSONObject();
109 vnfmObj.put("id", "id");
110 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
111 assertEquals(Constant.REST_FAIL, result.get("retCode"));
115 public void testGenerateParamsMap() {
116 Map<String, String> result = VnfmRestfulUtil.generateParamsMap("url", "methodType", "path","auth");
117 Map<String, String> paramsMap = new HashMap<String, String>(6);
118 paramsMap.put("url", "url");
119 paramsMap.put("methodType", "methodType");
120 paramsMap.put("path", "path");
121 paramsMap.put("authMode", "auth");
122 assertEquals(paramsMap, result);
125 public void getRemoteResponseTestInvalidGet(){
126 Map<String,String> paramsMap = new HashMap<>();
127 paramsMap.put("path", "/test/abc");
128 paramsMap.put("url", "http://localhost:8080");
129 paramsMap.put("methodType","get");
130 paramsMap.put("authMode","test");
131 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
132 assertTrue(resp == null);
136 public void getRemoteResponseTestInvalidPut(){
137 Map<String,String> paramsMap = new HashMap<>();
138 paramsMap.put("path", "/test/abc");
139 paramsMap.put("url", "http://localhost:8080");
140 paramsMap.put("methodType","put");
141 paramsMap.put("authMode","test");
142 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
143 assertTrue(resp == null);
147 public void getRemoteResponseTestInvalidPost(){
148 Map<String,String> paramsMap = new HashMap<>();
149 paramsMap.put("path", "/openoapi/test");
150 paramsMap.put("url", "http://localhost:8080");
151 paramsMap.put("methodType","post");
152 paramsMap.put("authMode","test");
153 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
154 assertTrue(resp == null);
157 public void getRemoteResponseTestInvalidDelete(){
158 Map<String,String> paramsMap = new HashMap<>();
159 paramsMap.put("path", "/openoapi/test");
160 paramsMap.put("url", "http://localhost:8080");
161 paramsMap.put("methodType","delete");
162 paramsMap.put("authMode","test");
163 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
164 assertTrue(resp == null);
168 public void getRemoteResponse2TestInvalidGet(){
169 String url = "http://localhost:8080";
170 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "get", "test123");
171 assertTrue(resp == null);
175 public void getRemoteResponse2TestInvalidPut(){
176 String url = "http://localhost:8080";
177 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "put", "test123");
178 assertTrue(resp == null);
182 public void getRemoteResponse2TestInvalidPost(){
183 String url = "http://localhost:8080";
184 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "post", "test123");
185 assertTrue(resp == null);
188 public void getRemoteResponse2TestInvalidDelete(){
190 String url = "http://localhost:8080";
192 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "delete", "test123");
193 assertTrue(resp == null);
197 public void getRemoteResponseTestFalseNfvoApp(){
198 Map<String,String> paramsMap = new HashMap<>();
199 paramsMap.put("path", "/openoapi/test");
200 paramsMap.put("url", "http://localhost:8080");
201 paramsMap.put("methodType","delete");
202 paramsMap.put("authMode","test");
203 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
204 assertTrue(resp == null);
208 public void sentEvtByRestTest(){
209 VnfmRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
213 public void sentEvtByRestTest2(){
214 JSONObject bodyParam = new JSONObject();
215 VnfmRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
219 public void testPrivateConstructor() throws Exception {
220 Constructor constructor = VnfmRestfulUtil.class.getDeclaredConstructor();
221 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
222 constructor.setAccessible(true);
223 constructor.newInstance();