adjust the code
[vfc/nfvo/wfengine.git] / rest-client / src / test / java / org / openo / baseservice / roa / util / restclient / TestRestfulOptions.java
1 /*
2  * Copyright (c) 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.openo.baseservice.roa.util.restclient;
18
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;
25
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;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import mockit.integration.junit4.JMockit;
39
40 /**
41  * <br/>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version SDNO 0.5 20-Jun-2016
47  */
48 @RunWith(JMockit.class)
49 public class TestRestfulOptions {
50
51     @Rule
52     final public ExpectedException thrown = ExpectedException.none();
53
54     /**
55      * <br/>
56      * 
57      * @throws java.lang.Exception
58      * @since SDNO 0.5
59      */
60     @BeforeClass
61     public static void setUpBeforeClass() throws Exception {
62     }
63
64     /**
65      * <br/>
66      * 
67      * @throws java.lang.Exception
68      * @since SDNO 0.5
69      */
70     @AfterClass
71     public static void tearDownAfterClass() throws Exception {
72     }
73
74     /**
75      * <br/>
76      * 
77      * @throws java.lang.Exception
78      * @since SDNO 0.5
79      */
80     @Before
81     public void setUp() throws Exception {
82     }
83
84     /**
85      * <br/>
86      * 
87      * @throws java.lang.Exception
88      * @since SDNO 0.5
89      */
90     @After
91     public void tearDown() throws Exception {
92     }
93
94     /**
95      * <br/>
96      * 
97      * @since SDNO 0.5
98      */
99     @Test
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());
105     }
106
107     /**
108      * <br/>
109      * 
110      * @since SDNO 0.5
111      */
112     @Test
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());
119     }
120
121     /**
122      * <br/>
123      * 
124      * @since SDNO 0.5
125      */
126     @Test
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());
133     }
134
135     /**
136      * <br/>
137      * 
138      * @since SDNO 0.5
139      */
140     @Test
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());
146     }
147
148     /**
149      * <br/>
150      * 
151      * @since SDNO 0.5
152      */
153     @Test
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());
160     }
161
162     /**
163      * <br/>
164      * 
165      * @since SDNO 0.5
166      */
167     @Test
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());
173     }
174
175     /**
176      * <br/>
177      * 
178      * @since SDNO 0.5
179      */
180     @Test
181     public void testSetRestTimeout() {
182         final RestfulOptions options = new RestfulOptions();
183         int timeout = 0;
184         assertFalse(options.setRestTimeout(timeout));
185         assertEquals(0, options.getRestTimeout());
186
187         timeout = 1;
188         assertTrue(options.setRestTimeout(timeout));
189         assertEquals(timeout, options.getRestTimeout());
190
191         timeout = 10;
192         assertTrue(options.setRestTimeout(timeout));
193         assertEquals(timeout, options.getRestTimeout());
194
195         timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1;
196         assertTrue(options.setRestTimeout(timeout));
197         assertEquals(timeout, options.getRestTimeout());
198
199         timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT;
200         assertTrue(options.setRestTimeout(timeout));
201         assertEquals(timeout, options.getRestTimeout());
202
203         timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1;
204         assertFalse(options.setRestTimeout(timeout));
205     }
206
207     /**
208      * <br/>
209      * 
210      * @since SDNO 0.5
211      */
212     @Test
213     public void testGetRestTimeout() {
214         final RestfulOptions options = new RestfulOptions();
215         int timeout = 0;
216         assertEquals(0, options.getRestTimeout());
217
218         timeout = 1;
219         assertTrue(options.setRestTimeout(timeout));
220         assertEquals(timeout, options.getRestTimeout());
221     }
222
223     /**
224      * <br/>
225      * 
226      * @since SDNO 0.5
227      */
228     @Test
229     public void testGetOption() {
230         final RestfulOptions options = new RestfulOptions();
231         assertNull(options.getOption("invalid"));
232
233         options.setHost("localhost");
234         Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
235         assertNotNull(obj);
236         assertTrue(obj instanceof String);
237         assertEquals("localhost", obj);
238
239         final List<String> list = new ArrayList<String>();
240         list.add("data");
241         options.setOption("list", list);
242         obj = options.getOption("list");
243         assertNotNull(obj);
244         assertTrue(obj instanceof List);
245         assertSame(list, obj);
246     }
247
248     /**
249      * <br/>
250      * 
251      * @since SDNO 0.5
252      */
253     @Test
254     public void testGetIntOption() {
255         final RestfulOptions options = new RestfulOptions();
256
257         assertEquals(0, options.getIntOption("count"));
258
259         options.setOption("count", 1);
260         assertEquals(1, options.getIntOption("count"));
261
262         thrown.expect(RuntimeException.class);
263
264         options.setOption("string-count", "two");
265         final int value = options.getIntOption("string-count");
266         assertEquals(2, value);
267
268     }
269
270     /**
271      * <br/>
272      * 
273      * @since SDNO 0.5
274      */
275     @Test
276     public void testGetStringOption() {
277         final RestfulOptions options = new RestfulOptions();
278
279         assertEquals("", options.getStringOption("count"));
280
281         options.setOption("string-count", "one");
282         assertEquals("one", options.getStringOption("string-count"));
283
284         thrown.expect(RuntimeException.class);
285
286         options.setOption("count", 2);
287         final String value = options.getStringOption("count");
288         assertEquals(2, value);
289     }
290
291     /**
292      * <br/>
293      * 
294      * @since SDNO 0.5
295      */
296     @Test
297     public void testSetOption() {
298         final RestfulOptions options = new RestfulOptions();
299         assertNull(options.getOption("invalid"));
300
301         options.setHost("localhost");
302         Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
303         assertNotNull(obj);
304         assertTrue(obj instanceof String);
305         assertEquals("localhost", obj);
306
307         final List<String> list = new ArrayList<String>();
308         list.add("data");
309         options.setOption("list", list);
310         obj = options.getOption("list");
311         assertNotNull(obj);
312         assertTrue(obj instanceof List);
313         assertSame(list, obj);
314     }
315 }