2 * Copyright 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.Test;
28 import org.junit.runner.RunWith;
30 import net.sf.json.JSONObject;
33 import java.io.IOException;
38 import mockit.integration.junit4.JMockit;
46 * @version 20-Jun-2016
48 @RunWith(JMockit.class)
49 public class TestRestfulConfigure {
54 * @throws java.lang.Exception
58 public static void setUpBeforeClass() throws Exception {
65 * @throws java.lang.Exception
69 public static void tearDownAfterClass() throws Exception {
75 * @throws java.lang.Exception
79 public void setUp() throws Exception {
85 * @throws java.lang.Exception
89 public void tearDown() throws Exception {
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", "");
97 final String path = new File(urlPath).getCanonicalPath();
98 System.out.println("path: " + path);
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
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"));
133 public final void testRestfulConfigureAppRootNull() throws Exception {
134 new MockUp<SystemEnvVariablesDefImpl>() {
137 public String getAppRoot() {
142 final RestfulConfigure configure = new RestfulConfigure();
143 final RestfulOptions options = configure.getOptions();
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"));
161 public final void testRestfulConfigureDefault() throws Exception {
168 public boolean isFile() {
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"));
191 public final void testRestfulConfigureException() throws Exception {
193 new MockUp<JSONObject>() {
196 JSONObject fromObject(final Object object) throws IOException {
197 throw new IOException();
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"));
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"));
236 public final void testGetOptions() {