2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.util;
28 import static org.junit.Assert.fail;
30 import java.io.BufferedReader;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.io.StringReader;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Collections;
39 import javax.servlet.ReadListener;
40 import javax.servlet.ServletInputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
44 import org.mockito.Mockito;
47 * The Class HttpServletHelper.
49 public class HttpServletHelper {
51 public static HttpServletRequest getMockHttpServletRequest() {
52 return Mockito.mock(HttpServletRequest.class);
56 * Sets the request payload.
58 * @param request the request
59 * @param mimeType the mime type
60 * @param payloadContent the payload content
62 public static void setRequestPayload(HttpServletRequest request, String mimeType,
63 String payloadContent) {
66 Mockito.when(request.getContentType()).thenReturn(mimeType);
69 final ByteArrayInputStream bais =
70 new ByteArrayInputStream(payloadContent.getBytes(StandardCharsets.UTF_8));
72 ServletInputStream servletInputStream = new ServletInputStream() {
75 public int read() throws IOException {
80 public boolean isFinished() {
85 public boolean isReady() {
90 public void setReadListener(ReadListener readListener) {
91 // TODO Auto-generated method stub
96 Mockito.when(request.getInputStream()).thenReturn(servletInputStream);
97 Mockito.when(request.getReader()).thenReturn(new BufferedReader(new StringReader(payloadContent)));
99 } catch (IOException ioe) {
100 fail(ExceptionHelper.extractStackTraceElements(5, ioe));
106 * Gets the mock http servlet response.
108 * @param printWriter the print writer
109 * @return the mock http servlet response
111 public static HttpServletResponse getMockHttpServletResponse(PrintWriter printWriter) {
112 HttpServletResponse commonResponse = Mockito.mock(HttpServletResponse.class);
115 * Use the StringWriter wrapped in a PrintWriter to redirect output stream to an in-memory
116 * buffer instead of an on-disk file.
120 Mockito.when(commonResponse.getWriter()).thenReturn(printWriter);
121 } catch (IOException ioe) {
122 fail(ExceptionHelper.extractStackTraceElements(5, ioe));
125 return commonResponse;
129 * Assign request uri.
132 * @param requestUri the request uri
134 public static void assignRequestUri(HttpServletRequest req, String requestUri) {
135 Mockito.when(req.getRequestURI()).thenReturn(requestUri);
139 * Assign request parameter name map.
142 * @param paramNameValueMap the param name value map
144 public static void assignRequestParameterNameMap(HttpServletRequest req,
145 Map<String, String> paramNameValueMap) {
146 if (paramNameValueMap != null) {
147 Mockito.when(req.getParameterNames())
148 .thenReturn(Collections.enumeration(paramNameValueMap.keySet()));
150 for (String key : paramNameValueMap.keySet()) {
151 Mockito.when(req.getParameter(key)).thenReturn(paramNameValueMap.get(key));
157 public static void assignRequestHeader(HttpServletRequest req, String headerName, String headerValue) {
158 Mockito.when(req.getHeader(headerName)).thenReturn(headerValue);