2 * Copyright (c) 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.baseservice.roa.util.restclient;
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;
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
33 import java.util.HashMap;
42 * @version SDNO 0.5 20-Jun-2016
44 public class TestRestfulResponse {
47 public ExpectedException thrown = ExpectedException.none();
52 * @throws java.lang.Exception
56 public static void setUpBeforeClass() throws Exception {
62 * @throws java.lang.Exception
66 public static void tearDownAfterClass() throws Exception {
72 * @throws java.lang.Exception
76 public void setUp() throws Exception {
82 * @throws java.lang.Exception
86 public void tearDown() throws Exception {
95 public void testGetStatus() {
96 final RestfulResponse response = new RestfulResponse();
97 int actual = response.getStatus();
100 assertEquals(expected, actual);
102 response.setStatus(expected);
103 actual = response.getStatus();
104 assertEquals(expected, actual);
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);
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);
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);
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);
177 actual = response.getRespHeaderInt("count");
178 assertEquals(1, actual);
180 thrown.expect(RuntimeException.class);
181 actual = response.getRespHeaderInt("key");
182 assertEquals(1, actual);
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);
206 actual = response.getRespHeaderLong("count");
207 assertEquals(1, actual);
209 actual = response.getRespHeaderLong("max");
210 assertEquals(Long.MAX_VALUE, actual);
212 actual = response.getRespHeaderLong("max++");
213 assertTrue(actual < 0);
215 thrown.expect(RuntimeException.class);
216 actual = response.getRespHeaderLong("key");
217 assertEquals(1, actual);
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);
239 actual = response.getRespHeaderStr("key");
240 assertEquals("value", actual);
250 public void testGetResponseContent() {
251 final RestfulResponse response = new RestfulResponse();
252 assertEquals(null, response.getResponseContent());
254 final String content = "{ \"content\" = \"The response content\" }";
255 response.setResponseJson(content);
256 assertEquals(content, response.getResponseContent());
265 public void testSetResponseJson() {
266 final RestfulResponse response = new RestfulResponse();
267 assertEquals(null, response.getResponseContent());
269 final String content = "{ \"content\" = \"The response content\" }";
270 response.setResponseJson(content);
271 assertEquals(content, response.getResponseContent());