419d4d0bc45007a69f26a5dde5079f92b8a5d474
[vfc/nfvo/wfengine.git] / CommonLibrary / rest-client / src / test / java / org / openo / baseservice / roa / util / clientsdk / TestHttpUtil.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openo.baseservice.roa.util.clientsdk;
18
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLEncoder;
21 import java.util.Arrays;
22 import java.util.Date;
23
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
31
32 import mockit.Mocked;
33 import mockit.NonStrictExpectations;
34
35 /**
36  * <br/>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version SDNO 0.5 13-Jun-2016
42  */
43 public class TestHttpUtil {
44
45     /**
46      * <br/>
47      * 
48      * @throws java.lang.Exception
49      * @since SDNO 0.5
50      */
51     @BeforeClass
52     public static void setUpBeforeClass() throws Exception {
53     }
54
55     /**
56      * <br/>
57      * 
58      * @throws java.lang.Exception
59      * @since SDNO 0.5
60      */
61     @AfterClass
62     public static void tearDownAfterClass() throws Exception {
63     }
64
65     /**
66      * <br/>
67      * 
68      * @throws java.lang.Exception
69      * @since SDNO 0.5
70      */
71     @Before
72     public void setUp() throws Exception {
73     }
74
75     /**
76      * <br/>
77      * 
78      * @throws java.lang.Exception
79      * @since SDNO 0.5
80      */
81     @After
82     public void tearDown() throws Exception {
83     }
84
85     /**
86      * <br/>
87      * 
88      * @since SDNO 0.5
89      */
90     @Test
91     public void testContainsIgnoreCase() {
92         final String[] array = {"hello", "how", "are", "you", "?"};
93         final String toFind = "Hello";
94         Assert.assertTrue(HttpUtil.containsIgnoreCase(array, toFind));
95     }
96
97     /**
98      * <br/>
99      * 
100      * @since SDNO 0.5
101      */
102     @Test
103     public void testContainsIgnoreCaseNull() {
104         final String[] array = {"hello", "how", "are", "you", "?"};
105         final String toFind = "Hello";
106         Assert.assertFalse(HttpUtil.containsIgnoreCase(array, null));
107
108         array[0] = null;
109         Assert.assertFalse(HttpUtil.containsIgnoreCase(array, toFind));
110
111         Assert.assertTrue(HttpUtil.containsIgnoreCase(array, null));
112         array[0] = "hello";
113         array[array.length - 1] = null;
114         Assert.assertTrue(HttpUtil.containsIgnoreCase(array, null));
115     }
116
117     /**
118      * <br/>
119      * 
120      * @since SDNO 0.5
121      */
122     @Test
123     public void testJoin() {
124         final String[] array = {"hello", "how", "are", "you", "?"};
125         String actual = HttpUtil.join(array, ",");
126         String expected = "hello,how,are,you,?";
127         Assert.assertEquals(actual, expected);
128
129         actual = HttpUtil.join(array, "#");
130         expected = expected.replaceAll(",", "#");
131         Assert.assertEquals(actual, expected);
132         actual = HttpUtil.join(new String[] {}, ",");
133         Assert.assertEquals(actual, "");
134     }
135
136     /**
137      * <br/>
138      * 
139      * @since SDNO 0.5
140      */
141     @Test
142     public void testParameterToString() {
143         // with param string.
144         Object param = new String("String Param");
145         String actual = HttpUtil.parameterToString(param);
146         String expected = "String Param";
147         Assert.assertEquals(expected, actual);
148
149         // with param date.
150         final Date date = new Date();
151         param = date;
152         expected = "" + date.getTime();
153         actual = HttpUtil.parameterToString(param);
154         Assert.assertEquals(expected, actual);
155
156         // with param collection.
157         final String[] array = {"hello", "how", "are", "you", "?"};
158         param = Arrays.asList(array);
159         expected = HttpUtil.join(array, ",");
160         actual = HttpUtil.parameterToString(param);
161         Assert.assertEquals(expected, actual);
162
163         // with param any
164         param = new Object() {
165
166             @Override
167             public String toString() {
168                 return "test object";
169             }
170         };
171         expected = "test object";
172         actual = HttpUtil.parameterToString(param);
173         Assert.assertEquals(expected, actual);
174
175         // with param null.
176         expected = "";
177         actual = HttpUtil.parameterToString(null);
178         Assert.assertEquals(expected, actual);
179
180     }
181
182     /**
183      * <br/>
184      * 
185      * @since SDNO 0.5
186      */
187     @Test
188     public void testSelectHeaderAccept() {
189         final String[] accepts = {"application/json", "text/plain", "application/xml"};
190         String expected = "application/json";
191         String actual = HttpUtil.selectHeaderAccept(accepts);
192         Assert.assertEquals(expected, actual);
193
194         accepts[0] = "application/x-www-form-urlencoded";
195         expected = HttpUtil.join(accepts, ",");
196         actual = HttpUtil.selectHeaderAccept(accepts);
197         Assert.assertEquals(expected, actual);
198
199         expected = null;
200         actual = HttpUtil.selectHeaderAccept(new String[] {});
201         Assert.assertEquals(expected, actual);
202
203     }
204
205     /**
206      * <br/>
207      * 
208      * @since SDNO 0.5
209      */
210     @Test
211     public void testSelectHeaderContentType() {
212         final String[] accepts = {"application/json", "text/plain", "application/xml"};
213         String expected = "application/json";
214         String actual = HttpUtil.selectHeaderContentType(accepts);
215         Assert.assertEquals(expected, actual);
216
217         accepts[0] = "application/x-www-form-urlencoded";
218         expected = "application/x-www-form-urlencoded";
219         actual = HttpUtil.selectHeaderContentType(accepts);
220         Assert.assertEquals(expected, actual);
221
222         expected = "application/json";
223         actual = HttpUtil.selectHeaderContentType(new String[] {});
224         Assert.assertEquals(expected, actual);
225     }
226
227     /**
228      * <br/>
229      * 
230      * @throws Exception
231      * @since SDNO 0.5
232      */
233     @Test
234     public void testEscapeString() throws Exception {
235         final String str = "/this/url/to be encoded";
236         final String actual = HttpUtil.escapeString(str);
237         final String expected = "%2Fthis%2Furl%2Fto%20be%20encoded";
238         Assert.assertEquals(expected, actual);
239     }
240
241     /**
242      * <br/>
243      * 
244      * @throws Exception
245      * @since SDNO 0.5
246      */
247     @Ignore
248     @Test
249     public void testEscapeStringException() throws Exception {
250
251         final String str = "/this/url/to be encoded";
252         new NonStrictExpectations() {
253
254             @Mocked
255             URLEncoder encoder;
256
257             {
258                 URLEncoder.encode(str, "utf8");
259                 result = new UnsupportedEncodingException();
260             }
261         };
262
263         final String actual = HttpUtil.escapeString(str);
264         Assert.assertEquals(str, actual);
265     }
266 }