workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / common-util / src / test / java / org / openo / baseservice / util / RestUtilsTest.java
1 /*
2  * Copyright 2016 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 package org.openo.baseservice.util;
17
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;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27
28 import javax.servlet.ServletInputStream;
29 import javax.servlet.http.HttpServletRequest;
30
31 import junit.framework.Assert;
32 import mockit.Expectations;
33 import mockit.Mocked;
34 import mockit.integration.junit4.JMockit;
35
36 /**
37  * <br/>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version   08-Jun-2016
43  */
44 @RunWith(JMockit.class)
45 public class RestUtilsTest {
46
47     @Mocked
48     HttpServletRequest mockHttpServletRequest;
49
50     /**
51      * <br/>
52      * 
53      * @throws java.lang.Exception
54      * @since  
55      */
56     @BeforeClass
57     public static void setUpBeforeClass() throws Exception {
58     }
59
60     /**
61      * <br/>
62      * 
63      * @throws java.lang.Exception
64      * @since  
65      */
66     @AfterClass
67     public static void tearDownAfterClass() throws Exception {
68     }
69
70     /**
71      * <br/>
72      * 
73      * @throws java.lang.Exception
74      * @since  
75      */
76     @Before
77     public void setUp() throws Exception {
78     }
79
80     /**
81      * <br/>
82      * 
83      * @throws java.lang.Exception
84      * @since  
85      */
86     @After
87     public void tearDown() throws Exception {
88     }
89
90     /**
91      * Test method for
92      * {@link org.openo.baseservice.util.RestUtils#getRequestBody(javax.servlet.http.HttpServletRequest)}
93      * .
94      * 
95      * @throws IOException
96      */
97     @Test
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() {
101
102             final ByteArrayInputStream stream = new ByteArrayInputStream(dummy.getBytes());
103
104             @Override
105             public int read() throws IOException {
106                 return stream.read();
107             }
108
109         };
110
111         new Expectations() {
112
113             {
114                 mockHttpServletRequest.getInputStream();
115                 returns(inputStream);
116             }
117         };
118         final String body = RestUtils.getRequestBody(mockHttpServletRequest);
119
120         Assert.assertEquals(dummy, body);
121     }
122
123     @Test
124     public void testGetRequestBodyNull() throws IOException {
125         final ServletInputStream inputStream = null;
126         new Expectations() {
127
128             {
129                 mockHttpServletRequest.getInputStream();
130                 returns(inputStream);
131             }
132         };
133         final String body = RestUtils.getRequestBody(mockHttpServletRequest);
134
135         Assert.assertEquals("", body);
136     }
137
138     @Test
139     public void testGetRequestBodyIOException() throws IOException {
140         final ServletInputStream inputStream = new ServletInputStream() {
141
142             @Override
143             public int read() throws IOException {
144                 throw new IOException();
145             }
146
147         };
148
149         new Expectations() {
150
151             {
152                 mockHttpServletRequest.getInputStream();
153                 returns(inputStream);
154             }
155         };
156         final String body = RestUtils.getRequestBody(mockHttpServletRequest);
157
158         Assert.assertEquals("", body);
159     }
160
161     @Test
162     public void testGetRequestBodyCloseIOException() throws IOException {
163         final ServletInputStream inputStream = new ServletInputStream() {
164
165             final ByteArrayInputStream stream = new ByteArrayInputStream("dummy".getBytes());
166
167             @Override
168             public int read() throws IOException {
169                 return stream.read();
170             }
171
172             @Override
173             public void close() throws IOException {
174                 throw new IOException();
175             }
176         };
177
178         new Expectations() {
179
180             {
181                 mockHttpServletRequest.getInputStream();
182                 returns(inputStream);
183             }
184         };
185         final String body = RestUtils.getRequestBody(mockHttpServletRequest);
186
187         Assert.assertEquals("dummy", body);
188     }
189
190 }