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 / TestRestfulOptions.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.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 java.util.ArrayList;
27 import java.util.List;
28
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;
37
38 import mockit.integration.junit4.JMockit;
39
40 /**
41  * <br/>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version
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
59      */
60     @BeforeClass
61     public static void setUpBeforeClass() throws Exception {
62     }
63
64     /**
65      * <br/>
66      * 
67      * @throws java.lang.Exception
68      * @since
69      */
70     @AfterClass
71     public static void tearDownAfterClass() throws Exception {
72     }
73
74     /**
75      * <br/>
76      * 
77      * @throws java.lang.Exception
78      * @since
79      */
80     @Before
81     public void setUp() throws Exception {
82     }
83
84     /**
85      * <br/>
86      * 
87      * @throws java.lang.Exception
88      * @since
89      */
90     @After
91     public void tearDown() throws Exception {
92     }
93
94     /**
95      * <br/>
96      * 
97      * @since
98      */
99     @Test
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());
106     }
107
108     /**
109      * <br/>
110      * 
111      * @since
112      */
113     @Test
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());
119     }
120
121     /**
122      * <br/>
123      * 
124      * @since
125      */
126     @Test
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());
133     }
134
135     /**
136      * <br/>
137      * 
138      * @since
139      */
140     @Test
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());
146     }
147
148     /**
149      * <br/>
150      * 
151      * @since
152      */
153     @Test
154     public void testSetRestTimeout() {
155         final RestfulOptions options = new RestfulOptions();
156         int timeout = 0;
157         assertFalse(options.setRestTimeout(timeout));
158         assertEquals(0, options.getRestTimeout());
159
160         timeout = 1;
161         assertTrue(options.setRestTimeout(timeout));
162         assertEquals(timeout, options.getRestTimeout());
163
164         timeout = 10;
165         assertTrue(options.setRestTimeout(timeout));
166         assertEquals(timeout, options.getRestTimeout());
167
168         timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1;
169         assertTrue(options.setRestTimeout(timeout));
170         assertEquals(timeout, options.getRestTimeout());
171
172         timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT;
173         assertTrue(options.setRestTimeout(timeout));
174         assertEquals(timeout, options.getRestTimeout());
175
176         timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1;
177         assertFalse(options.setRestTimeout(timeout));
178     }
179
180     /**
181      * <br/>
182      * 
183      * @since
184      */
185     @Test
186     public void testGetRestTimeout() {
187         final RestfulOptions options = new RestfulOptions();
188         int timeout = 0;
189         assertEquals(0, options.getRestTimeout());
190
191         timeout = 1;
192         assertTrue(options.setRestTimeout(timeout));
193         assertEquals(timeout, options.getRestTimeout());
194     }
195
196     /**
197      * <br/>
198      * 
199      * @since
200      */
201     @Test
202     public void testGetOption() {
203         final RestfulOptions options = new RestfulOptions();
204         assertNull(options.getOption("invalid"));
205
206         options.setHost("localhost");
207         Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
208         assertNotNull(obj);
209         assertTrue(obj instanceof String);
210         assertEquals("localhost", obj);
211
212         final List<String> list = new ArrayList<String>();
213         list.add("data");
214         options.setOption("list", list);
215         obj = options.getOption("list");
216         assertNotNull(obj);
217         assertTrue(obj instanceof List);
218         assertSame(list, obj);
219     }
220
221     /**
222      * <br/>
223      * 
224      * @since
225      */
226     @Test
227     public void testGetIntOption() {
228         final RestfulOptions options = new RestfulOptions();
229
230         assertEquals(0, options.getIntOption("count"));
231
232         options.setOption("count", 1);
233         assertEquals(1, options.getIntOption("count"));
234
235         thrown.expect(RuntimeException.class);
236
237         options.setOption("string-count", "two");
238         final int value = options.getIntOption("string-count");
239         assertEquals(2, value);
240
241     }
242
243     /**
244      * <br/>
245      * 
246      * @since
247      */
248     @Test
249     public void testGetStringOption() {
250         final RestfulOptions options = new RestfulOptions();
251
252         assertEquals("", options.getStringOption("count"));
253
254         options.setOption("string-count", "one");
255         assertEquals("one", options.getStringOption("string-count"));
256
257         thrown.expect(RuntimeException.class);
258
259         options.setOption("count", 2);
260         final String value = options.getStringOption("count");
261         assertEquals(2, value);
262     }
263
264     /**
265      * <br/>
266      * 
267      * @since
268      */
269     @Test
270     public void testSetOption() {
271         final RestfulOptions options = new RestfulOptions();
272         assertNull(options.getOption("invalid"));
273
274         options.setHost("localhost");
275         Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
276         assertNotNull(obj);
277         assertTrue(obj instanceof String);
278         assertEquals("localhost", obj);
279
280         final List<String> list = new ArrayList<String>();
281         list.add("data");
282         options.setOption("list", list);
283         obj = options.getOption("list");
284         assertNotNull(obj);
285         assertTrue(obj instanceof List);
286         assertSame(list, obj);
287     }
288 }