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.openo.nfvo.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.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
34 import net.sf.json.JSONObject;
36 public class VnfmRestfulUtilTest {
39 public void testGetRestResByDefaultByNull() {
40 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
44 public void testGetRestResByDefaultByGet() {
45 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
46 assertNotNull(result);
50 public void testGetRestResByDefaultByPut() {
51 RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
52 assertNotNull(result);
57 public void testSendReqToAppByFail() {
58 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
59 assertEquals(Constant.REST_FAIL, result.get("retCode"));
63 public void testSendReqToAppByVnfmInfoPut() {
64 JSONObject paraJson = new JSONObject();
65 JSONObject vnfmObj = new JSONObject();
66 vnfmObj.put("id", "id");
67 paraJson.put("vnfmInfo", vnfmObj);
68 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
69 assertEquals(Constant.REST_FAIL, result.get("retCode"));
73 public void testSendReqToAppByVnfmInfoPutNormal() {
74 new MockUp<VnfmRestfulUtil>(){
76 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
77 RestfulResponse resp = new RestfulResponse();
79 JSONObject obj = new JSONObject();
80 obj.put("retCode", 1);
81 obj.put("data", new JSONObject());
82 resp.setResponseJson(obj.toString());
86 JSONObject paraJson = new JSONObject();
87 JSONObject vnfmObj = new JSONObject();
88 vnfmObj.put("id", "id");
89 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
90 assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
93 public void testSendReqToAppByVnfmInfoPutNormal2() {
94 new MockUp<VnfmRestfulUtil>(){
96 public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
97 RestfulResponse resp = new RestfulResponse();
99 JSONObject obj = new JSONObject();
100 obj.put("retCode", -1);
101 obj.put("data", new JSONObject());
102 resp.setResponseJson(obj.toString());
106 JSONObject paraJson = new JSONObject();
107 JSONObject vnfmObj = new JSONObject();
108 vnfmObj.put("id", "id");
109 JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
110 assertEquals(Constant.REST_FAIL, result.get("retCode"));
114 public void testGenerateParamsMap() {
115 Map<String, String> result = VnfmRestfulUtil.generateParamsMap("url", "methodType", "path","auth");
116 Map<String, String> paramsMap = new HashMap<String, String>(6);
117 paramsMap.put("url", "url");
118 paramsMap.put("methodType", "methodType");
119 paramsMap.put("path", "path");
120 paramsMap.put("authMode", "auth");
121 assertEquals(paramsMap, result);
124 public void getRemoteResponseTestInvalidGet(){
125 Map<String,String> paramsMap = new HashMap<>();
126 paramsMap.put("path", "/test/abc");
127 paramsMap.put("url", "http://localhost:8080");
128 paramsMap.put("methodType","get");
129 paramsMap.put("authMode","test");
130 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
131 assertTrue(resp == null);
135 public void getRemoteResponseTestInvalidPut(){
136 Map<String,String> paramsMap = new HashMap<>();
137 paramsMap.put("path", "/test/abc");
138 paramsMap.put("url", "http://localhost:8080");
139 paramsMap.put("methodType","put");
140 paramsMap.put("authMode","test");
141 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
142 assertTrue(resp == null);
146 public void getRemoteResponseTestInvalidPost(){
147 Map<String,String> paramsMap = new HashMap<>();
148 paramsMap.put("path", "/openoapi/test");
149 paramsMap.put("url", "http://localhost:8080");
150 paramsMap.put("methodType","post");
151 paramsMap.put("authMode","test");
152 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
153 assertTrue(resp == null);
156 public void getRemoteResponseTestInvalidDelete(){
157 Map<String,String> paramsMap = new HashMap<>();
158 paramsMap.put("path", "/openoapi/test");
159 paramsMap.put("url", "http://localhost:8080");
160 paramsMap.put("methodType","delete");
161 paramsMap.put("authMode","test");
162 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
163 assertTrue(resp == null);
167 public void getRemoteResponse2TestInvalidGet(){
168 String url = "http://localhost:8080";
169 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "get", "test123");
170 assertTrue(resp == null);
174 public void getRemoteResponse2TestInvalidPut(){
175 String url = "http://localhost:8080";
176 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "put", "test123");
177 assertTrue(resp == null);
181 public void getRemoteResponse2TestInvalidPost(){
182 String url = "http://localhost:8080";
183 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "post", "test123");
184 assertTrue(resp == null);
187 public void getRemoteResponse2TestInvalidDelete(){
189 String url = "http://localhost:8080";
191 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "delete", "test123");
192 assertTrue(resp == null);
196 public void getRemoteResponseTestFalseNfvoApp(){
197 Map<String,String> paramsMap = new HashMap<>();
198 paramsMap.put("path", "/openoapi/test");
199 paramsMap.put("url", "http://localhost:8080");
200 paramsMap.put("methodType","delete");
201 paramsMap.put("authMode","test");
202 RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
203 assertTrue(resp == null);
207 public void sentEvtByRestTest(){
208 VnfmRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
212 public void sentEvtByRestTest2(){
213 JSONObject bodyParam = new JSONObject();
214 VnfmRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
218 public void testPrivateConstructor() throws Exception {
219 Constructor constructor = VnfmRestfulUtil.class.getDeclaredConstructor();
220 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
221 constructor.setAccessible(true);
222 constructor.newInstance();