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.onap.aai.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())
98 .thenReturn(new BufferedReader(new StringReader(payloadContent)));
100 } catch (IOException ioe) {
101 fail(ExceptionHelper.extractStackTraceElements(5, ioe));
107 * Gets the mock http servlet response.
109 * @param printWriter the print writer
110 * @return the mock http servlet response
112 public static HttpServletResponse getMockHttpServletResponse(PrintWriter printWriter) {
113 HttpServletResponse commonResponse = Mockito.mock(HttpServletResponse.class);
116 * Use the StringWriter wrapped in a PrintWriter to redirect output stream to an in-memory
117 * buffer instead of an on-disk file.
121 Mockito.when(commonResponse.getWriter()).thenReturn(printWriter);
122 } catch (IOException ioe) {
123 fail(ExceptionHelper.extractStackTraceElements(5, ioe));
126 return commonResponse;
130 * Assign request uri.
133 * @param requestUri the request uri
135 public static void assignRequestUri(HttpServletRequest req, String requestUri) {
136 Mockito.when(req.getRequestURI()).thenReturn(requestUri);
140 * Assign request parameter name map.
143 * @param paramNameValueMap the param name value map
145 public static void assignRequestParameterNameMap(HttpServletRequest req,
146 Map<String, String> paramNameValueMap) {
147 if (paramNameValueMap != null) {
148 Mockito.when(req.getParameterNames())
149 .thenReturn(Collections.enumeration(paramNameValueMap.keySet()));
151 for (String key : paramNameValueMap.keySet()) {
152 Mockito.when(req.getParameter(key)).thenReturn(paramNameValueMap.get(key));
158 public static void assignRequestHeader(HttpServletRequest req, String headerName,
159 String headerValue) {
160 Mockito.when(req.getHeader(headerName)).thenReturn(headerValue);