Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / util / HttpServletHelper.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
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
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
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=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.openecomp.sparky.util;
27
28 import static org.junit.Assert.fail;
29
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;
37 import java.util.Map;
38
39 import javax.servlet.ReadListener;
40 import javax.servlet.ServletInputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43
44 import org.mockito.Mockito;
45
46 /**
47  * The Class HttpServletHelper.
48  */
49 public class HttpServletHelper {
50
51   public static HttpServletRequest getMockHttpServletRequest() {
52     return Mockito.mock(HttpServletRequest.class);
53   }
54
55   /**
56    * Sets the request payload.
57    *
58    * @param request the request
59    * @param mimeType the mime type
60    * @param payloadContent the payload content
61    */
62   public static void setRequestPayload(HttpServletRequest request, String mimeType,
63       String payloadContent) {
64
65     try {
66       Mockito.when(request.getContentType()).thenReturn(mimeType);
67       
68       
69       final ByteArrayInputStream bais =
70           new ByteArrayInputStream(payloadContent.getBytes(StandardCharsets.UTF_8));
71
72       ServletInputStream servletInputStream = new ServletInputStream() {
73
74         @Override
75         public int read() throws IOException {
76           return bais.read();
77         }
78
79         @Override
80         public boolean isFinished() {
81           return true;
82         }
83
84         @Override
85         public boolean isReady() {
86           return true;
87         }
88
89         @Override
90         public void setReadListener(ReadListener readListener) {
91           // TODO Auto-generated method stub
92
93         }
94       };
95
96       Mockito.when(request.getInputStream()).thenReturn(servletInputStream);
97       Mockito.when(request.getReader()).thenReturn(new BufferedReader(new StringReader(payloadContent)));
98       
99     } catch (IOException ioe) {
100       fail(ExceptionHelper.extractStackTraceElements(5, ioe));
101     }
102
103   }
104
105   /**
106    * Gets the mock http servlet response.
107    *
108    * @param printWriter the print writer
109    * @return the mock http servlet response
110    */
111   public static HttpServletResponse getMockHttpServletResponse(PrintWriter printWriter) {
112     HttpServletResponse commonResponse = Mockito.mock(HttpServletResponse.class);
113
114     /*
115      * Use the StringWriter wrapped in a PrintWriter to redirect output stream to an in-memory
116      * buffer instead of an on-disk file.
117      */
118
119     try {
120       Mockito.when(commonResponse.getWriter()).thenReturn(printWriter);
121     } catch (IOException ioe) {
122       fail(ExceptionHelper.extractStackTraceElements(5, ioe));
123     }
124
125     return commonResponse;
126   }
127
128   /**
129    * Assign request uri.
130    *
131    * @param req the req
132    * @param requestUri the request uri
133    */
134   public static void assignRequestUri(HttpServletRequest req, String requestUri) {
135     Mockito.when(req.getRequestURI()).thenReturn(requestUri);
136   }
137
138   /**
139    * Assign request parameter name map.
140    *
141    * @param req the req
142    * @param paramNameValueMap the param name value map
143    */
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()));
149
150       for (String key : paramNameValueMap.keySet()) {
151         Mockito.when(req.getParameter(key)).thenReturn(paramNameValueMap.get(key));
152       }
153       
154     }
155   }
156   
157   public static void assignRequestHeader(HttpServletRequest req, String headerName, String headerValue) {
158     Mockito.when(req.getHeader(headerName)).thenReturn(headerValue);
159   }
160
161 }