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.onap.vfc.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.onap.vfc.nfvo.resmanagement.common.constant.Constant;
27 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
28 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
29 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulOptions;
30 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
31 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
35 import net.sf.json.JSONArray;
36 import net.sf.json.JSONException;
37 import net.sf.json.JSONObject;
39 public class RestfulUtilTest {
42 public void testGetResponseObjWithTwoParams() {
43 JSONObject result = RestfulUtil.getResponseObj(null, null);
44 JSONObject expectedResult = null;
45 assertEquals(expectedResult, result);
49 public void testGetResponseObjWithThreeParams() {
50 new MockUp<RestfulUtil>() {
53 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
55 return "{\"ResponseContent\":\"123\"}";
58 JSONObject result = RestfulUtil.getResponseObj(null, null, null);
59 @SuppressWarnings("static-access")
60 JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}");
61 assertEquals(expectedResult, result);
65 public void testGetResponseObjExpections() {
66 new MockUp<RestfulUtil>() {
69 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
71 throw new JSONException();
74 JSONObject result = RestfulUtil.getResponseObj(null, null, null);
75 JSONObject expectedResult = null;
76 assertEquals(expectedResult, result);
80 public void testGetResponseContent() {
81 String result = RestfulUtil.getResponseContent(null, null, null);
82 String expectedResult = null;
83 assertEquals(expectedResult, result);
87 public void testGetResponseMap() {
88 Map<String, Object> result = RestfulUtil.getResponseMap(null, null, null, null);
89 Map<String, Object> expectedResult = new HashMap<String, Object>(10);
90 expectedResult.put(Constant.RESPONSE_CONTENT, null);
91 expectedResult.put(Constant.STATUS_CODE, -1);
92 assertEquals(expectedResult, result);
96 public void testGetResponseContentMap() {
97 Map<String, Object> result = RestfulUtil.getResponseContentMap(null, null);
98 Map<String, Object> expectedResult = new HashMap<String, Object>(10);
99 expectedResult.put(Constant.RESPONSE_CONTENT, null);
100 expectedResult.put(Constant.STATUS_CODE, -1);
101 assertEquals(expectedResult, result);
105 public void testGetResponseContentWithFourParams() {
106 new MockUp<RestfulResponse>() {
109 public int getStatus() {
113 String result = RestfulUtil.getResponseContent(null, null, null);
114 String expectedResult = null;
115 assertEquals(expectedResult, result);
119 public void testGetRestfulResponse() {
120 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, null);
121 RestfulResponse expectedResult = new RestfulResponse();
122 assertEquals(expectedResult.getStatus(), result.getStatus());
126 public void testRestfulResponse() {
127 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "get");
128 RestfulResponse expectedResult = new RestfulResponse();
129 assertEquals(expectedResult.getStatus(), result.getStatus());
133 public void testRestfulResponse1() {
134 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "add");
135 RestfulResponse expectedResult = new RestfulResponse();
136 assertEquals(expectedResult.getStatus(), result.getStatus());
140 public void testRestfulResponse2() {
141 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "put");
142 RestfulResponse expectedResult = new RestfulResponse();
143 assertEquals(expectedResult.getStatus(), result.getStatus());
147 public void testRestfulResponse3() {
148 RestfulResponse result = RestfulUtil.getRestfulResponse(null, null, "delete");
149 RestfulResponse expectedResult = new RestfulResponse();
150 assertEquals(expectedResult.getStatus(), result.getStatus());
154 public void testGetRestResObjectsIsNull() {
155 RestfulResponse result = RestfulUtil.getRestRes(null, null);
156 RestfulResponse expectedResult = null;
157 assertEquals(expectedResult, result);
161 public void testGetRestResReflectiveOperationException() {
162 RestfulResponse result = RestfulUtil.getRestRes("123", "get");
163 RestfulResponse expectedResult = null;
164 assertEquals(expectedResult, result);
168 public void testGetRestRes() {
169 RestfulResponse result = RestfulUtil.getRestRes("async123", new RestfulResponse());
170 RestfulResponse expectedResult = null;
171 assertEquals(expectedResult, result);
175 public void testGetResponseResResultIsNull() throws ServiceException {
177 RestfulUtil.getResponseRes(null, null);
178 } catch (ServiceException e) {
184 public void testGetResponse() throws ServiceException {
185 new MockUp<RestfulUtil>() {
188 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
189 return "{\"ResponseContent\":\"123\",\"data\":[\"datas\"]}";
192 JSONArray result = RestfulUtil.getResponseRes(null, null);
193 JSONArray expectedResult = JSONArray.fromObject("[\"datas\"]");
194 assertEquals(expectedResult, result);
198 public void testGetResponseExceptions() throws ServiceException {
199 new MockUp<RestfulUtil>() {
202 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
203 return "{\"ResponseContent\":\"123\",}";
207 RestfulUtil.getResponseRes(null, null);
208 } catch (ServiceException e) {
214 public void testGgetResponseRes() throws ServiceException {
215 new MockUp<RestfulUtil>() {
218 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
219 return "{\"ResponseContent\":\"123\",}";
223 RestfulUtil.getResponseRes(null, null, null);
224 } catch (ServiceException e) {
230 public void testGgetResponseResException() throws ServiceException {
231 new MockUp<RestfulUtil>() {
234 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
239 RestfulUtil.getResponseRes(null, null, null);
240 } catch (ServiceException e) {