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.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertSame;
24 import static org.junit.Assert.assertTrue;
26 import org.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
35 import java.util.ArrayList;
36 import java.util.List;
38 import mockit.integration.junit4.JMockit;
46 * @version SDNO 0.5 20-Jun-2016
48 @RunWith(JMockit.class)
49 public class TestRestfulOptions {
52 final public ExpectedException thrown = ExpectedException.none();
57 * @throws java.lang.Exception
61 public static void setUpBeforeClass() throws Exception {
67 * @throws java.lang.Exception
71 public static void tearDownAfterClass() throws Exception {
77 * @throws java.lang.Exception
81 public void setUp() throws Exception {
87 * @throws java.lang.Exception
91 public void tearDown() throws Exception {
100 public void testSetCalledServiceName() {
101 final RestfulOptions options = new RestfulOptions();
102 final String serviceName = "sample-service";
103 assertTrue(options.setCalledServiceName(serviceName));
104 assertEquals(serviceName, options.getCalledServicName());
113 public void testGetCalledServicName() {
114 final RestfulOptions options = new RestfulOptions();
115 final String serviceName = "sample-service";
116 assertEquals("", options.getCalledServicName());
117 options.setCalledServiceName(serviceName);
118 assertEquals(serviceName, options.getCalledServicName());
127 public void testGetPort() {
128 final RestfulOptions options = new RestfulOptions();
129 final int port = 9091;
130 assertEquals(0, options.getPort());
131 options.setPort(port);
132 assertEquals(port, options.getPort());
141 public void testSetPort() {
142 final RestfulOptions options = new RestfulOptions();
143 final int port = 9091;
144 assertTrue(options.setPort(port));
145 assertEquals(port, options.getPort());
154 public void testGetHost() {
155 final RestfulOptions options = new RestfulOptions();
156 final String host = "localhost";
157 assertEquals("", options.getHost());
158 options.setHost(host);
159 assertEquals(host, options.getHost());
168 public void testSetHost() {
169 final RestfulOptions options = new RestfulOptions();
170 final String host = "localhost";
171 assertTrue(options.setHost(host));
172 assertEquals(host, options.getHost());
181 public void testSetRestTimeout() {
182 final RestfulOptions options = new RestfulOptions();
184 assertFalse(options.setRestTimeout(timeout));
185 assertEquals(0, options.getRestTimeout());
188 assertTrue(options.setRestTimeout(timeout));
189 assertEquals(timeout, options.getRestTimeout());
192 assertTrue(options.setRestTimeout(timeout));
193 assertEquals(timeout, options.getRestTimeout());
195 timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1;
196 assertTrue(options.setRestTimeout(timeout));
197 assertEquals(timeout, options.getRestTimeout());
199 timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT;
200 assertTrue(options.setRestTimeout(timeout));
201 assertEquals(timeout, options.getRestTimeout());
203 timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1;
204 assertFalse(options.setRestTimeout(timeout));
213 public void testGetRestTimeout() {
214 final RestfulOptions options = new RestfulOptions();
216 assertEquals(0, options.getRestTimeout());
219 assertTrue(options.setRestTimeout(timeout));
220 assertEquals(timeout, options.getRestTimeout());
229 public void testGetOption() {
230 final RestfulOptions options = new RestfulOptions();
231 assertNull(options.getOption("invalid"));
233 options.setHost("localhost");
234 Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
236 assertTrue(obj instanceof String);
237 assertEquals("localhost", obj);
239 final List<String> list = new ArrayList<String>();
241 options.setOption("list", list);
242 obj = options.getOption("list");
244 assertTrue(obj instanceof List);
245 assertSame(list, obj);
254 public void testGetIntOption() {
255 final RestfulOptions options = new RestfulOptions();
257 assertEquals(0, options.getIntOption("count"));
259 options.setOption("count", 1);
260 assertEquals(1, options.getIntOption("count"));
262 thrown.expect(RuntimeException.class);
264 options.setOption("string-count", "two");
265 final int value = options.getIntOption("string-count");
266 assertEquals(2, value);
276 public void testGetStringOption() {
277 final RestfulOptions options = new RestfulOptions();
279 assertEquals("", options.getStringOption("count"));
281 options.setOption("string-count", "one");
282 assertEquals("one", options.getStringOption("string-count"));
284 thrown.expect(RuntimeException.class);
286 options.setOption("count", 2);
287 final String value = options.getStringOption("count");
288 assertEquals(2, value);
297 public void testSetOption() {
298 final RestfulOptions options = new RestfulOptions();
299 assertNull(options.getOption("invalid"));
301 options.setHost("localhost");
302 Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
304 assertTrue(obj instanceof String);
305 assertEquals("localhost", obj);
307 final List<String> list = new ArrayList<String>();
309 options.setOption("list", list);
310 obj = options.getOption("list");
312 assertTrue(obj instanceof List);
313 assertSame(list, obj);