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 org.junit.Test;
23 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulOptions;
24 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
25 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
26 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
34 public class RestfulUtilTest {
37 public void testGetResponseObjWithTwoParams() {
38 mockGetResponseContent();
39 JSONObject result = RestfulUtil.getResponseObj(null, null);
40 JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}");
41 assertEquals(expectedResult, result);
44 private void mockGetResponseContent() {
45 new MockUp<RestfulUtil>() {
48 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
50 return "{\"ResponseContent\":\"123\"}";
55 private void mockGetResponseContentReturnNull() {
56 new MockUp<RestfulUtil>() {
59 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
67 public void testGetResponseObjWithThreeParams() {
68 new MockUp<RestfulUtil>() {
71 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
73 return "{\"ResponseContent\":\"123\"}";
76 JSONObject result = RestfulUtil.getResponseObj(null, null, null);
77 @SuppressWarnings("static-access")
78 JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}");
79 assertEquals(expectedResult, result);
83 public void testGetResponseObjExpections() {
84 new MockUp<RestfulUtil>() {
87 public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
89 throw new JSONException();
92 JSONObject result = RestfulUtil.getResponseObj(null, null, null);
93 JSONObject expectedResult = null;
94 assertEquals(expectedResult, result);
98 public void testGetRestResObjectsIsNull() {
99 mockGetResponseContentReturnNull();
100 RestfulResponse result = RestfulUtil.getRestRes(null, null);
101 RestfulResponse expectedResult = null;
102 assertEquals(expectedResult, result);
106 public void testGetRestResReflectiveOperationException() {
107 RestfulResponse result = RestfulUtil.getRestRes("123", "get");
108 RestfulResponse expectedResult = null;
109 assertEquals(expectedResult, result);
113 public void testGetRestRes() {
114 RestfulResponse result = RestfulUtil.getRestRes("async123", new RestfulResponse());
115 RestfulResponse expectedResult = null;
116 assertEquals(expectedResult, result);
120 public void testGetResponse() throws ServiceException {
121 new MockUp<RestfulUtil>() {
124 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
125 return "{\"ResponseContent\":\"123\",\"data\":[\"datas\"]}";
128 JSONArray result = RestfulUtil.getResponseRes(null, null);
129 JSONArray expectedResult = JSONArray.fromObject("[\"datas\"]");
130 assertEquals(expectedResult, result);
134 public void testGetResponseExceptions() throws ServiceException {
135 new MockUp<RestfulUtil>() {
138 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
139 return "{\"ResponseContent\":\"123\",}";
143 RestfulUtil.getResponseRes(null, null);
144 } catch(ServiceException e) {
150 public void testGgetResponseRes() throws ServiceException {
151 new MockUp<RestfulUtil>() {
154 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
155 return "{\"ResponseContent\":\"123\",}";
159 RestfulUtil.getResponseRes(null, null, null);
160 } catch(ServiceException e) {
166 public void testGgetResponseResException() throws ServiceException {
167 new MockUp<RestfulUtil>() {
170 public String getResponseContent(String url, RestfulParametes restParametes, String type) {
175 RestfulUtil.getResponseRes(null, null, null);
176 } catch(ServiceException e) {