2 * Copyright 2017 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.vnfm.svnfm.vnfmadapter.common.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 java.util.ArrayList;
27 import java.util.List;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
38 import mockit.integration.junit4.JMockit;
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 testGetPort() {
101 final RestfulOptions options = new RestfulOptions();
102 final int port = 9091;
103 assertEquals(0, options.getPort());
104 options.setPort(port);
105 assertEquals(port, options.getPort());
114 public void testSetPort() {
115 final RestfulOptions options = new RestfulOptions();
116 final int port = 9091;
117 assertTrue(options.setPort(port));
118 assertEquals(port, options.getPort());
127 public void testGetHost() {
128 final RestfulOptions options = new RestfulOptions();
129 final String host = "localhost";
130 assertEquals("", options.getHost());
131 options.setHost(host);
132 assertEquals(host, options.getHost());
141 public void testSetHost() {
142 final RestfulOptions options = new RestfulOptions();
143 final String host = "localhost";
144 assertTrue(options.setHost(host));
145 assertEquals(host, options.getHost());
154 public void testSetRestTimeout() {
155 final RestfulOptions options = new RestfulOptions();
157 assertFalse(options.setRestTimeout(timeout));
158 assertEquals(0, options.getRestTimeout());
161 assertTrue(options.setRestTimeout(timeout));
162 assertEquals(timeout, options.getRestTimeout());
165 assertTrue(options.setRestTimeout(timeout));
166 assertEquals(timeout, options.getRestTimeout());
168 timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1;
169 assertTrue(options.setRestTimeout(timeout));
170 assertEquals(timeout, options.getRestTimeout());
172 timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT;
173 assertTrue(options.setRestTimeout(timeout));
174 assertEquals(timeout, options.getRestTimeout());
176 timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1;
177 assertFalse(options.setRestTimeout(timeout));
186 public void testGetRestTimeout() {
187 final RestfulOptions options = new RestfulOptions();
189 assertEquals(0, options.getRestTimeout());
192 assertTrue(options.setRestTimeout(timeout));
193 assertEquals(timeout, options.getRestTimeout());
202 public void testGetOption() {
203 final RestfulOptions options = new RestfulOptions();
204 assertNull(options.getOption("invalid"));
206 options.setHost("localhost");
207 Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
209 assertTrue(obj instanceof String);
210 assertEquals("localhost", obj);
212 final List<String> list = new ArrayList<String>();
214 options.setOption("list", list);
215 obj = options.getOption("list");
217 assertTrue(obj instanceof List);
218 assertSame(list, obj);
227 public void testGetIntOption() {
228 final RestfulOptions options = new RestfulOptions();
230 assertEquals(0, options.getIntOption("count"));
232 options.setOption("count", 1);
233 assertEquals(1, options.getIntOption("count"));
235 thrown.expect(RuntimeException.class);
237 options.setOption("string-count", "two");
238 final int value = options.getIntOption("string-count");
239 assertEquals(2, value);
249 public void testGetStringOption() {
250 final RestfulOptions options = new RestfulOptions();
252 assertEquals("", options.getStringOption("count"));
254 options.setOption("string-count", "one");
255 assertEquals("one", options.getStringOption("string-count"));
257 thrown.expect(RuntimeException.class);
259 options.setOption("count", 2);
260 final String value = options.getStringOption("count");
261 assertEquals(2, value);
270 public void testSetOption() {
271 final RestfulOptions options = new RestfulOptions();
272 assertNull(options.getOption("invalid"));
274 options.setHost("localhost");
275 Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
277 assertTrue(obj instanceof String);
278 assertEquals("localhost", obj);
280 final List<String> list = new ArrayList<String>();
282 options.setOption("list", list);
283 obj = options.getOption("list");
285 assertTrue(obj instanceof List);
286 assertSame(list, obj);