2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.res.common.util.request;
19 import static org.junit.Assert.*;
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;
30 import javax.servlet.ServletInputStream;
31 import javax.servlet.http.HttpServletRequest;
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;
38 import javassist.Modifier;
42 import net.sf.json.JSONException;
43 import net.sf.json.JSONObject;
45 public class RequestUtilTest {
48 public void testGetStringRequestBody() {
49 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
52 ServletInputStream input;
55 public ServletInputStream getInputStream() throws IOException {
59 new MockUp<IOUtils>() {
61 String data = "{\"NETWORK\":{\"id\": \"123\"}}";
64 public String toString(InputStream input) throws IOException {
68 String result = RequestUtil.getStringRequestBody(context);
69 String expectedResult = "{\"NETWORK\":{\"id\": \"123\"}}";
70 assertEquals(expectedResult, result);
74 public void testGetStringRequestBodyException() {
75 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
78 public ServletInputStream getInputStream() throws IOException {
79 throw new IOException();
82 String result = RequestUtil.getStringRequestBody(context);
83 String expectedResult = null;
84 assertEquals(expectedResult, result);
88 public void testGetJsonRequestBody() {
89 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
92 ServletInputStream input;
95 public ServletInputStream getInputStream() throws IOException {
99 new MockUp<IOUtils>() {
101 String data = "{\"NETWORK\":{\"id\": \"123\"}}";
104 public String toString(InputStream input) throws IOException {
108 JSONObject result = RequestUtil.getJsonRequestBody(context);
109 String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
110 JSONObject expectedResult = JSONObject.fromObject(data1);
111 assertEquals(expectedResult, result);
115 public void testGetJsonRequestBody1() {
116 new MockUp<RequestUtil>() {
118 String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
121 public String getStringRequestBody(HttpServletRequest context) {
125 JSONObject result = RequestUtil.getJsonRequestBody(null);
126 String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
127 JSONObject expectedResult = JSONObject.fromObject(data1);
128 assertEquals(expectedResult, result);
132 public void testGetJsonRequestBodyException() {
133 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
136 ServletInputStream input;
139 public ServletInputStream getInputStream() throws JSONException {
140 throw new JSONException();
143 JSONObject result = RequestUtil.getJsonRequestBody(context);
144 JSONObject expectedResult = null;
145 assertEquals(expectedResult, result);
148 @SuppressWarnings("rawtypes")
150 public void testGetAllJsonRequestBodyRequestBodyIsNull() {
151 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
154 ServletInputStream input;
157 public ServletInputStream getInputStream() throws IOException {
162 public Enumeration getHeaderNames() {
163 return new Enumeration() {
165 List<String> a = Arrays.asList(new String[] { "1", "2" });
168 public boolean hasMoreElements() {
173 public Object nextElement() {
181 new MockUp<RequestUtil>() {
184 public JSONObject getJsonRequestBody(HttpServletRequest context) {
188 JSONObject result = RequestUtil.getAllJsonRequestBody(context);
189 JSONObject expectedResult = new JSONObject();
190 expectedResult.put("header", new HashMap<String, String>());
191 assertEquals(expectedResult, result);
194 @SuppressWarnings("rawtypes")
196 public void testGetContextHeader() {
197 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
200 public String getHeader(String name) {
205 public Enumeration getHeaderNames() {
206 return new Enumeration() {
208 List<String> a = Arrays.asList(new String[] { "1", "2" });
213 public boolean hasMoreElements() {
222 public Object nextElement() {
230 new MockUp<RequestUtil>() {
233 public JSONObject getJsonRequestBody(HttpServletRequest context) {
237 JSONObject result = RequestUtil.getAllJsonRequestBody(context);
238 JSONObject expectedResult = new JSONObject();
239 Map<String, String> map = new HashMap<String, String>();
241 expectedResult.put("header", map);
242 assertEquals(expectedResult, result);
245 public void testPrivateConstructor() throws Exception {
246 Constructor constructor = RequestUtil.class.getDeclaredConstructor();
247 assertTrue("Constructor is private", Modifier.isPrivate(constructor.getModifiers()));
249 constructor.setAccessible(true);
250 constructor.newInstance();