2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.test.helper;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.io.StringWriter;
35 import java.net.InetSocketAddress;
36 import java.util.Enumeration;
38 import java.util.Vector;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Executors;
42 import javax.servlet.ServletInputStream;
43 import javax.servlet.ServletOutputStream;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.junit.After;
48 import org.junit.Before;
49 import com.sun.net.httpserver.HttpExchange;
50 import com.sun.net.httpserver.HttpHandler;
51 import com.sun.net.httpserver.HttpServer;
53 public class HelpServletBase {
55 public static final String RESPONSE_GET = "This is the response get";
56 public static final String RESPONSE_POST = "This is the response post";
57 public static final String RESPONSE_PUT = "This is the response put";
58 public static final String RESPONSE_DELETE = "This is the response delete";
59 public static final String RESPONSE_OPTIONS = "This is the response options";
61 public static final String HTTPMETHOD_GET = "GET";
62 public static final String HTTPMETHOD_POST = "POST";
63 public static final String HTTPMETHOD_PUT = "PUT";
64 public static final String HTTPMETHOD_DELETE = "DELETE";
65 public static final String HTTPMETHOD_OPTIONS = "OPTIONS";
66 private IPublicServlet servlet;
67 private static HttpServer server;
68 private static ExecutorService httpThreadPool;
70 public final String HOST = "localhost";
71 protected static int testPort;
72 private final String baseUri;
73 protected static final String LR = "\n";
75 public HelpServletBase(String baseuri, int port) {
76 this.baseUri = baseuri;
80 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);
88 protected void testrequest(String uri, String method, String data, String expectedResponse, boolean exact) {
89 this.testrequest(uri, method, data, expectedResponse, exact, null);
92 protected void testrequest(String uri, String method, String data, String expectedResponse, boolean exact,
93 Map<String, String> headersToCheck) {
95 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
96 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
98 StringWriter out = new StringWriter();
99 ServletOutputStream printOut = new ServletOutputStream() {
102 public void write(int arg0) throws IOException {
106 ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes());
107 ServletInputStream inputStream = new ServletInputStream() {
109 public int read() throws IOException {
113 Vector<String> headers = new Vector<String>();
114 headers.addElement("Accept");
115 headers.add("User-Agent");
116 Enumeration<String> headerNames = headers.elements();
118 when(mockRequest.getRequestURI()).thenReturn(this.baseUri + uri);
119 when(mockRequest.getHeaderNames()).thenReturn(headerNames);
120 when(mockRequest.getHeader("Accept")).thenReturn("application/json");
121 when(mockRequest.getHeader("User-Agent")).thenReturn("Gecko abc");
122 when(mockRequest.getInputStream()).thenReturn(inputStream);
123 when(mockResponse.getOutputStream()).thenReturn(printOut);
124 System.out.println("do a " + method + " request");
125 if (method == HTTPMETHOD_GET)
126 this.servlet.doGet(mockRequest, mockResponse);
127 else if (method == HTTPMETHOD_POST)
128 this.servlet.doPost(mockRequest, mockResponse);
129 else if (method == HTTPMETHOD_PUT)
130 this.servlet.doPut(mockRequest, mockResponse);
131 else if (method == HTTPMETHOD_DELETE)
132 this.servlet.doDelete(mockRequest, mockResponse);
133 else if (method == HTTPMETHOD_OPTIONS)
134 this.servlet.doOptions(mockRequest, mockResponse);
136 fail("http request method " + method + " test not implemented");
137 } catch (Exception e) {
138 System.err.println(e.getMessage());
141 verify(mockResponse).setStatus(200);
143 assertEquals(expectedResponse, out.toString());
145 assertTrue("response not for method " + method + "correct", out.toString().contains(expectedResponse));
146 // currently unable to check extra headers
147 if (headersToCheck != null) {
153 private void init() throws IOException {
156 initEsTestWebserver(testPort);
160 private void deinit() {
164 public static void initEsTestWebserver(int port) throws IOException {
165 initEsTestWebserver(port, "/mwtn/test");
168 public static void initEsTestWebserver(int port, String baseUri) throws IOException {
169 server = HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0);
170 httpThreadPool = Executors.newFixedThreadPool(5);
171 server.setExecutor(httpThreadPool);
172 server.createContext(baseUri, new MyHandler());
173 //server.createContext("/", new MyRootHandler());
174 server.setExecutor(null); // creates a default executor
176 System.out.println("http server started");
179 public static void stopTestWebserver() {
180 if (server != null) {
182 httpThreadPool.shutdownNow();
183 System.out.println("http server stopped");
189 public static class MyHandler implements HttpHandler {
191 public void handle(HttpExchange t) throws IOException {
192 String method = t.getRequestMethod();
193 System.out.println(String.format("req received: %s %s", method, t.getRequestURI()));
194 OutputStream os = null;
196 if (method.equals(HTTPMETHOD_GET)) {
197 t.sendResponseHeaders(200, RESPONSE_GET.length());
198 os = t.getResponseBody();
199 os.write(RESPONSE_GET.getBytes());
200 } else if (method.equals(HTTPMETHOD_POST)) {
201 t.sendResponseHeaders(200, RESPONSE_POST.length());
202 os = t.getResponseBody();
203 os.write(RESPONSE_POST.getBytes());
204 } else if (method.equals(HTTPMETHOD_PUT)) {
205 t.sendResponseHeaders(200, RESPONSE_PUT.length());
206 os = t.getResponseBody();
207 os.write(RESPONSE_PUT.getBytes());
208 } else if (method.equals(HTTPMETHOD_DELETE)) {
209 t.sendResponseHeaders(200, RESPONSE_DELETE.length());
210 os = t.getResponseBody();
211 os.write(RESPONSE_DELETE.getBytes());
212 } else if (method.equals(HTTPMETHOD_OPTIONS)) {
213 t.sendResponseHeaders(200, RESPONSE_OPTIONS.length());
214 //os = t.getResponseBody();
215 //os.write(RESPONSE_OPTIONS.getBytes());
217 t.sendResponseHeaders(404, 0);
219 System.out.println("req handled successful");
221 } catch (Exception e) {
222 System.out.println(e.getMessage());