e4d34b84fd7713d2c8746634f30094b78f208d72
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 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.nfvo.resmanagement.common.util.request;
18
19 import static org.junit.Assert.*;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.reflect.Constructor;
24 import java.util.Arrays;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.servlet.ServletInputStream;
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.apache.commons.io.IOUtils;
34 import org.apache.cxf.jaxrs.impl.HttpServletRequestFilter;
35 import org.junit.Test;
36
37 import javassist.Modifier;
38 import mockit.Mock;
39 import mockit.MockUp;
40 import mockit.Mocked;
41 import net.sf.json.JSONException;
42 import net.sf.json.JSONObject;
43
44 public class RequestUtilTest {
45
46     @Test
47     public void testGetStringRequestBody() {
48         HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
49
50             @Mocked
51             ServletInputStream input;
52
53             @Mock
54             public ServletInputStream getInputStream() throws IOException {
55                 return input;
56             }
57         }.getMockInstance();
58         new MockUp<IOUtils>() {
59
60             String data = "{\"NETWORK\":{\"id\": \"123\"}}";
61
62             @Mock
63             public String toString(InputStream input) throws IOException {
64                 return data;
65             }
66         };
67         String result = RequestUtil.getStringRequestBody(context);
68         String expectedResult = "{\"NETWORK\":{\"id\": \"123\"}}";
69         assertEquals(expectedResult, result);
70     }
71
72     @Test
73     public void testGetStringRequestBodyException() {
74         HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
75
76             @Mock
77             public ServletInputStream getInputStream() throws IOException {
78                 throw new IOException();
79             }
80         }.getMockInstance();
81         String result = RequestUtil.getStringRequestBody(context);
82         String expectedResult = null;
83         assertEquals(expectedResult, result);
84     }
85
86     @Test
87     public void testGetJsonRequestBody() {
88         HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
89
90             @Mocked
91             ServletInputStream input;
92
93             @Mock
94             public ServletInputStream getInputStream() throws IOException {
95                 return input;
96             }
97         }.getMockInstance();
98         new MockUp<IOUtils>() {
99
100             String data = "{\"NETWORK\":{\"id\": \"123\"}}";
101
102             @Mock
103             public String toString(InputStream input) throws IOException {
104                 return data;
105             }
106         };
107         JSONObject result = RequestUtil.getJsonRequestBody(context);
108         String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
109         JSONObject expectedResult = JSONObject.fromObject(data1);
110         assertEquals(expectedResult, result);
111     }
112
113     @Test
114     public void testGetJsonRequestBody1() {
115         new MockUp<RequestUtil>() {
116
117             String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
118
119             @Mock
120             public String getStringRequestBody(HttpServletRequest context) {
121                 return data1;
122             }
123         };
124         JSONObject result = RequestUtil.getJsonRequestBody(null);
125         String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
126         JSONObject expectedResult = JSONObject.fromObject(data1);
127         assertEquals(expectedResult, result);
128     }
129
130     @Test
131     public void testGetJsonRequestBodyException() {
132         HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
133
134             @Mocked
135             ServletInputStream input;
136
137             @Mock
138             public ServletInputStream getInputStream() throws JSONException {
139                 throw new JSONException();
140             }
141         }.getMockInstance();
142         JSONObject result = RequestUtil.getJsonRequestBody(context);
143         JSONObject expectedResult = null;
144         assertEquals(expectedResult, result);
145     }
146
147     @SuppressWarnings("rawtypes")
148     @Test
149     public void testGetAllJsonRequestBodyRequestBodyIsNull() {
150         HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
151
152             @Mocked
153             ServletInputStream input;
154
155             @Mock
156             public ServletInputStream getInputStream() throws IOException {
157                 return input;
158             }
159
160             @Mock
161             public Enumeration getHeaderNames() {
162                 return new Enumeration() {
163
164                     List<String> a = Arrays.asList(new String[] { "1", "2" });
165
166                     @Override
167                     public boolean hasMoreElements() {
168                         return false;
169                     }
170
171                     @Override
172                     public Object nextElement() {
173                         return null;
174                     }
175
176                 };
177             }
178
179         }.getMockInstance();
180         new MockUp<RequestUtil>() {
181
182             @Mock
183             public JSONObject getJsonRequestBody(HttpServletRequest context) {
184                 return null;
185             }
186         };
187         JSONObject result = RequestUtil.getAllJsonRequestBody(context);
188         JSONObject expectedResult = new JSONObject();
189         expectedResult.put("header", new HashMap<String, String>());
190         assertEquals(expectedResult, result);
191     }
192
193     @SuppressWarnings("rawtypes")
194     @Test
195     public void testGetContextHeader() {
196         HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
197
198             @Mock
199             public String getHeader(String name) {
200                 return "1";
201             }
202
203             @Mock
204             public Enumeration getHeaderNames() {
205                 return new Enumeration() {
206
207                     List<String> a = Arrays.asList(new String[] { "1", "2" });
208
209                     int count = 1;
210
211                     @Override
212                     public boolean hasMoreElements() {
213                         if (count == 1) {
214                             count += 1;
215                             return true;
216                         } else
217                             return false;
218                     }
219
220                     @Override
221                     public Object nextElement() {
222                         return "1";
223                     }
224
225                 };
226             }
227
228         }.getMockInstance();
229         new MockUp<RequestUtil>() {
230
231             @Mock
232             public JSONObject getJsonRequestBody(HttpServletRequest context) {
233                 return null;
234             }
235         };
236         JSONObject result = RequestUtil.getAllJsonRequestBody(context);
237         JSONObject expectedResult = new JSONObject();
238         Map<String, String> map = new HashMap<String, String>();
239         map.put("1", "1");
240         expectedResult.put("header", map);
241         assertEquals(expectedResult, result);
242     }
243     @Test
244     public void testPrivateConstructor() throws Exception {
245         Constructor constructor = RequestUtil.class.getDeclaredConstructor();
246         assertTrue("Constructor is  private", Modifier.isPrivate(constructor.getModifiers()));
247
248         constructor.setAccessible(true);
249         constructor.newInstance();
250     }
251
252 }