e75a823e46e26dec9be7d17ec79945d448cb90cc
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.common.util;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21
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;
27
28 import mockit.Mock;
29 import mockit.MockUp;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33
34 public class RestfulUtilTest {
35
36     @Test
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);
42     }
43
44     private void mockGetResponseContent() {
45         new MockUp<RestfulUtil>() {
46
47             @Mock
48             public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
49                     String type) {
50                 return "{\"ResponseContent\":\"123\"}";
51             }
52         };
53     }
54
55     private void mockGetResponseContentReturnNull() {
56         new MockUp<RestfulUtil>() {
57
58             @Mock
59             public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
60                     String type) {
61                 return null;
62             }
63         };
64     }
65
66     @Test
67     public void testGetResponseObjWithThreeParams() {
68         new MockUp<RestfulUtil>() {
69
70             @Mock
71             public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
72                     String type) {
73                 return "{\"ResponseContent\":\"123\"}";
74             }
75         };
76         JSONObject result = RestfulUtil.getResponseObj(null, null, null);
77         @SuppressWarnings("static-access")
78         JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}");
79         assertEquals(expectedResult, result);
80     }
81
82     @Test
83     public void testGetResponseObjExpections() {
84         new MockUp<RestfulUtil>() {
85
86             @Mock
87             public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
88                     String type) {
89                 throw new JSONException();
90             }
91         };
92         JSONObject result = RestfulUtil.getResponseObj(null, null, null);
93         JSONObject expectedResult = null;
94         assertEquals(expectedResult, result);
95     }
96
97     @Test
98     public void testGetRestResObjectsIsNull() {
99         mockGetResponseContentReturnNull();
100         RestfulResponse result = RestfulUtil.getRestRes(null, null);
101         RestfulResponse expectedResult = null;
102         assertEquals(expectedResult, result);
103     }
104
105     @Test
106     public void testGetRestResReflectiveOperationException() {
107         RestfulResponse result = RestfulUtil.getRestRes("123", "get");
108         RestfulResponse expectedResult = null;
109         assertEquals(expectedResult, result);
110     }
111
112     @Test
113     public void testGetRestRes() {
114         RestfulResponse result = RestfulUtil.getRestRes("async123", new RestfulResponse());
115         RestfulResponse expectedResult = null;
116         assertEquals(expectedResult, result);
117     }
118
119     @Test
120     public void testGetResponse() throws ServiceException {
121         new MockUp<RestfulUtil>() {
122
123             @Mock
124             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
125                 return "{\"ResponseContent\":\"123\",\"data\":[\"datas\"]}";
126             }
127         };
128         JSONArray result = RestfulUtil.getResponseRes(null, null);
129         JSONArray expectedResult = JSONArray.fromObject("[\"datas\"]");
130         assertEquals(expectedResult, result);
131     }
132
133     @Test
134     public void testGetResponseExceptions() throws ServiceException {
135         new MockUp<RestfulUtil>() {
136
137             @Mock
138             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
139                 return "{\"ResponseContent\":\"123\",}";
140             }
141         };
142         try {
143             RestfulUtil.getResponseRes(null, null);
144         } catch(ServiceException e) {
145             assertTrue(true);
146         }
147     }
148
149     @Test
150     public void testGgetResponseRes() throws ServiceException {
151         new MockUp<RestfulUtil>() {
152
153             @Mock
154             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
155                 return "{\"ResponseContent\":\"123\",}";
156             }
157         };
158         try {
159             RestfulUtil.getResponseRes(null, null, null);
160         } catch(ServiceException e) {
161             assertTrue(true);
162         }
163     }
164
165     @Test
166     public void testGgetResponseResException() throws ServiceException {
167         new MockUp<RestfulUtil>() {
168
169             @Mock
170             public String getResponseContent(String url, RestfulParametes restParametes, String type) {
171                 return null;
172             }
173         };
174         try {
175             RestfulUtil.getResponseRes(null, null, null);
176         } catch(ServiceException e) {
177             assertTrue(true);
178         }
179     }
180
181 }