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