253b790ebc851cfcbeaa417c45ad6fd557c1be50
[ccsdk/features.git] / sdnr / wt / common / src / test / java / org / onap / ccsdk / features / sdnr / wt / common / test / TestBaseHttpClient.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
23
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;
35 import java.util.Map;
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;
43
44 public class TestBaseHttpClient {
45
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;
60
61     @Test
62     public void test() {
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;
68         try {
69             response = httpClient.sendRequest(TESTURI, HTTPMETHOD_GET, null, headers);
70         } catch (IOException e) {
71             fail(e.getMessage());
72         }
73         assertNotNull(response);
74         assertEquals(RESPONSE_GET, response.body);
75         try {
76             response = httpClient.sendRequest(TESTURI, HTTPMETHOD_POST, "{}", headers);
77         } catch (IOException e) {
78             fail(e.getMessage());
79         }
80         assertNotNull(response);
81         assertTrue(response.isSuccess());
82         System.out.println(response.toString());
83         assertEquals(RESPONSE_POST, response.body);
84         try {
85             response = httpClient.sendRequest(TESTURI, HTTPMETHOD_PUT, "{}", headers);
86         } catch (IOException e) {
87             fail(e.getMessage());
88         }
89         assertNotNull(response);
90         assertEquals(RESPONSE_PUT, response.body);
91         try {
92             response = httpClient.sendRequest(TESTURI, HTTPMETHOD_DELETE, "{}", headers);
93         } catch (IOException e) {
94             fail(e.getMessage());
95         }
96         assertNotNull(response);
97         assertEquals(RESPONSE_DELETE, response.body);
98
99     }
100
101
102
103     @BeforeClass
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
111         server.start();
112         System.out.println("http server started");
113     }
114
115     @AfterClass
116     public static void stopTestWebserver() {
117         System.out.println("try to stop server");
118         if (server != null) {
119             server.stop(0);
120             httpThreadPool.shutdownNow();
121             System.out.println("http server stopped");
122         }
123     }
124
125     private class MyHttpClient extends BaseHTTPClient {
126
127         public MyHttpClient(String base, boolean trustAllCerts) {
128             super(base, trustAllCerts);
129         }
130
131         @Override
132         public BaseHTTPResponse sendRequest(String uri, String method, String body, Map<String, String> headers)
133                 throws IOException {
134             return super.sendRequest(uri, method, body, headers);
135         }
136     }
137
138     public static class MyHandler implements HttpHandler {
139
140         @Override
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;
145             try {
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());
166                 } else {
167                     t.sendResponseHeaders(404, 0);
168                 }
169                 System.out.println("req handled successful");
170
171             } catch (Exception e) {
172                 System.out.println(e.getMessage());
173             } finally {
174                 if (os != null) {
175                     os.flush();
176                     os.close();
177                 }
178             }
179         }
180     }
181 }