2 * Copyright 2016 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.resmanagement.common.util;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
22 import java.util.HashMap;
25 import org.junit.Test;
26 import org.openo.baseservice.remoteservice.exception.ServiceException;
27 import org.openo.baseservice.roa.util.restclient.RestfulOptions;
28 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.nfvo.resmanagement.common.constant.Constant;
34 import net.sf.json.JSONArray;
35 import net.sf.json.JSONException;
36 import net.sf.json.JSONObject;
38 public class RestfulUtilTest {
41 public void testGetResponseObjWithTwoParams() {
42 JSONObject result = RestfulUtil.getResponseObj(null, null);
43 JSONObject expectedResult = null;
44 assertEquals(expectedResult, result);
48 public void testGetResponseObjWithThreeParams() {
49 new MockUp<RestfulUtil>() {
52 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
54 return "{\"ResponseContent\":\"123\"}";
57 JSONObject result = RestfulUtil.getResponseObj(null, null, null);
58 @SuppressWarnings("static-access")
59 JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}");
60 assertEquals(expectedResult, result);
64 public void testGetResponseObjExpections() {
65 new MockUp<RestfulUtil>() {
68 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
70 throw new JSONException();
73 JSONObject result = RestfulUtil.getResponseObj(null, null, null);
74 JSONObject expectedResult = null;
75 assertEquals(expectedResult, result);
79 public void testGetResponseContent() {
80 String result = RestfulUtil.getResponseContent(null, null, null);
81 String expectedResult = null;
82 assertEquals(expectedResult, result);
86 public void testGetResponseMap() {
87 Map<String, Object> result = RestfulUtil.getResponseMap(null, null, null, null);
88 Map<String, Object> expectedResult = new HashMap<String, Object>(10);
89 expectedResult.put(Constant.RESPONSE_CONTENT, null);
90 expectedResult.put(Constant.STATUS_CODE, -1);
91 assertEquals(expectedResult, result);
95 public void testGetResponseContentMap() {
96 Map<String, Object> result = RestfulUtil.getResponseContentMap(null, null);
97 Map<String, Object> expectedResult = new HashMap<String, Object>(10);
98 expectedResult.put(Constant.RESPONSE_CONTENT, null);
99 expectedResult.put(Constant.STATUS_CODE, -1);
100 assertEquals(expectedResult, result);
104 public void testGetResponseContentWithFourParams() {
105 new MockUp<RestfulResponse>() {
108 public int getStatus() {
112 String result = RestfulUtil.getResponseContent(null, null, null);
113 String expectedResult = null;
114 assertEquals(expectedResult, result);
118 public void testGetRestfulResponse() {
119 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, null);
120 RestfulResponse expectedResult = new RestfulResponse();
121 assertEquals(expectedResult.getStatus(), result.getStatus());
125 public void testRestfulResponse() {
126 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "get");
127 RestfulResponse expectedResult = new RestfulResponse();
128 assertEquals(expectedResult.getStatus(), result.getStatus());
132 public void testRestfulResponse1() {
133 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "add");
134 RestfulResponse expectedResult = new RestfulResponse();
135 assertEquals(expectedResult.getStatus(), result.getStatus());
139 public void testRestfulResponse2() {
140 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "put");
141 RestfulResponse expectedResult = new RestfulResponse();
142 assertEquals(expectedResult.getStatus(), result.getStatus());
146 public void testRestfulResponse3() {
147 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "delete");
148 RestfulResponse expectedResult = new RestfulResponse();
149 assertEquals(expectedResult.getStatus(), result.getStatus());
153 public void testGetRestResObjectsIsNull() {
154 RestfulResponse result = RestfulUtil.getRestRes(null, null);
155 RestfulResponse expectedResult = null;
156 assertEquals(expectedResult, result);
160 public void testGetRestResReflectiveOperationException() {
161 RestfulResponse result = RestfulUtil.getRestRes("123", "get");
162 RestfulResponse expectedResult = null;
163 assertEquals(expectedResult, result);
167 public void testGetRestRes() {
168 RestfulResponse result = RestfulUtil.getRestRes("async123", new RestfulResponse());
169 RestfulResponse expectedResult = null;
170 assertEquals(expectedResult, result);
174 public void testGetResponseResResultIsNull() throws ServiceException {
176 RestfulUtil.getResponseRes(null, null);
177 } catch (ServiceException e) {
183 public void testGetResponse() throws ServiceException {
184 new MockUp<RestfulUtil>() {
187 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
188 return "{\"ResponseContent\":\"123\",\"data\":[\"datas\"]}";
191 JSONArray result = RestfulUtil.getResponseRes(null, null);
192 JSONArray expectedResult = JSONArray.fromObject("[\"datas\"]");
193 assertEquals(expectedResult, result);
197 public void testGetResponseExceptions() throws ServiceException {
198 new MockUp<RestfulUtil>() {
201 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
202 return "{\"ResponseContent\":\"123\",}";
206 RestfulUtil.getResponseRes(null, null);
207 } catch (ServiceException e) {
213 public void testGgetResponseRes() throws ServiceException {
214 new MockUp<RestfulUtil>() {
217 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
218 return "{\"ResponseContent\":\"123\",}";
222 RestfulUtil.getResponseRes(null, null, null);
223 } catch (ServiceException e) {
229 public void testGgetResponseResException() throws ServiceException {
230 new MockUp<RestfulUtil>() {
233 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
238 RestfulUtil.getResponseRes(null, null, null);
239 } catch (ServiceException e) {