2 * ============LICENSE_START=======================================================
3 * ONAP : CCSDK.apps.sdnr.wt.apigateway
4 * ================================================================================
5 * Copyright (C) 2018 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.features.sdnr.wt.common.test.helper;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.io.StringWriter;
34 import java.net.InetSocketAddress;
35 import java.util.Enumeration;
37 import java.util.Vector;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
41 import javax.servlet.ServletInputStream;
42 import javax.servlet.ServletOutputStream;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
46 import org.junit.After;
47 import org.junit.Before;
48 import com.sun.net.httpserver.HttpExchange;
49 import com.sun.net.httpserver.HttpHandler;
50 import com.sun.net.httpserver.HttpServer;
52 public class HelpServletBase {
54 public static final String RESPONSE_GET = "This is the response get";
55 public static final String RESPONSE_POST = "This is the response post";
56 public static final String RESPONSE_PUT = "This is the response put";
57 public static final String RESPONSE_DELETE = "This is the response delete";
58 public static final String RESPONSE_OPTIONS = "This is the response options";
60 public static final String HTTPMETHOD_GET = "GET";
61 public static final String HTTPMETHOD_POST = "POST";
62 public static final String HTTPMETHOD_PUT = "PUT";
63 public static final String HTTPMETHOD_DELETE = "DELETE";
64 public static final String HTTPMETHOD_OPTIONS = "OPTIONS";
65 private IPublicServlet servlet;
66 private static HttpServer server;
67 private static ExecutorService httpThreadPool;
69 public final String HOST = "localhost";
70 protected static int testPort;
71 private final String baseUri;
72 protected static final String LR = "\n";
74 public HelpServletBase(String baseuri, int port) {
79 public void setServlet(IPublicServlet s)
84 protected void testrequest(String method, String data, String expectedResponse, boolean exact) {
85 this.testrequest("/mwtn/test",method, data, expectedResponse, exact, null);
87 protected void testrequest(String uri,String method, String data, String expectedResponse, boolean exact) {
88 this.testrequest(uri,method, data, expectedResponse, exact, null);
91 protected void testrequest(String uri,String method, String data, String expectedResponse, boolean exact,
92 Map<String, String> headersToCheck) {
94 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
95 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
97 StringWriter out = new StringWriter();
98 ServletOutputStream printOut = new ServletOutputStream() {
101 public void write(int arg0) throws IOException {
105 ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes());
106 ServletInputStream inputStream= new ServletInputStream() {
108 public int read() throws IOException {
112 Vector<String> headers=new Vector<String>();
113 headers.addElement("Accept");
114 headers.add("User-Agent");
115 Enumeration<String> headerNames = headers.elements();
117 when(mockRequest.getRequestURI()).thenReturn(this.baseUri+uri);
118 when(mockRequest.getHeaderNames()).thenReturn(headerNames);
119 when(mockRequest.getHeader("Accept")).thenReturn("application/json");
120 when(mockRequest.getHeader("User-Agent")).thenReturn("Gecko abc");
121 when(mockRequest.getInputStream()).thenReturn(inputStream);
122 when(mockResponse.getOutputStream()).thenReturn(printOut);
123 System.out.println("do a " + method + " request");
124 if (method == HTTPMETHOD_GET)
125 this.servlet.doGet(mockRequest, mockResponse);
126 else if (method == HTTPMETHOD_POST)
127 this.servlet.doPost(mockRequest, mockResponse);
128 else if (method == HTTPMETHOD_PUT)
129 this.servlet.doPut(mockRequest, mockResponse);
130 else if (method == HTTPMETHOD_DELETE)
131 this.servlet.doDelete(mockRequest, mockResponse);
132 else if (method == HTTPMETHOD_OPTIONS)
133 this.servlet.doOptions(mockRequest, mockResponse);
135 fail("http request method " + method + " test not implemented");
136 } catch (Exception e) {
137 System.err.println(e.getMessage());
140 verify(mockResponse).setStatus(200);
142 assertEquals(expectedResponse, out.toString());
144 assertTrue("response not for method " + method + "correct", out.toString().contains(expectedResponse));
145 // currently unable to check extra headers
146 if (headersToCheck != null) {
151 private void init() throws IOException{
154 initEsTestWebserver(testPort);
157 private void deinit() {
161 public static void initEsTestWebserver(int port) throws IOException {
162 initEsTestWebserver(port, "/mwtn/test");
164 public static void initEsTestWebserver(int port,String baseUri) throws IOException {
165 server = HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0);
166 httpThreadPool = Executors.newFixedThreadPool(5);
167 server.setExecutor(httpThreadPool);
168 server.createContext(baseUri, new MyHandler());
169 //server.createContext("/", new MyRootHandler());
170 server.setExecutor(null); // creates a default executor
172 System.out.println("http server started");
175 public static void stopTestWebserver() {
176 if (server != null) {
178 httpThreadPool.shutdownNow();
179 System.out.println("http server stopped" );
185 public static class MyHandler implements HttpHandler {
187 public void handle(HttpExchange t) throws IOException {
188 String method = t.getRequestMethod();
189 System.out.println(String.format("req received: %s %s" ,method,t.getRequestURI()));
190 OutputStream os = null;
192 if (method.equals(HTTPMETHOD_GET)) {
193 t.sendResponseHeaders(200, RESPONSE_GET.length());
194 os = t.getResponseBody();
195 os.write(RESPONSE_GET.getBytes());
196 } else if (method.equals(HTTPMETHOD_POST)) {
197 t.sendResponseHeaders(200, RESPONSE_POST.length());
198 os = t.getResponseBody();
199 os.write(RESPONSE_POST.getBytes());
200 } else if (method.equals(HTTPMETHOD_PUT)) {
201 t.sendResponseHeaders(200, RESPONSE_PUT.length());
202 os = t.getResponseBody();
203 os.write(RESPONSE_PUT.getBytes());
204 } else if (method.equals(HTTPMETHOD_DELETE)) {
205 t.sendResponseHeaders(200, RESPONSE_DELETE.length());
206 os = t.getResponseBody();
207 os.write(RESPONSE_DELETE.getBytes());
208 } else if (method.equals(HTTPMETHOD_OPTIONS)) {
209 t.sendResponseHeaders(200, RESPONSE_OPTIONS.length());
210 //os = t.getResponseBody();
211 //os.write(RESPONSE_OPTIONS.getBytes());
213 t.sendResponseHeaders(404, 0);
215 System.out.println("req handled successful");
217 } catch (Exception e) {
218 System.out.println(e.getMessage());