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.openo.nfvo.resmanagement.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;
37 import javassist.Modifier;
41 import net.sf.json.JSONException;
42 import net.sf.json.JSONObject;
44 public class RequestUtilTest {
47 public void testGetStringRequestBody() {
48 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
51 ServletInputStream input;
54 public ServletInputStream getInputStream() throws IOException {
58 new MockUp<IOUtils>() {
60 String data = "{\"NETWORK\":{\"id\": \"123\"}}";
63 public String toString(InputStream input) throws IOException {
67 String result = RequestUtil.getStringRequestBody(context);
68 String expectedResult = "{\"NETWORK\":{\"id\": \"123\"}}";
69 assertEquals(expectedResult, result);
73 public void testGetStringRequestBodyException() {
74 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
77 public ServletInputStream getInputStream() throws IOException {
78 throw new IOException();
81 String result = RequestUtil.getStringRequestBody(context);
82 String expectedResult = null;
83 assertEquals(expectedResult, result);
87 public void testGetJsonRequestBody() {
88 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
91 ServletInputStream input;
94 public ServletInputStream getInputStream() throws IOException {
98 new MockUp<IOUtils>() {
100 String data = "{\"NETWORK\":{\"id\": \"123\"}}";
103 public String toString(InputStream input) throws IOException {
107 JSONObject result = RequestUtil.getJsonRequestBody(context);
108 String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
109 JSONObject expectedResult = JSONObject.fromObject(data1);
110 assertEquals(expectedResult, result);
114 public void testGetJsonRequestBody1() {
115 new MockUp<RequestUtil>() {
117 String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
120 public String getStringRequestBody(HttpServletRequest context) {
124 JSONObject result = RequestUtil.getJsonRequestBody(null);
125 String data1 = "{\"NETWORK\":{\"id\": \"123\"}}";
126 JSONObject expectedResult = JSONObject.fromObject(data1);
127 assertEquals(expectedResult, result);
131 public void testGetJsonRequestBodyException() {
132 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
135 ServletInputStream input;
138 public ServletInputStream getInputStream() throws JSONException {
139 throw new JSONException();
142 JSONObject result = RequestUtil.getJsonRequestBody(context);
143 JSONObject expectedResult = null;
144 assertEquals(expectedResult, result);
147 @SuppressWarnings("rawtypes")
149 public void testGetAllJsonRequestBodyRequestBodyIsNull() {
150 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
153 ServletInputStream input;
156 public ServletInputStream getInputStream() throws IOException {
161 public Enumeration getHeaderNames() {
162 return new Enumeration() {
164 List<String> a = Arrays.asList(new String[] { "1", "2" });
167 public boolean hasMoreElements() {
172 public Object nextElement() {
180 new MockUp<RequestUtil>() {
183 public JSONObject getJsonRequestBody(HttpServletRequest context) {
187 JSONObject result = RequestUtil.getAllJsonRequestBody(context);
188 JSONObject expectedResult = new JSONObject();
189 expectedResult.put("header", new HashMap<String, String>());
190 assertEquals(expectedResult, result);
193 @SuppressWarnings("rawtypes")
195 public void testGetContextHeader() {
196 HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {
199 public String getHeader(String name) {
204 public Enumeration getHeaderNames() {
205 return new Enumeration() {
207 List<String> a = Arrays.asList(new String[] { "1", "2" });
212 public boolean hasMoreElements() {
221 public Object nextElement() {
229 new MockUp<RequestUtil>() {
232 public JSONObject getJsonRequestBody(HttpServletRequest context) {
236 JSONObject result = RequestUtil.getAllJsonRequestBody(context);
237 JSONObject expectedResult = new JSONObject();
238 Map<String, String> map = new HashMap<String, String>();
240 expectedResult.put("header", map);
241 assertEquals(expectedResult, result);
244 public void testPrivateConstructor() throws Exception {
245 Constructor constructor = RequestUtil.class.getDeclaredConstructor();
246 assertTrue("Constructor is private", Modifier.isPrivate(constructor.getModifiers()));
248 constructor.setAccessible(true);
249 constructor.newInstance();