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