2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 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;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import com.sun.net.httpserver.HttpExchange;
29 import com.sun.net.httpserver.HttpHandler;
30 import com.sun.net.httpserver.HttpServer;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.net.InetSocketAddress;
34 import java.util.HashMap;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
42 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
44 public class TestBaseHttpClient {
46 public static final String HTTPMETHOD_GET = "GET";
47 public static final String HTTPMETHOD_POST = "POST";
48 public static final String HTTPMETHOD_PUT = "PUT";
49 public static final String HTTPMETHOD_DELETE = "DELETE";
50 public static final String HTTPMETHOD_OPTIONS = "OPTIONS";
51 public static final String RESPONSE_GET = "This is the response get";
52 public static final String RESPONSE_POST = "This is the response post";
53 public static final String RESPONSE_PUT = "This is the response put";
54 public static final String RESPONSE_DELETE = "This is the response delete";
55 public static final String RESPONSE_OPTIONS = "This is the response options";
56 private static final String TESTURI = "/mwtn/test";
57 private static HttpServer server;
58 private static ExecutorService httpThreadPool;
59 private static final int testPort = 54440;
63 MyHttpClient httpClient = new MyHttpClient("http://localhost:" + testPort, true);
64 Map<String, String> headers = new HashMap<String, String>();
65 headers.put("Authorization", BaseHTTPClient.getAuthorizationHeaderValue("admin", "admin"));
66 headers.put("Content-Type", "application/json");
67 BaseHTTPResponse response = null;
69 response = httpClient.sendRequest(TESTURI, HTTPMETHOD_GET, null, headers);
70 } catch (IOException e) {
73 assertNotNull(response);
74 assertEquals(RESPONSE_GET, response.body);
76 response = httpClient.sendRequest(TESTURI, HTTPMETHOD_POST, "{}", headers);
77 } catch (IOException e) {
80 assertNotNull(response);
81 assertTrue(response.isSuccess());
82 System.out.println(response.toString());
83 assertEquals(RESPONSE_POST, response.body);
85 response = httpClient.sendRequest(TESTURI, HTTPMETHOD_PUT, "{}", headers);
86 } catch (IOException e) {
89 assertNotNull(response);
90 assertEquals(RESPONSE_PUT, response.body);
92 response = httpClient.sendRequest(TESTURI, HTTPMETHOD_DELETE, "{}", headers);
93 } catch (IOException e) {
96 assertNotNull(response);
97 assertEquals(RESPONSE_DELETE, response.body);
104 public static void initTestWebserver() throws IOException {
105 server = HttpServer.create(new InetSocketAddress("127.0.0.1", testPort), 0);
106 httpThreadPool = Executors.newFixedThreadPool(5);
107 server.setExecutor(httpThreadPool);
108 server.createContext(TESTURI, new MyHandler());
109 //server.createContext("/", new MyRootHandler());
110 server.setExecutor(null); // creates a default executor
112 System.out.println("http server started");
116 public static void stopTestWebserver() {
117 System.out.println("try to stop server");
118 if (server != null) {
120 httpThreadPool.shutdownNow();
121 System.out.println("http server stopped");
125 private class MyHttpClient extends BaseHTTPClient {
127 public MyHttpClient(String base, boolean trustAllCerts) {
128 super(base, trustAllCerts);
132 public BaseHTTPResponse sendRequest(String uri, String method, String body, Map<String, String> headers)
134 return super.sendRequest(uri, method, body, headers);
138 public static class MyHandler implements HttpHandler {
141 public void handle(HttpExchange t) throws IOException {
142 String method = t.getRequestMethod();
143 System.out.println(String.format("req received: %s %s", method, t.getRequestURI()));
144 OutputStream os = null;
146 if (method.equals(HTTPMETHOD_GET)) {
147 t.sendResponseHeaders(200, RESPONSE_GET.length());
148 os = t.getResponseBody();
149 os.write(RESPONSE_GET.getBytes());
150 } else if (method.equals(HTTPMETHOD_POST)) {
151 t.sendResponseHeaders(200, RESPONSE_POST.length());
152 os = t.getResponseBody();
153 os.write(RESPONSE_POST.getBytes());
154 } else if (method.equals(HTTPMETHOD_PUT)) {
155 t.sendResponseHeaders(200, RESPONSE_PUT.length());
156 os = t.getResponseBody();
157 os.write(RESPONSE_PUT.getBytes());
158 } else if (method.equals(HTTPMETHOD_DELETE)) {
159 t.sendResponseHeaders(200, RESPONSE_DELETE.length());
160 os = t.getResponseBody();
161 os.write(RESPONSE_DELETE.getBytes());
162 } else if (method.equals(HTTPMETHOD_OPTIONS)) {
163 t.sendResponseHeaders(200, RESPONSE_OPTIONS.length());
164 //os = t.getResponseBody();
165 //os.write(RESPONSE_OPTIONS.getBytes());
167 t.sendResponseHeaders(404, 0);
169 System.out.println("req handled successful");
171 } catch (Exception e) {
172 System.out.println(e.getMessage());