Adding UI extensibility
[aai/sparky-be.git] / src / test / java / org / onap / aai / 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.onap.aai.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())
98           .thenReturn(new BufferedReader(new StringReader(payloadContent)));
99
100     } catch (IOException ioe) {
101       fail(ExceptionHelper.extractStackTraceElements(5, ioe));
102     }
103
104   }
105
106   /**
107    * Gets the mock http servlet response.
108    *
109    * @param printWriter the print writer
110    * @return the mock http servlet response
111    */
112   public static HttpServletResponse getMockHttpServletResponse(PrintWriter printWriter) {
113     HttpServletResponse commonResponse = Mockito.mock(HttpServletResponse.class);
114
115     /*
116      * Use the StringWriter wrapped in a PrintWriter to redirect output stream to an in-memory
117      * buffer instead of an on-disk file.
118      */
119
120     try {
121       Mockito.when(commonResponse.getWriter()).thenReturn(printWriter);
122     } catch (IOException ioe) {
123       fail(ExceptionHelper.extractStackTraceElements(5, ioe));
124     }
125
126     return commonResponse;
127   }
128
129   /**
130    * Assign request uri.
131    *
132    * @param req the req
133    * @param requestUri the request uri
134    */
135   public static void assignRequestUri(HttpServletRequest req, String requestUri) {
136     Mockito.when(req.getRequestURI()).thenReturn(requestUri);
137   }
138
139   /**
140    * Assign request parameter name map.
141    *
142    * @param req the req
143    * @param paramNameValueMap the param name value map
144    */
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()));
150
151       for (String key : paramNameValueMap.keySet()) {
152         Mockito.when(req.getParameter(key)).thenReturn(paramNameValueMap.get(key));
153       }
154
155     }
156   }
157
158   public static void assignRequestHeader(HttpServletRequest req, String headerName,
159       String headerValue) {
160     Mockito.when(req.getHeader(headerName)).thenReturn(headerValue);
161   }
162
163 }