2 * Copyright (c) 2016, 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.openo.baseservice.roa.util.restclient;
19 import static org.junit.Assert.assertEquals;
21 import org.openo.baseservice.util.impl.SystemEnvVariablesDefImpl;
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;
30 import net.sf.json.JSONObject;
33 import java.io.IOException;
45 * @version SDNO 0.5 20-Jun-2016
47 public class TestRestfulConfigure {
52 * @throws java.lang.Exception
56 public static void setUpBeforeClass() throws Exception {
63 * @throws java.lang.Exception
67 public static void tearDownAfterClass() throws Exception {
73 * @throws java.lang.Exception
77 public void setUp() throws Exception {
83 * @throws java.lang.Exception
87 public void tearDown() throws Exception {
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", "");
95 final String path = new File(urlPath).getCanonicalPath();
96 System.out.println("path: " + path);
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
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"));
135 public final void testRestfulConfigureAppRootNull() throws Exception {
136 new MockUp<SystemEnvVariablesDefImpl>() {
139 public String getAppRoot() {
144 final RestfulConfigure configure = new RestfulConfigure();
145 final RestfulOptions options = configure.getOptions();
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"));
164 public final void testRestfulConfigureDefault() throws Exception {
171 public boolean isFile() {
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"));
195 public final void testRestfulConfigureException() throws Exception {
197 new MockUp<JSONObject>() {
200 JSONObject fromObject(final Object object) throws IOException {
201 throw new IOException();
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"));
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"));
243 public final void testGetOptions() {