summaryrefslogtreecommitdiffstats
path: root/rest-client/src/test/java/org
diff options
context:
space:
mode:
authortian.ming@huawei.com <tian.ming@huawei.com>2016-08-25 21:13:33 +0800
committertian.ming@huawei.com <tian.ming@huawei.com>2016-08-25 21:13:33 +0800
commit79d67955b160929ffacc6828014d3c5ce3110fb8 (patch)
tree011fb1952e2cfd8292d6ce247abdd745bfe1d700 /rest-client/src/test/java/org
parent8b49886b96d5f7ebc91dbf1aa158b31ac4a72848 (diff)
adjust the code
Change-Id: Id896bed917ba6730c6b5d96665ef9978cfb413de Signed-off-by: tian.ming@huawei.com <tian.ming@huawei.com>
Diffstat (limited to 'rest-client/src/test/java/org')
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/ReaderHelperTest.java116
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/TestServiceUtil.java92
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestHttpUtil.java266
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java184
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestRestClientUtil.java210
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/demo/JsonTestClass.java88
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestHttpRest.java1289
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestHttpContentExchange.java590
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulConfigure.java245
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulFactory.java106
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulOptions.java315
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulParametes.java229
-rw-r--r--rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulResponse.java273
13 files changed, 4003 insertions, 0 deletions
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/ReaderHelperTest.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/ReaderHelperTest.java
new file mode 100644
index 0000000..78f08da
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/ReaderHelperTest.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openo.baseservice.roa.util;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import junit.framework.Assert;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 12-Jun-2016
+ */
+public class ReaderHelperTest {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * Test method for {@link org.openo.baseservice.util.inf.ReaderHelper#getLine()}.
+ */
+ @Test
+ public void testGetLine() {
+ final String message = "hello.. how are you?";
+ final Reader reader = new StringReader(message);
+ final ReaderHelper helper = new ReaderHelper(reader);
+ final String actual = helper.getLine();
+ Assert.assertEquals(message, actual);
+ }
+
+ /**
+ * Test method for {@link org.openo.baseservice.util.inf.ReaderHelper#getLine()}.
+ */
+ @Test
+ public void testGetLineMultiLine() {
+ final String line1 = "hello.. how are you?";
+ final String line2 = "I am fine.";
+ final Reader reader = new StringReader(line1 + System.lineSeparator() + line2);
+ final ReaderHelper helper = new ReaderHelper(reader);
+ String actual = helper.getLine();
+ Assert.assertEquals(line1, actual);
+ actual = helper.getLine();
+ Assert.assertEquals(line2, actual);
+ actual = helper.getLine();
+ Assert.assertEquals(null, actual);
+ }
+
+ @Test
+ public void testGetLineNull() {
+ final ReaderHelper helper = new ReaderHelper(null);
+ final String actual = helper.getLine();
+ Assert.assertEquals(null, actual);
+
+ }
+
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/TestServiceUtil.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/TestServiceUtil.java
new file mode 100644
index 0000000..f4f93e4
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/TestServiceUtil.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 13-Jun-2016
+ */
+public class TestServiceUtil {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetServiceHost() {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetServicePort() {
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestHttpUtil.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestHttpUtil.java
new file mode 100644
index 0000000..419d4d0
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestHttpUtil.java
@@ -0,0 +1,266 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.clientsdk;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Arrays;
+import java.util.Date;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import mockit.Mocked;
+import mockit.NonStrictExpectations;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 13-Jun-2016
+ */
+public class TestHttpUtil {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testContainsIgnoreCase() {
+ final String[] array = {"hello", "how", "are", "you", "?"};
+ final String toFind = "Hello";
+ Assert.assertTrue(HttpUtil.containsIgnoreCase(array, toFind));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testContainsIgnoreCaseNull() {
+ final String[] array = {"hello", "how", "are", "you", "?"};
+ final String toFind = "Hello";
+ Assert.assertFalse(HttpUtil.containsIgnoreCase(array, null));
+
+ array[0] = null;
+ Assert.assertFalse(HttpUtil.containsIgnoreCase(array, toFind));
+
+ Assert.assertTrue(HttpUtil.containsIgnoreCase(array, null));
+ array[0] = "hello";
+ array[array.length - 1] = null;
+ Assert.assertTrue(HttpUtil.containsIgnoreCase(array, null));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testJoin() {
+ final String[] array = {"hello", "how", "are", "you", "?"};
+ String actual = HttpUtil.join(array, ",");
+ String expected = "hello,how,are,you,?";
+ Assert.assertEquals(actual, expected);
+
+ actual = HttpUtil.join(array, "#");
+ expected = expected.replaceAll(",", "#");
+ Assert.assertEquals(actual, expected);
+ actual = HttpUtil.join(new String[] {}, ",");
+ Assert.assertEquals(actual, "");
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testParameterToString() {
+ // with param string.
+ Object param = new String("String Param");
+ String actual = HttpUtil.parameterToString(param);
+ String expected = "String Param";
+ Assert.assertEquals(expected, actual);
+
+ // with param date.
+ final Date date = new Date();
+ param = date;
+ expected = "" + date.getTime();
+ actual = HttpUtil.parameterToString(param);
+ Assert.assertEquals(expected, actual);
+
+ // with param collection.
+ final String[] array = {"hello", "how", "are", "you", "?"};
+ param = Arrays.asList(array);
+ expected = HttpUtil.join(array, ",");
+ actual = HttpUtil.parameterToString(param);
+ Assert.assertEquals(expected, actual);
+
+ // with param any
+ param = new Object() {
+
+ @Override
+ public String toString() {
+ return "test object";
+ }
+ };
+ expected = "test object";
+ actual = HttpUtil.parameterToString(param);
+ Assert.assertEquals(expected, actual);
+
+ // with param null.
+ expected = "";
+ actual = HttpUtil.parameterToString(null);
+ Assert.assertEquals(expected, actual);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSelectHeaderAccept() {
+ final String[] accepts = {"application/json", "text/plain", "application/xml"};
+ String expected = "application/json";
+ String actual = HttpUtil.selectHeaderAccept(accepts);
+ Assert.assertEquals(expected, actual);
+
+ accepts[0] = "application/x-www-form-urlencoded";
+ expected = HttpUtil.join(accepts, ",");
+ actual = HttpUtil.selectHeaderAccept(accepts);
+ Assert.assertEquals(expected, actual);
+
+ expected = null;
+ actual = HttpUtil.selectHeaderAccept(new String[] {});
+ Assert.assertEquals(expected, actual);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSelectHeaderContentType() {
+ final String[] accepts = {"application/json", "text/plain", "application/xml"};
+ String expected = "application/json";
+ String actual = HttpUtil.selectHeaderContentType(accepts);
+ Assert.assertEquals(expected, actual);
+
+ accepts[0] = "application/x-www-form-urlencoded";
+ expected = "application/x-www-form-urlencoded";
+ actual = HttpUtil.selectHeaderContentType(accepts);
+ Assert.assertEquals(expected, actual);
+
+ expected = "application/json";
+ actual = HttpUtil.selectHeaderContentType(new String[] {});
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testEscapeString() throws Exception {
+ final String str = "/this/url/to be encoded";
+ final String actual = HttpUtil.escapeString(str);
+ final String expected = "%2Fthis%2Furl%2Fto%20be%20encoded";
+ Assert.assertEquals(expected, actual);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testEscapeStringException() throws Exception {
+
+ final String str = "/this/url/to be encoded";
+ new NonStrictExpectations() {
+
+ @Mocked
+ URLEncoder encoder;
+
+ {
+ URLEncoder.encode(str, "utf8");
+ result = new UnsupportedEncodingException();
+ }
+ };
+
+ final String actual = HttpUtil.escapeString(str);
+ Assert.assertEquals(str, actual);
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java
new file mode 100644
index 0000000..d532b94
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.clientsdk;
+
+import org.openo.baseservice.roa.util.clientsdk.demo.JsonTestClass;
+
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.type.TypeReference;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import net.sf.json.JSONObject;
+
+import junit.framework.Assert;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 13-Jun-2016
+ */
+public class TestJsonUtil {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testUnMarshalStringClassOfT() throws Exception {
+ final String name = "myname";
+ final int id = 25;
+ final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}";
+
+ final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, JsonTestClass.class);
+
+ Assert.assertNotNull(jsonObj);
+ Assert.assertEquals(name, jsonObj.getName());
+ Assert.assertEquals(id, jsonObj.getId());
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testUnMarshalStringTypeReferenceOfT() throws Exception {
+ final String name = "myname";
+ final int id = 25;
+ final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}";
+
+ final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, new TypeReference<JsonTestClass>() {});
+
+ Assert.assertNotNull(jsonObj);
+ Assert.assertEquals(name, jsonObj.getName());
+ Assert.assertEquals(id, jsonObj.getId());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testMarshal() throws Exception {
+ final JsonTestClass jsonObj = new JsonTestClass();
+ jsonObj.setId(1);
+ jsonObj.setName("somename");
+ final String str = JsonUtil.marshal(jsonObj);
+ final JSONObject json = JSONObject.fromObject(str);
+ Assert.assertNotNull(json);
+ Assert.assertEquals(json.getString("name"), jsonObj.getName());
+ Assert.assertEquals(json.getInt("id"), jsonObj.getId());
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testMarshalJsonObj() throws Exception {
+ final JSONObject jsonObj = new JSONObject();
+ jsonObj.put("id", 10);
+ jsonObj.put("name", "some-name");
+ final String str = JsonUtil.marshal(jsonObj);
+ final JSONObject json = JSONObject.fromObject(str);
+ Assert.assertNotNull(json);
+ Assert.assertEquals(json.getString("name"), "some-name");
+ Assert.assertEquals(json.getInt("id"), 10);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws JsonParseException
+ * @throws JsonMappingException
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetMapper() throws JsonParseException, JsonMappingException, Exception {
+ final String name = "myname";
+ final int id = 25;
+ final ObjectMapper mapper = JsonUtil.getMapper();
+ Assert.assertNotNull(mapper);
+ final JSONObject json = new JSONObject();
+ json.put("name", name);
+ json.put("id", id);
+ final JsonTestClass jsonObj = mapper.readValue(json.toString(), JsonTestClass.class);
+ Assert.assertNotNull(jsonObj);
+ Assert.assertEquals(name, jsonObj.getName());
+ Assert.assertEquals(id, jsonObj.getId());
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestRestClientUtil.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestRestClientUtil.java
new file mode 100644
index 0000000..0fcaf40
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestRestClientUtil.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.clientsdk;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.roa.util.restclient.Restful;
+import org.openo.baseservice.roa.util.restclient.RestfulAsyncCallback;
+import org.openo.baseservice.roa.util.restclient.RestfulParametes;
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import java.util.Arrays;
+import java.util.Date;
+
+import junit.framework.Assert;
+import mockit.Expectations;
+import mockit.Mocked;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 13-Jun-2016
+ */
+public class TestRestClientUtil {
+
+ @Mocked
+ Restful restFullMock;
+
+ ExpectedException thrown = ExpectedException.none();;
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testInvokeMethod() throws ServiceException {
+ final String path = "test/path";
+ final RestfulParametes parameters = null;
+ final RestfulResponse expected = new RestfulResponse();
+ expected.setStatus(200);
+
+ new Expectations() {
+
+ {
+ restFullMock.get(path, parameters);
+ returns(expected);
+
+ restFullMock.post(path, parameters);
+ returns(expected);
+
+ restFullMock.patch(path, parameters);
+ returns(expected);
+
+ restFullMock.delete(path, parameters);
+ returns(expected);
+
+ restFullMock.put(path, parameters);
+ returns(expected);
+ }
+ };
+ RestfulResponse actual = RestClientUtil.invokeMethod("GET", path, parameters, restFullMock);
+ Assert.assertEquals(200, actual.getStatus());
+
+ actual = RestClientUtil.invokeMethod("POST", path, parameters, restFullMock);
+ Assert.assertEquals(200, actual.getStatus());
+
+ actual = RestClientUtil.invokeMethod("PATCH", path, parameters, restFullMock);
+ Assert.assertEquals(200, actual.getStatus());
+
+ actual = RestClientUtil.invokeMethod("DELETE", path, parameters, restFullMock);
+ Assert.assertEquals(200, actual.getStatus());
+
+ actual = RestClientUtil.invokeMethod("PUT", path, parameters, restFullMock);
+ Assert.assertEquals(200, actual.getStatus());
+ }
+
+ @Ignore
+ @Test(expected = ServiceException.class)
+ public void testInvokeMethodException() throws ServiceException {
+ RestClientUtil.invokeMethod("UNKNOWN-METHOD", "some/path", null, restFullMock);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test(expected = ServiceException.class)
+ public void testInvokeAsyncMethod() throws ServiceException {
+ final String path = "test/path";
+ final RestfulParametes parameters = null;
+ final RestfulAsyncCallback callback = null;
+
+ RestClientUtil.invokeAsyncMethod("GET", path, parameters, restFullMock, callback);
+
+ RestClientUtil.invokeAsyncMethod("POST", path, parameters, restFullMock, callback);
+
+ RestClientUtil.invokeAsyncMethod("PATCH", path, parameters, restFullMock, callback);
+
+ RestClientUtil.invokeAsyncMethod("DELETE", path, parameters, restFullMock, callback);
+
+ RestClientUtil.invokeAsyncMethod("PUT", path, parameters, restFullMock, callback);
+
+ RestClientUtil.invokeAsyncMethod("UNKNOWN", path, parameters, restFullMock, callback);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testIsPrimitive() {
+
+ Assert.assertTrue(RestClientUtil.isPrimitive(Integer.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Long.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Double.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Void.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(String.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Boolean.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Byte.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Character.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Short.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(Float.class));
+
+ Assert.assertTrue(RestClientUtil.isPrimitive(int.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(long.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(double.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(void.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(boolean.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(byte.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(char.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(short.class));
+ Assert.assertTrue(RestClientUtil.isPrimitive(float.class));
+
+ Assert.assertFalse(RestClientUtil.isPrimitive(Object.class));
+ Assert.assertFalse(RestClientUtil.isPrimitive(Date.class));
+ Assert.assertFalse(RestClientUtil.isPrimitive(Arrays.class));
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/demo/JsonTestClass.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/demo/JsonTestClass.java
new file mode 100644
index 0000000..5833bca
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/demo/JsonTestClass.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.clientsdk.demo;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 13-Jun-2016
+ */
+public class JsonTestClass {
+
+ /**
+ *
+ */
+ private String name;
+
+ /**
+ *
+ */
+ private int id;
+
+ /**
+ * Constructor<br/>
+ * <p>
+ * </p>
+ *
+ * @since SDNO 0.5
+ */
+ public JsonTestClass() {
+ }
+
+ /**
+ * <br/>
+ *
+ * @return
+ * @since SDNO 0.5
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * <br/>
+ *
+ * @param name
+ * @since SDNO 0.5
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * <br/>
+ *
+ * @return
+ * @since SDNO 0.5
+ */
+ public int getId() {
+ return id;
+ }
+
+ /**
+ * <br/>
+ *
+ * @param id
+ * @since SDNO 0.5
+ */
+ public void setId(int id) {
+ this.id = id;
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestHttpRest.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestHttpRest.java
new file mode 100644
index 0000000..ac8b4a8
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestHttpRest.java
@@ -0,0 +1,1289 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.roa.util.ServiceUtil;
+
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.HttpExchange;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Field;
+import java.net.URLEncoder;
+
+import mockit.Expectations;
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+import mockit.NonStrictExpectations;
+import mockit.integration.junit4.JMockit;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 13-Jun-2016
+ */
+@RunWith(JMockit.class)
+public class TestHttpRest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testInitHttpRest() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+ new MockUp<HttpClient>() {
+
+ @Mock
+ public void doStart() {
+ System.out.println("started");
+ }
+ };
+ final HttpRest httpRest = new HttpRest();
+ httpRest.initHttpRest(options);
+ final Field httpClient = HttpBaseRest.class.getDeclaredField("client");
+ httpClient.setAccessible(true);
+ Assert.assertNotNull(httpClient.get(httpRest));
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testInitHttpRestExcpetion() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+ new MockUp<HttpClient>() {
+
+ @Mock
+ public void doStart() throws Exception {
+ throw new Exception();
+ }
+ };
+ final HttpRest httpRest = new HttpRest();
+ thrown.expect(ServiceException.class);
+ thrown.expectMessage("http client init failed.");
+ httpRest.initHttpRest(options);
+ final Field httpClient = HttpRest.class.getDeclaredField("client");
+ httpClient.setAccessible(true);
+ Assert.assertNull(httpClient.get(httpRest));
+ System.out.println("finished");
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testInitHttpRestNull() throws Exception {
+ final HttpRest httpRest = new HttpRest();
+ thrown.expect(ServiceException.class);
+ thrown.expectMessage("option is null.");
+ httpRest.initHttpRest(null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws NoSuchFieldException
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception {
+ final HttpBaseRest httpRest = new HttpRest();
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ }
+
+ };
+ final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback);
+ assertNotNull(exchange);
+ final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback");
+ assertNotNull(callbackField);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetStringRestfulParametes() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parametes = new RestfulParametes();
+ parametes.put("id", "1234");
+ parametes.put("name", "some-name");
+ parametes.put("address", null);
+ parametes.putHttpContextHeader("Content-Type", "application/json");
+ parametes.putHttpContextHeader("Accept-Encoding", "*/*");
+ final RestfulResponse response = httpRest.get("path/to/service", parametes);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetStringRestfulParametesRestfulOptions() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testGetStringRestfulParametesEncodeError() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ new NonStrictExpectations() {
+
+ @Mocked
+ URLEncoder encoder;
+
+ {
+ URLEncoder.encode(anyString, RestfulClientConst.ENCODING);
+ result = new UnsupportedEncodingException();
+ }
+
+ };
+
+ thrown.expect(ServiceException.class);
+ thrown.expectMessage("Broken VM does not support");
+
+ final RestfulParametes parametes = new RestfulParametes();
+ parametes.put("id", "1234");
+ parametes.put("name", "some-name");
+ parametes.put("address", null);
+ parametes.putHttpContextHeader("Content-Type", "application/json");
+ parametes.putHttpContextHeader("Accept-Encoding", "*/*");
+ final RestfulResponse response = httpRest.get("path/to/service", parametes);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testHeadStringRestfulParametes() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parametes = new RestfulParametes();
+ parametes.put("id", "1234");
+ parametes.put("name", "some-name");
+ parametes.put("address", null);
+ parametes.putHttpContextHeader("Content-Type", "");
+ parametes.putHttpContextHeader("Accept-Encoding", "");
+ final RestfulResponse response = httpRest.head("path/to/service", parametes);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testHeadStringRestfulParametesRestfulOptions() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parametes = new RestfulParametes();
+ parametes.put("id", "1234");
+ parametes.put("name", "some-name");
+ parametes.put("address", null);
+ parametes.putHttpContextHeader("Content-Type", "");
+ parametes.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(10);
+ }
+
+ };
+ final RestfulResponse response = httpRest.head("path/to/service", parametes, options);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @param options
+ * @return
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException {
+ final HttpRest httpRest = new HttpRest();
+ {
+ new MockUp<HttpClient>() {
+
+ @Mock
+ public void doStart() {
+ System.out.println("started");
+ }
+
+ @Mock
+ public void send(final HttpExchange exchange) throws IOException {
+ System.out.println("send");
+ }
+ };
+ httpRest.initHttpRest(options);
+
+ }
+ return httpRest;
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncGet("path/to/service", new RestfulParametes(), callback);
+ httpRest.asyncGet("path/to/service", new RestfulParametes(), null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
+ httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testPutStringRestfulParametes() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parametes = new RestfulParametes();
+ parametes.put("id", "1234");
+ parametes.put("name", "some-name");
+ parametes.put("address", null);
+ parametes.putHttpContextHeader("Content-Type", "");
+ parametes.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(-1);
+ }
+
+ };
+ final RestfulResponse response = httpRest.put("path/to/service", parametes);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testPutStringRestfulParametesRestfulOptions() throws ServiceException {
+
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parametes = new RestfulParametes();
+ parametes.put("id", "1234");
+ parametes.put("name", "some-name");
+ parametes.put("address", null);
+ parametes.putHttpContextHeader("Content-Type", "");
+ parametes.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(10);
+ }
+
+ };
+ final RestfulResponse response = httpRest.put("path/to/service", parametes, null);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncPut("path/to/service", new RestfulParametes(), callback);
+ httpRest.asyncPut("path/to/service", new RestfulParametes(), null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback);
+ httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testPostStringRestfulParametes() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone");
+ return HttpExchange.STATUS_EXPIRED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_EXPIRED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parameters = new RestfulParametes();
+ parameters.put("id", "1234");
+ parameters.put("name", "some-name");
+ parameters.put("address", null);
+ parameters.putHttpContextHeader("Content-Type", "");
+ parameters.putHttpContextHeader("Accept-Encoding", "");
+
+ thrown.expect(ServiceException.class);
+ thrown.expectMessage("request is expierd");
+ final RestfulResponse response = httpRest.post("http://localhost:80/path/to/service", parameters);
+ assertEquals(HttpExchange.STATUS_EXPIRED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testPostStringRestfulParametesRestfulOptions() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone" + HttpExchange.STATUS_EXCEPTED);
+ return HttpExchange.STATUS_EXCEPTED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parameters = new RestfulParametes();
+ parameters.put("id", "1234");
+ parameters.put("name", "some-name");
+ parameters.put("address", null);
+ parameters.setRawData("{ \"data\"=\"sample JSON data\"");
+ parameters.putHttpContextHeader("Content-Type", "");
+ parameters.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(10);
+ }
+
+ };
+ thrown.expect(ServiceException.class);
+ thrown.expectMessage("request is exception");
+ final RestfulResponse response = httpRest.post("path/to/service", parameters, null);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception {
+ final RestfulOptions options = new RestfulOptions();
+ options.setRestTimeout(10);
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone:" + HttpExchange.STATUS_EXCEPTED);
+ return 99;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_EXCEPTED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
+ httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+ options.setRestTimeout(10);
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback);
+ httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testDeleteStringRestfulParametes() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+
+ final RestfulResponse response = httpRest.delete("path/to/service", null);
+ assertEquals(-1, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parameters = new RestfulParametes();
+ parameters.put("id", "1234");
+ parameters.put("name", "some-name");
+ parameters.put("address", null);
+ parameters.setRawData("{ \"data\"=\"sample JSON data\"");
+ parameters.putHttpContextHeader("Content-Type", "");
+ parameters.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(10);
+ }
+
+ };
+ final RestfulResponse response = httpRest.delete("path/to/service", parameters, options);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+ options.setRestTimeout(10);
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback);
+ httpRest.asyncDelete("path/to/service", new RestfulParametes(), null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+ options.setRestTimeout(10);
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback);
+ httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testPatchStringRestfulParametes() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parameters = new RestfulParametes();
+ parameters.put("id", "1234");
+ parameters.put("name", "some-name");
+ parameters.put("address", null);
+ parameters.setRawData("{ \"data\"=\"sample JSON data\"");
+ parameters.putHttpContextHeader("Content-Type", "");
+ parameters.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(10);
+ }
+
+ };
+ final RestfulResponse response = httpRest.patch("path/to/service", parameters);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+ final RestfulParametes parameters = new RestfulParametes();
+ parameters.put("id", "1234");
+ parameters.put("name", "some-name");
+ parameters.put("address", null);
+ parameters.setRawData("{ \"data\"=\"sample JSON data\"");
+ parameters.putHttpContextHeader("Content-Type", "");
+ parameters.putHttpContextHeader("Accept-Encoding", "");
+ new Expectations() {
+
+ ServiceUtil serviceUtil;
+
+ {
+ serviceUtil = new ServiceUtil(anyString, anyString);
+ serviceUtil.getServiceHost();
+ returns("127.0.0.1");
+
+ serviceUtil.getServicePort();
+ returns(10);
+ }
+
+ };
+ final RestfulResponse response = httpRest.patch("path/to/service", parameters, options);
+ assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+ options.setRestTimeout(10);
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback);
+ httpRest.asyncPatch("path/to/service", new RestfulParametes(), null);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws ServiceException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException {
+ final RestfulOptions options = new RestfulOptions();
+ options.setRestTimeout(10);
+
+ final HttpBaseRest httpRest = getHttpRest(options);
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public int waitForDone() {
+ System.out.println("waitForDone:" + HttpExchange.STATUS_COMPLETED);
+ return HttpExchange.STATUS_COMPLETED;
+ }
+
+ @Mock
+ public RestfulResponse getResponse() throws IOException {
+ final RestfulResponse response = new RestfulResponse();
+ response.setStatus(HttpExchange.STATUS_COMPLETED);
+ return response;
+ }
+
+ };
+
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ System.out.println("callback called.");
+
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+
+ System.out.println("handleExcepion called.");
+ }
+
+ };
+ httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback);
+ httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null);
+ }
+
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestHttpContentExchange.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestHttpContentExchange.java
new file mode 100644
index 0000000..094e43d
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestHttpContentExchange.java
@@ -0,0 +1,590 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.eclipse.jetty.client.Address;
+import org.eclipse.jetty.client.CachedExchange;
+import org.eclipse.jetty.client.HttpDestination;
+import org.eclipse.jetty.client.HttpExchange;
+import org.eclipse.jetty.http.HttpFields;
+import org.eclipse.jetty.http.HttpHeaders;
+import org.eclipse.jetty.io.Buffer;
+import org.eclipse.jetty.io.ByteArrayBuffer;
+import org.eclipse.jetty.util.StringUtil;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.zip.GZIPInputStream;
+
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+import mockit.integration.junit4.JMockit;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 20-Jun-2016
+ */
+@RunWith(JMockit.class)
+public class TestRestHttpContentExchange {
+
+ @Mocked
+ HttpDestination mockedDest;
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.ERROR);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws IOException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnRequestCommitted() throws IOException {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onRequestCommitted();
+
+ LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
+ exchange.onRequestCommitted();
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws IOException
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnRequestComplete() throws IOException {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onRequestComplete();
+
+ LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
+ exchange.onRequestComplete();
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnResponseComplete() throws Exception {
+ RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onResponseComplete();
+
+ LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG);
+ exchange.onResponseComplete();
+
+ final AtomicInteger isCallback = new AtomicInteger(0);
+ final AtomicInteger isException = new AtomicInteger(0);
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ isCallback.set(1);
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+ isException.set(1);
+ }
+
+ };
+
+ final Field statusField = HttpExchange.class.getDeclaredField("_status");
+ statusField.setAccessible(true);
+ exchange = new RestHttpContentExchange(false, callback);
+ statusField.set(exchange, new AtomicInteger(200));
+ exchange.setAddress(new Address("localhost", 9999));
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onResponseComplete();
+ assertEquals(1, isCallback.get());
+ assertEquals(0, isException.get());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testDecompressGzipToStr() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+
+ final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz");
+ final byte[] binaryData = new byte[1024];
+ stream.read(binaryData);
+ final String expected = "sample data.";
+
+ final String actual = exchange.decompressGzipToStr(binaryData);
+
+ System.out.println("actual: '" + actual + "'");
+ System.out.println("expected: '" + expected + "'");
+ assertEquals(actual, expected);
+
+ new MockUp<ByteArrayInputStream>() {
+
+ @Mock
+ public int read() throws Exception {
+ throw new IOException();
+ }
+
+ @Mock
+ public int read(final byte abyte0[], final int i, final int j) {
+
+ return -1;
+ }
+
+ };
+
+ thrown.expect(IOException.class);
+ exchange.decompressGzipToStr(binaryData);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testDecompressGzipToStrException() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+
+ final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz");
+ final byte[] binaryData = new byte[1024];
+ stream.read(binaryData);
+ final String expected = "sample data.";
+
+ new MockUp<GZIPInputStream>() {
+
+ @Mock
+ public void close() throws IOException {
+ throw new IOException();
+ }
+
+ };
+
+ new MockUp<InputStreamReader>() {
+
+ @Mock
+ public void close() throws IOException {
+ throw new IOException();
+ }
+
+ };
+
+ new MockUp<ByteArrayInputStream>() {
+
+ @Mock
+ public void close() throws IOException {
+ throw new IOException();
+ }
+
+ };
+
+ final String actual = exchange.decompressGzipToStr(binaryData);
+
+ System.out.println("actual: '" + actual + "'");
+ System.out.println("expected: '" + expected + "'");
+ assertEquals(actual, expected);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testDecompressGzipToStrNull() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ final String expected = "";
+ final String actual = exchange.decompressGzipToStr(null);
+
+ System.out.println("actual: '" + actual + "'");
+ System.out.println("expected: '" + expected + "'");
+ assertEquals(actual, expected);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnResponseHeaderBufferBuffer() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+
+ final Buffer name = new ByteArrayBuffer("key");
+ final Buffer value = new ByteArrayBuffer("value");
+ exchange.onResponseHeader(name, value);
+
+ new MockUp<HttpHeaders>() {
+
+ @Mock
+ public int getOrdinal(final Buffer buffer) {
+ return HttpHeaders.CONTENT_ENCODING_ORDINAL;
+ }
+
+ };
+ exchange.onResponseHeader(name, value);
+
+ new MockUp<StringUtil>() {
+
+ @Mock
+ public String asciiToLowerCase(final String s) {
+ return "gzip";
+ }
+
+ };
+ exchange.onResponseHeader(name, value);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnExceptionThrowable() {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onException(new Exception());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnExceptionThrowableWithCallback() {
+ final AtomicInteger isCallback = new AtomicInteger(0);
+ final AtomicInteger isException = new AtomicInteger(0);
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ isCallback.set(1);
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+ isException.set(1);
+ }
+
+ };
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onException(new Exception());
+ assertEquals(0, isCallback.get());
+ assertEquals(1, isException.get());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnConnectionFailedThrowable() {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onConnectionFailed(new Exception());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testOnConnectionFailedThrowableException() {
+ final AtomicInteger isCallback = new AtomicInteger(0);
+ final AtomicInteger isException = new AtomicInteger(0);
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ isCallback.set(1);
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+ isException.set(1);
+ }
+
+ };
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.onConnectionFailed(new Exception());
+ assertEquals(0, isCallback.get());
+ assertEquals(1, isException.get());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testExpireHttpDestination() {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(true, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.expire(mockedDest);
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testExpireHttpDestinationException() throws Exception {
+ final AtomicInteger isCallback = new AtomicInteger(0);
+ final AtomicInteger isException = new AtomicInteger(0);
+ final List<Throwable> thrSet = new ArrayList<Throwable>();
+ final RestfulAsyncCallback callback = new RestfulAsyncCallback() {
+
+ @Override
+ public void callback(final RestfulResponse response) {
+ isCallback.set(1);
+ }
+
+ @Override
+ public void handleExcepion(final Throwable e) {
+ isException.set(1);
+ thrSet.add(e);
+ }
+
+ };
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ exchange.expire(mockedDest);
+ assertEquals(0, isCallback.get());
+ assertEquals(1, isException.get());
+ assertEquals(1, thrSet.size());
+ final Throwable t = thrSet.get(0);
+ assertEquals(ServiceException.class, t.getClass());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testIsGzip() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+
+ final Buffer name = new ByteArrayBuffer("key");
+ final Buffer value = new ByteArrayBuffer("value");
+
+ new MockUp<HttpHeaders>() {
+
+ @Mock
+ public int getOrdinal(final Buffer buffer) {
+ return HttpHeaders.CONTENT_ENCODING_ORDINAL;
+ }
+
+ };
+ exchange.onResponseHeader(name, value);
+ assertFalse(exchange.isGzip());
+
+ new MockUp<StringUtil>() {
+
+ @Mock
+ public String asciiToLowerCase(final String s) {
+ return "gzip";
+ }
+
+ };
+ exchange.onResponseHeader(name, value);
+ assertTrue(exchange.isGzip());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetResponse() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+
+ final Field statusField = HttpExchange.class.getDeclaredField("_status");
+ statusField.setAccessible(true);
+ statusField.set(exchange, new AtomicInteger(200));
+
+ RestfulResponse response = exchange.getResponse();
+ assertEquals(0, response.getStatus());
+
+ final HttpFields fields = new HttpFields();
+ final Field headerFields = CachedExchange.class.getDeclaredField("_responseFields");
+ headerFields.setAccessible(true);
+ headerFields.set(exchange, fields);
+ response = exchange.getResponse();
+ assertEquals(0, response.getStatus());
+ fields.add("Content-Type", "application/json");
+ fields.add("Content-Encode", "UTF-8");
+ response = exchange.getResponse();
+ assertEquals(0, response.getStatus());
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetResponseGzip() throws Exception {
+ final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null);
+ final Address address = new Address("localhost", 9999);
+ exchange.setAddress(address);
+ exchange.setRequestURI("/the/request/uri");
+ new MockUp<RestHttpContentExchange>() {
+
+ @Mock
+ public boolean isGzip() {
+ return true;
+ }
+ };
+ final Field statusField = HttpExchange.class.getDeclaredField("_status");
+ statusField.setAccessible(true);
+ statusField.set(exchange, new AtomicInteger(200));
+
+ final RestfulResponse response = exchange.getResponse();
+ assertEquals(0, response.getStatus());
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulConfigure.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulConfigure.java
new file mode 100644
index 0000000..30b5620
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulConfigure.java
@@ -0,0 +1,245 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertEquals;
+
+import org.openo.baseservice.util.impl.SystemEnvVariablesDefImpl;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import net.sf.json.JSONObject;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import mockit.Mock;
+import mockit.MockUp;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 20-Jun-2016
+ */
+public class TestRestfulConfigure {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ setAppRoot();
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ private static void setAppRoot() {
+ final URL resource = ClassLoader.getSystemResource("etc/conf/restclient.json");
+ final String urlPath = resource.getPath().replace("etc/conf/restclient.json", "");
+
+ try {
+ final String path = new File(urlPath).getCanonicalPath();
+ System.out.println("path: " + path);
+
+ System.setProperty("catalina.base", path);
+ System.out.println("approot:" + System.getProperty("catalina.base"));
+ } catch(final IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public final void testRestfulConfigure() throws Exception {
+ final RestfulConfigure configure = new RestfulConfigure();
+ final RestfulOptions options = configure.getOptions();
+ assertEquals("127.0.0.1", options.getHost());
+ assertEquals(8080, options.getPort());
+ assertEquals(1000, options.getIntOption("ConnectTimeout"));
+ assertEquals(10, options.getIntOption("thread"));
+ assertEquals(500000, options.getIntOption("idletimeout"));
+ assertEquals(10000, options.getIntOption("timeout"));
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public final void testRestfulConfigureAppRootNull() throws Exception {
+ new MockUp<SystemEnvVariablesDefImpl>() {
+
+ @Mock
+ public String getAppRoot() {
+ return null;
+ }
+
+ };
+ final RestfulConfigure configure = new RestfulConfigure();
+ final RestfulOptions options = configure.getOptions();
+
+ assertEquals("", options.getHost());
+ assertEquals(0, options.getPort());
+ assertEquals(3000, options.getIntOption("ConnectTimeout"));
+ assertEquals(200, options.getIntOption("thread"));
+ assertEquals(30000, options.getIntOption("idletimeout"));
+ assertEquals(30000, options.getIntOption("timeout"));
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public final void testRestfulConfigureDefault() throws Exception {
+
+ {
+
+ new MockUp<File>() {
+
+ @Mock
+ public boolean isFile() {
+ return false;
+ }
+ };
+ final RestfulConfigure configure = new RestfulConfigure();
+ final RestfulOptions options = configure.getOptions();
+ assertEquals("", options.getHost());
+ assertEquals(0, options.getPort());
+ assertEquals(3000, options.getIntOption("ConnectTimeout"));
+ assertEquals(200, options.getIntOption("thread"));
+ assertEquals(30000, options.getIntOption("idletimeout"));
+ assertEquals(30000, options.getIntOption("timeout"));
+ }
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws Exception
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public final void testRestfulConfigureException() throws Exception {
+
+ new MockUp<JSONObject>() {
+
+ @Mock
+ JSONObject fromObject(final Object object) throws IOException {
+ throw new IOException();
+ }
+ };
+
+ final RestfulConfigure configure = new RestfulConfigure();
+ final RestfulOptions options = configure.getOptions();
+ assertEquals("", options.getHost());
+ assertEquals(0, options.getPort());
+ assertEquals(3000, options.getIntOption("ConnectTimeout"));
+ assertEquals(200, options.getIntOption("thread"));
+ assertEquals(30000, options.getIntOption("idletimeout"));
+ assertEquals(30000, options.getIntOption("timeout"));
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public final void testRestfulConfigureString() {
+ final String configFile = "rest-client-test.json";
+ final String appRoot = System.getProperty("catalina.base");
+ final RestfulConfigure configure = new RestfulConfigure(appRoot + File.separator + configFile);
+ final RestfulOptions options = configure.getOptions();
+ assertEquals("10.10.10.10", options.getHost());
+ assertEquals(443, options.getPort());
+ assertEquals(10, options.getIntOption("ConnectTimeout"));
+ assertEquals(100, options.getIntOption("thread"));
+ assertEquals(30, options.getIntOption("idletimeout"));
+ assertEquals(60, options.getIntOption("timeout"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Ignore
+ @Test
+ public final void testGetOptions() {
+ }
+} \ No newline at end of file
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulFactory.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulFactory.java
new file mode 100644
index 0000000..70c7dea
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulFactory.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import mockit.Mock;
+import mockit.MockUp;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 21-Jun-2016
+ */
+public class TestRestfulFactory {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRestInstance() {
+ Restful instance = RestfulFactory.getRestInstance("https");
+ assertNull(instance);
+ new MockUp<HttpRest>() {
+
+ @Mock
+ public void initHttpRest(final RestfulOptions option) throws ServiceException {
+ throw new ServiceException();
+ }
+
+ };
+ instance = RestfulFactory.getRestInstance("http");
+ assertNotNull(instance);
+
+ instance = RestfulFactory.getRestInstance("http");
+ assertNotNull(instance);
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulOptions.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulOptions.java
new file mode 100644
index 0000000..624f54c
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulOptions.java
@@ -0,0 +1,315 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import mockit.integration.junit4.JMockit;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 20-Jun-2016
+ */
+@RunWith(JMockit.class)
+public class TestRestfulOptions {
+
+ @Rule
+ final public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetCalledServiceName() {
+ final RestfulOptions options = new RestfulOptions();
+ final String serviceName = "sample-service";
+ assertTrue(options.setCalledServiceName(serviceName));
+ assertEquals(serviceName, options.getCalledServicName());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetCalledServicName() {
+ final RestfulOptions options = new RestfulOptions();
+ final String serviceName = "sample-service";
+ assertEquals("", options.getCalledServicName());
+ options.setCalledServiceName(serviceName);
+ assertEquals(serviceName, options.getCalledServicName());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetPort() {
+ final RestfulOptions options = new RestfulOptions();
+ final int port = 9091;
+ assertEquals(0, options.getPort());
+ options.setPort(port);
+ assertEquals(port, options.getPort());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetPort() {
+ final RestfulOptions options = new RestfulOptions();
+ final int port = 9091;
+ assertTrue(options.setPort(port));
+ assertEquals(port, options.getPort());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetHost() {
+ final RestfulOptions options = new RestfulOptions();
+ final String host = "localhost";
+ assertEquals("", options.getHost());
+ options.setHost(host);
+ assertEquals(host, options.getHost());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetHost() {
+ final RestfulOptions options = new RestfulOptions();
+ final String host = "localhost";
+ assertTrue(options.setHost(host));
+ assertEquals(host, options.getHost());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetRestTimeout() {
+ final RestfulOptions options = new RestfulOptions();
+ int timeout = 0;
+ assertFalse(options.setRestTimeout(timeout));
+ assertEquals(0, options.getRestTimeout());
+
+ timeout = 1;
+ assertTrue(options.setRestTimeout(timeout));
+ assertEquals(timeout, options.getRestTimeout());
+
+ timeout = 10;
+ assertTrue(options.setRestTimeout(timeout));
+ assertEquals(timeout, options.getRestTimeout());
+
+ timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1;
+ assertTrue(options.setRestTimeout(timeout));
+ assertEquals(timeout, options.getRestTimeout());
+
+ timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT;
+ assertTrue(options.setRestTimeout(timeout));
+ assertEquals(timeout, options.getRestTimeout());
+
+ timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1;
+ assertFalse(options.setRestTimeout(timeout));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRestTimeout() {
+ final RestfulOptions options = new RestfulOptions();
+ int timeout = 0;
+ assertEquals(0, options.getRestTimeout());
+
+ timeout = 1;
+ assertTrue(options.setRestTimeout(timeout));
+ assertEquals(timeout, options.getRestTimeout());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetOption() {
+ final RestfulOptions options = new RestfulOptions();
+ assertNull(options.getOption("invalid"));
+
+ options.setHost("localhost");
+ Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
+ assertNotNull(obj);
+ assertTrue(obj instanceof String);
+ assertEquals("localhost", obj);
+
+ final List<String> list = new ArrayList<String>();
+ list.add("data");
+ options.setOption("list", list);
+ obj = options.getOption("list");
+ assertNotNull(obj);
+ assertTrue(obj instanceof List);
+ assertSame(list, obj);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetIntOption() {
+ final RestfulOptions options = new RestfulOptions();
+
+ assertEquals(0, options.getIntOption("count"));
+
+ options.setOption("count", 1);
+ assertEquals(1, options.getIntOption("count"));
+
+ thrown.expect(RuntimeException.class);
+
+ options.setOption("string-count", "two");
+ final int value = options.getIntOption("string-count");
+ assertEquals(2, value);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetStringOption() {
+ final RestfulOptions options = new RestfulOptions();
+
+ assertEquals("", options.getStringOption("count"));
+
+ options.setOption("string-count", "one");
+ assertEquals("one", options.getStringOption("string-count"));
+
+ thrown.expect(RuntimeException.class);
+
+ options.setOption("count", 2);
+ final String value = options.getStringOption("count");
+ assertEquals(2, value);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetOption() {
+ final RestfulOptions options = new RestfulOptions();
+ assertNull(options.getOption("invalid"));
+
+ options.setHost("localhost");
+ Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME);
+ assertNotNull(obj);
+ assertTrue(obj instanceof String);
+ assertEquals("localhost", obj);
+
+ final List<String> list = new ArrayList<String>();
+ list.add("data");
+ options.setOption("list", list);
+ obj = options.getOption("list");
+ assertNotNull(obj);
+ assertTrue(obj instanceof List);
+ assertSame(list, obj);
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulParametes.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulParametes.java
new file mode 100644
index 0000000..fd6fd9f
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulParametes.java
@@ -0,0 +1,229 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 21-Jun-2016
+ */
+public class TestRestfulParametes {
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGet() {
+ final RestfulParametes params = new RestfulParametes();
+ assertNull(params.get("param"));
+ final Map<String, String> paramMap = new HashMap<String, String>();
+ params.setParamMap(paramMap);
+ paramMap.put("param", "value");
+ assertEquals("value", params.get("param"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetRawData() {
+ final RestfulParametes params = new RestfulParametes();
+ final String data = "Sample data.";
+ params.setRawData(data);
+ assertEquals(data, params.getRawData());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRawData() {
+ final RestfulParametes params = new RestfulParametes();
+ assertNull(params.getRawData());
+ final String data = "Sample data.";
+ params.setRawData(data);
+ assertEquals(data, params.getRawData());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testPut() {
+ final RestfulParametes params = new RestfulParametes();
+ params.put("somekey", "somevalue");
+ params.put("otherkey", "othervalue");
+ assertEquals("somevalue", params.get("somekey"));
+ assertEquals("othervalue", params.get("otherkey"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testPutHttpContextHeaderStringString() {
+ final RestfulParametes params = new RestfulParametes();
+ params.putHttpContextHeader("Context-Encoding", "UTF-8");
+ assertEquals("UTF-8", params.getHttpContextHeader("Context-Encoding"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testPutHttpContextHeaderStringInt() {
+ final RestfulParametes params = new RestfulParametes();
+ params.putHttpContextHeader("Expire-At", 1000);
+ assertEquals("1000", params.getHttpContextHeader("Expire-At"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetHttpContextHeader() {
+ final RestfulParametes params = new RestfulParametes();
+ params.putHttpContextHeader("Expire-At", 1000);
+ params.putHttpContextHeader("Context-Encoding", "UTF-8");
+ assertEquals("1000", params.getHttpContextHeader("Expire-At"));
+ assertEquals("UTF-8", params.getHttpContextHeader("Context-Encoding"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetParamMap() {
+ final RestfulParametes params = new RestfulParametes();
+ params.put("key", "value");
+ final Map<String, String> map = params.getParamMap();
+ assertEquals("value", map.get("key"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetParamMap() {
+ final RestfulParametes params = new RestfulParametes();
+ final Map<String, String> map = new HashMap<String, String>();
+ map.put("key", "value");
+ params.setParamMap(map);
+ assertEquals("value", params.get("key"));
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetHeaderMap() {
+ final RestfulParametes params = new RestfulParametes();
+ params.putHttpContextHeader("key", "value");
+ final Map<String, String> map = params.getHeaderMap();
+ assertEquals("value", map.get("key"));
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetHeaderMap() {
+ final RestfulParametes params = new RestfulParametes();
+ final Map<String, String> map = new HashMap<String, String>();
+ map.put("key", "value");
+ params.setHeaderMap(map);
+ assertEquals("value", params.getHttpContextHeader("key"));
+ }
+}
diff --git a/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulResponse.java b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulResponse.java
new file mode 100644
index 0000000..c837aec
--- /dev/null
+++ b/rest-client/src/test/java/org/openo/baseservice/roa/util/restclient/TestRestfulResponse.java
@@ -0,0 +1,273 @@
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.baseservice.roa.util.restclient;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version SDNO 0.5 20-Jun-2016
+ */
+public class TestRestfulResponse {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @throws java.lang.Exception
+ * @since SDNO 0.5
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetStatus() {
+ final RestfulResponse response = new RestfulResponse();
+ int actual = response.getStatus();
+ int expected = -1;
+
+ assertEquals(expected, actual);
+ expected = 202;
+ response.setStatus(expected);
+ actual = response.getStatus();
+ assertEquals(expected, actual);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetStatus() {
+ final RestfulResponse response = new RestfulResponse();
+ final int expected = 10;
+ response.setStatus(expected);
+ final int actual = response.getStatus();
+ assertEquals(expected, actual);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRespHeaderMap() {
+ final RestfulResponse response = new RestfulResponse();
+ Map<String, String> expected = response.getRespHeaderMap();
+ assertNull(expected);
+ expected = new HashMap<String, String>();
+ expected.put("key", "value");
+ response.setRespHeaderMap(expected);
+ final Map<String, String> actual = response.getRespHeaderMap();
+ assertNotNull(actual);
+ assertSame(actual, expected);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetRespHeaderMap() {
+ final RestfulResponse response = new RestfulResponse();
+ response.setRespHeaderMap(null);
+ Map<String, String> expected = response.getRespHeaderMap();
+ assertNull(expected);
+ expected = new HashMap<String, String>();
+ expected.put("key", "value");
+ response.setRespHeaderMap(expected);
+ final Map<String, String> actual = response.getRespHeaderMap();
+ assertNotNull(actual);
+ assertSame(actual, expected);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRespHeaderInt() {
+ final RestfulResponse response = new RestfulResponse();
+ response.setRespHeaderMap(null);
+ int actual = response.getRespHeaderInt("somekey");
+ assertEquals(-1, actual);
+ final Map<String, String> headers = new HashMap<String, String>();
+ headers.put("key", "value");
+ headers.put("count", "1");
+ response.setRespHeaderMap(headers);
+ actual = response.getRespHeaderInt("somekey");
+ assertEquals(-1, actual);
+
+ actual = response.getRespHeaderInt("count");
+ assertEquals(1, actual);
+
+ thrown.expect(RuntimeException.class);
+ actual = response.getRespHeaderInt("key");
+ assertEquals(1, actual);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRespHeaderLong() {
+ final RestfulResponse response = new RestfulResponse();
+ response.setRespHeaderMap(null);
+ long actual = response.getRespHeaderLong("somekey");
+ assertEquals(-1, actual);
+ final Map<String, String> headers = new HashMap<String, String>();
+ headers.put("key", "value");
+ headers.put("count", "1");
+ headers.put("max", "" + Long.MAX_VALUE);
+ headers.put("max++", Long.MAX_VALUE + 1 + "");
+ response.setRespHeaderMap(headers);
+ actual = response.getRespHeaderLong("somekey");
+ assertEquals(-1, actual);
+
+ actual = response.getRespHeaderLong("count");
+ assertEquals(1, actual);
+
+ actual = response.getRespHeaderLong("max");
+ assertEquals(Long.MAX_VALUE, actual);
+
+ actual = response.getRespHeaderLong("max++");
+ assertTrue(actual < 0);
+
+ thrown.expect(RuntimeException.class);
+ actual = response.getRespHeaderLong("key");
+ assertEquals(1, actual);
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetRespHeaderStr() {
+ final RestfulResponse response = new RestfulResponse();
+ response.setRespHeaderMap(null);
+ String actual = response.getRespHeaderStr("somekey");
+ assertEquals(null, actual);
+ final Map<String, String> headers = new HashMap<String, String>();
+ headers.put("key", "value");
+ headers.put("count", "1");
+ headers.put("max", "" + Long.MAX_VALUE);
+ response.setRespHeaderMap(headers);
+ actual = response.getRespHeaderStr("somekey");
+ assertEquals(null, actual);
+
+ actual = response.getRespHeaderStr("key");
+ assertEquals("value", actual);
+
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testGetResponseContent() {
+ final RestfulResponse response = new RestfulResponse();
+ assertEquals(null, response.getResponseContent());
+
+ final String content = "{ \"content\" = \"The response content\" }";
+ response.setResponseJson(content);
+ assertEquals(content, response.getResponseContent());
+ }
+
+ /**
+ * <br/>
+ *
+ * @since SDNO 0.5
+ */
+ @Test
+ public void testSetResponseJson() {
+ final RestfulResponse response = new RestfulResponse();
+ assertEquals(null, response.getResponseContent());
+
+ final String content = "{ \"content\" = \"The response content\" }";
+ response.setResponseJson(content);
+ assertEquals(content, response.getResponseContent());
+ }
+}