7cc7b1e9ed59d118569fd52cafedf2d13907238a
[vfc/nfvo/wfengine.git] / rest-client / src / test / java / org / openo / baseservice / roa / util / restclient / TestRestfulConfigure.java
1 /*
2  * Copyright 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
21 import org.openo.baseservice.util.impl.SystemEnvVariablesDefImpl;
22
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29
30 import net.sf.json.JSONObject;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.net.URL;
35
36 import mockit.Mock;
37 import mockit.MockUp;
38 import mockit.integration.junit4.JMockit;
39
40 /**
41  * <br/>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version   20-Jun-2016
47  */
48 @RunWith(JMockit.class)
49 public class TestRestfulConfigure {
50
51     /**
52      * <br/>
53      * 
54      * @throws java.lang.Exception
55      * @since  
56      */
57     @BeforeClass
58     public static void setUpBeforeClass() throws Exception {
59         setAppRoot();
60     }
61
62     /**
63      * <br/>
64      * 
65      * @throws java.lang.Exception
66      * @since  
67      */
68     @AfterClass
69     public static void tearDownAfterClass() throws Exception {
70     }
71
72     /**
73      * <br/>
74      * 
75      * @throws java.lang.Exception
76      * @since  
77      */
78     @Before
79     public void setUp() throws Exception {
80     }
81
82     /**
83      * <br/>
84      * 
85      * @throws java.lang.Exception
86      * @since  
87      */
88     @After
89     public void tearDown() throws Exception {
90     }
91
92     private static void setAppRoot() {
93         final URL resource = ClassLoader.getSystemResource("etc/conf/restclient.json");
94         final String urlPath = resource.getPath().replace("etc/conf/restclient.json", "");
95
96         try {
97             final String path = new File(urlPath).getCanonicalPath();
98             System.out.println("path: " + path);
99
100             System.setProperty("catalina.base", path);
101             System.out.println("approot:" + System.getProperty("catalina.base"));
102         } catch(final IOException e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         }
106
107     }
108
109     /**
110      * <br/>
111      * @throws Exception
112      * @since  
113      */
114     @Test
115     public final void testRestfulConfigure() throws Exception {
116         final RestfulConfigure configure = new RestfulConfigure();
117         final RestfulOptions options = configure.getOptions();
118         assertEquals("127.0.0.1", options.getHost());
119         assertEquals(8080, options.getPort());
120         assertEquals(1000, options.getIntOption("ConnectTimeout"));
121         assertEquals(10, options.getIntOption("thread"));
122         assertEquals(500000, options.getIntOption("idletimeout"));
123         assertEquals(10000, options.getIntOption("timeout"));
124
125     }
126
127     /**
128      * <br/>
129      * @throws Exception
130      * @since  
131      */
132     @Test
133     public final void testRestfulConfigureAppRootNull() throws Exception {
134         new MockUp<SystemEnvVariablesDefImpl>() {
135
136             @Mock
137             public String getAppRoot() {
138                 return null;
139             }
140
141         };
142         final RestfulConfigure configure = new RestfulConfigure();
143         final RestfulOptions options = configure.getOptions();
144
145         assertEquals("", options.getHost());
146         assertEquals(0, options.getPort());
147         assertEquals(3000, options.getIntOption("ConnectTimeout"));
148         assertEquals(200, options.getIntOption("thread"));
149         assertEquals(30000, options.getIntOption("idletimeout"));
150         assertEquals(30000, options.getIntOption("timeout"));
151
152     }
153
154     /**
155      * <br/>
156      * 
157      * @throws Exception
158      * @since  
159      */
160     @Test
161     public final void testRestfulConfigureDefault() throws Exception {
162
163         {
164
165             new MockUp<File>() {
166
167                 @Mock
168                 public boolean isFile() {
169                     return false;
170                 }
171             };
172             final RestfulConfigure configure = new RestfulConfigure();
173             final RestfulOptions options = configure.getOptions();
174             assertEquals("", options.getHost());
175             assertEquals(0, options.getPort());
176             assertEquals(3000, options.getIntOption("ConnectTimeout"));
177             assertEquals(200, options.getIntOption("thread"));
178             assertEquals(30000, options.getIntOption("idletimeout"));
179             assertEquals(30000, options.getIntOption("timeout"));
180         }
181
182     }
183
184     /**
185      * <br/>
186      * 
187      * @throws Exception
188      * @since  
189      */
190     @Test
191     public final void testRestfulConfigureException() throws Exception {
192
193         new MockUp<JSONObject>() {
194
195             @Mock
196             JSONObject fromObject(final Object object) throws IOException {
197                 throw new IOException();
198             }
199         };
200
201         final RestfulConfigure configure = new RestfulConfigure();
202         final RestfulOptions options = configure.getOptions();
203         assertEquals("", options.getHost());
204         assertEquals(0, options.getPort());
205         assertEquals(3000, options.getIntOption("ConnectTimeout"));
206         assertEquals(200, options.getIntOption("thread"));
207         assertEquals(30000, options.getIntOption("idletimeout"));
208         assertEquals(30000, options.getIntOption("timeout"));
209
210     }
211
212     /**
213      * 
214      * @since  
215      */
216     @Test
217     public final void testRestfulConfigureString() {
218         final String configFile = "rest-client-test.json";
219         final String appRoot = System.getProperty("catalina.base");
220         final RestfulConfigure configure = new RestfulConfigure(appRoot + File.separator + configFile);
221         final RestfulOptions options = configure.getOptions();
222         assertEquals("10.10.10.10", options.getHost());
223         assertEquals(443, options.getPort());
224         assertEquals(10, options.getIntOption("ConnectTimeout"));
225         assertEquals(100, options.getIntOption("thread"));
226         assertEquals(30, options.getIntOption("idletimeout"));
227         assertEquals(60, options.getIntOption("timeout"));
228     }
229
230     /**
231      * <br/>
232      * 
233      * @since  
234      */
235     @Test
236     public final void testGetOptions() {
237     }
238 }