diff options
Diffstat (limited to 'rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestHttpUtil.java')
-rw-r--r-- | rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestHttpUtil.java | 266 |
1 files changed, 266 insertions, 0 deletions
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); + } +} |