Improve the code coverage
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / restclient / TestRestfulResponse.java
1 /*
2  * Copyright 2017 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.vnfm.svnfm.vnfmadapter.common.restclient;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertSame;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35
36 /**
37  * <br/>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version
43  */
44 public class TestRestfulResponse {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     /**
50      * <br/>
51      * 
52      * @throws java.lang.Exception
53      * @since
54      */
55     @BeforeClass
56     public static void setUpBeforeClass() throws Exception {
57     }
58
59     /**
60      * <br/>
61      * 
62      * @throws java.lang.Exception
63      * @since
64      */
65     @AfterClass
66     public static void tearDownAfterClass() throws Exception {
67     }
68
69     /**
70      * <br/>
71      * 
72      * @throws java.lang.Exception
73      * @since
74      */
75     @Before
76     public void setUp() throws Exception {
77     }
78
79     /**
80      * <br/>
81      * 
82      * @throws java.lang.Exception
83      * @since
84      */
85     @After
86     public void tearDown() throws Exception {
87     }
88
89     /**
90      * <br/>
91      * 
92      * @since
93      */
94     @Test
95     public void testGetStatus() {
96         final RestfulResponse response = new RestfulResponse();
97         int actual = response.getStatus();
98         int expected = -1;
99
100         assertEquals(expected, actual);
101         expected = 202;
102         response.setStatus(expected);
103         actual = response.getStatus();
104         assertEquals(expected, actual);
105     }
106
107     /**
108      * <br/>
109      * 
110      * @since
111      */
112     @Test
113     public void testSetStatus() {
114         final RestfulResponse response = new RestfulResponse();
115         final int expected = 10;
116         response.setStatus(expected);
117         final int actual = response.getStatus();
118         assertEquals(expected, actual);
119     }
120
121     /**
122      * <br/>
123      * 
124      * @since
125      */
126     @Test
127     public void testGetRespHeaderMap() {
128         final RestfulResponse response = new RestfulResponse();
129         Map<String, String> expected = response.getRespHeaderMap();
130         assertNull(expected);
131         expected = new HashMap<String, String>();
132         expected.put("key", "value");
133         response.setRespHeaderMap(expected);
134         final Map<String, String> actual = response.getRespHeaderMap();
135         assertNotNull(actual);
136         assertSame(actual, expected);
137
138     }
139
140     /**
141      * <br/>
142      * 
143      * @since
144      */
145     @Test
146     public void testSetRespHeaderMap() {
147         final RestfulResponse response = new RestfulResponse();
148         response.setRespHeaderMap(null);
149         Map<String, String> expected = response.getRespHeaderMap();
150         assertNull(expected);
151         expected = new HashMap<String, String>();
152         expected.put("key", "value");
153         response.setRespHeaderMap(expected);
154         final Map<String, String> actual = response.getRespHeaderMap();
155         assertNotNull(actual);
156         assertSame(actual, expected);
157     }
158
159     /**
160      * <br/>
161      * 
162      * @since
163      */
164     @Test
165     public void testGetRespHeaderInt() {
166         final RestfulResponse response = new RestfulResponse();
167         response.setRespHeaderMap(null);
168         int actual = response.getRespHeaderInt("somekey");
169         assertEquals(-1, actual);
170         final Map<String, String> headers = new HashMap<String, String>();
171         headers.put("key", "value");
172         headers.put("count", "1");
173         response.setRespHeaderMap(headers);
174         actual = response.getRespHeaderInt("somekey");
175         assertEquals(-1, actual);
176
177         actual = response.getRespHeaderInt("count");
178         assertEquals(1, actual);
179
180         thrown.expect(RuntimeException.class);
181         actual = response.getRespHeaderInt("key");
182         assertEquals(1, actual);
183
184     }
185
186     /**
187      * <br/>
188      * 
189      * @since
190      */
191     @Test
192     public void testGetRespHeaderLong() {
193         final RestfulResponse response = new RestfulResponse();
194         response.setRespHeaderMap(null);
195         long actual = response.getRespHeaderLong("somekey");
196         assertEquals(-1, actual);
197         final Map<String, String> headers = new HashMap<String, String>();
198         headers.put("key", "value");
199         headers.put("count", "1");
200         headers.put("max", "" + Long.MAX_VALUE);
201         headers.put("max++", Long.MAX_VALUE + 1 + "");
202         response.setRespHeaderMap(headers);
203         actual = response.getRespHeaderLong("somekey");
204         assertEquals(-1, actual);
205
206         actual = response.getRespHeaderLong("count");
207         assertEquals(1, actual);
208
209         actual = response.getRespHeaderLong("max");
210         assertEquals(Long.MAX_VALUE, actual);
211
212         actual = response.getRespHeaderLong("max++");
213         assertTrue(actual < 0);
214
215         thrown.expect(RuntimeException.class);
216         actual = response.getRespHeaderLong("key");
217         assertEquals(1, actual);
218     }
219
220     /**
221      * <br/>
222      * 
223      * @since
224      */
225     @Test
226     public void testGetRespHeaderStr() {
227         final RestfulResponse response = new RestfulResponse();
228         response.setRespHeaderMap(null);
229         String actual = response.getRespHeaderStr("somekey");
230         assertEquals(null, actual);
231         final Map<String, String> headers = new HashMap<String, String>();
232         headers.put("key", "value");
233         headers.put("count", "1");
234         headers.put("max", "" + Long.MAX_VALUE);
235         response.setRespHeaderMap(headers);
236         actual = response.getRespHeaderStr("somekey");
237         assertEquals(null, actual);
238
239         actual = response.getRespHeaderStr("key");
240         assertEquals("value", actual);
241
242     }
243
244     /**
245      * <br/>
246      * 
247      * @since
248      */
249     @Test
250     public void testGetResponseContent() {
251         final RestfulResponse response = new RestfulResponse();
252         assertEquals(null, response.getResponseContent());
253
254         final String content = "{ \"content\" = \"The response content\" }";
255         response.setResponseJson(content);
256         assertEquals(content, response.getResponseContent());
257     }
258
259     /**
260      * <br/>
261      * 
262      * @since
263      */
264     @Test
265     public void testSetResponseJson() {
266         final RestfulResponse response = new RestfulResponse();
267         assertEquals(null, response.getResponseContent());
268
269         final String content = "{ \"content\" = \"The response content\" }";
270         response.setResponseJson(content);
271         assertEquals(content, response.getResponseContent());
272     }
273 }