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) {
80 public void setServlet(IPublicServlet s)
85 protected void testrequest(String method, String data, String expectedResponse, boolean exact) {
86 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) {
152 private void init() throws IOException{
155 initEsTestWebserver(testPort);
158 private void deinit() {
162 public static void initEsTestWebserver(int port) throws IOException {
163 initEsTestWebserver(port, "/mwtn/test");
165 public static void initEsTestWebserver(int port,String baseUri) throws IOException {
166 server = HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0);
167 httpThreadPool = Executors.newFixedThreadPool(5);
168 server.setExecutor(httpThreadPool);
169 server.createContext(baseUri, new MyHandler());
170 //server.createContext("/", new MyRootHandler());
171 server.setExecutor(null); // creates a default executor
173 System.out.println("http server started");
176 public static void stopTestWebserver() {
177 if (server != null) {
179 httpThreadPool.shutdownNow();
180 System.out.println("http server stopped" );
186 public static class MyHandler implements HttpHandler {
188 public void handle(HttpExchange t) throws IOException {
189 String method = t.getRequestMethod();
190 System.out.println(String.format("req received: %s %s" ,method,t.getRequestURI()));
191 OutputStream os = null;
193 if (method.equals(HTTPMETHOD_GET)) {
194 t.sendResponseHeaders(200, RESPONSE_GET.length());
195 os = t.getResponseBody();
196 os.write(RESPONSE_GET.getBytes());
197 } else if (method.equals(HTTPMETHOD_POST)) {
198 t.sendResponseHeaders(200, RESPONSE_POST.length());
199 os = t.getResponseBody();
200 os.write(RESPONSE_POST.getBytes());
201 } else if (method.equals(HTTPMETHOD_PUT)) {
202 t.sendResponseHeaders(200, RESPONSE_PUT.length());
203 os = t.getResponseBody();
204 os.write(RESPONSE_PUT.getBytes());
205 } else if (method.equals(HTTPMETHOD_DELETE)) {
206 t.sendResponseHeaders(200, RESPONSE_DELETE.length());
207 os = t.getResponseBody();
208 os.write(RESPONSE_DELETE.getBytes());
209 } else if (method.equals(HTTPMETHOD_OPTIONS)) {
210 t.sendResponseHeaders(200, RESPONSE_OPTIONS.length());
211 //os = t.getResponseBody();
212 //os.write(RESPONSE_OPTIONS.getBytes());
214 t.sendResponseHeaders(404, 0);
216 System.out.println("req handled successful");
218 } catch (Exception e) {
219 System.out.println(e.getMessage());