2 * Copyright (c) 2016, 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.
16 package org.openo.baseservice.util;
18 import org.junit.After;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
28 import javax.servlet.ServletInputStream;
29 import javax.servlet.http.HttpServletRequest;
31 import junit.framework.Assert;
32 import mockit.Expectations;
34 import mockit.integration.junit4.JMockit;
42 * @version SDNO 0.5 08-Jun-2016
44 @RunWith(JMockit.class)
45 public class RestUtilsTest {
48 HttpServletRequest mockHttpServletRequest;
53 * @throws java.lang.Exception
57 public static void setUpBeforeClass() throws Exception {
63 * @throws java.lang.Exception
67 public static void tearDownAfterClass() throws Exception {
73 * @throws java.lang.Exception
77 public void setUp() throws Exception {
83 * @throws java.lang.Exception
87 public void tearDown() throws Exception {
92 * {@link org.openo.baseservice.util.RestUtils#getRequestBody(javax.servlet.http.HttpServletRequest)}
98 public void testGetRequestBody() throws IOException {
99 final String dummy = "this is a dummy data to test request body";
100 final ServletInputStream inputStream = new ServletInputStream() {
102 final ByteArrayInputStream stream = new ByteArrayInputStream(dummy.getBytes());
105 public int read() throws IOException {
106 return stream.read();
114 mockHttpServletRequest.getInputStream();
115 returns(inputStream);
118 final String body = RestUtils.getRequestBody(mockHttpServletRequest);
120 Assert.assertEquals(dummy, body);
124 public void testGetRequestBodyNull() throws IOException {
125 final ServletInputStream inputStream = null;
129 mockHttpServletRequest.getInputStream();
130 returns(inputStream);
133 final String body = RestUtils.getRequestBody(mockHttpServletRequest);
135 Assert.assertEquals("", body);
139 public void testGetRequestBodyIOException() throws IOException {
140 final ServletInputStream inputStream = new ServletInputStream() {
143 public int read() throws IOException {
144 throw new IOException();
152 mockHttpServletRequest.getInputStream();
153 returns(inputStream);
156 final String body = RestUtils.getRequestBody(mockHttpServletRequest);
158 Assert.assertEquals("", body);
162 public void testGetRequestBodyCloseIOException() throws IOException {
163 final ServletInputStream inputStream = new ServletInputStream() {
165 final ByteArrayInputStream stream = new ByteArrayInputStream("dummy".getBytes());
168 public int read() throws IOException {
169 return stream.read();
173 public void close() throws IOException {
174 throw new IOException();
181 mockHttpServletRequest.getInputStream();
182 returns(inputStream);
185 final String body = RestUtils.getRequestBody(mockHttpServletRequest);
187 Assert.assertEquals("dummy", body);