2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.simulators;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
27 import java.io.BufferedReader;
28 import java.io.DataOutputStream;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.net.HttpURLConnection;
33 import java.net.URLConnection;
34 import java.nio.charset.StandardCharsets;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
40 import org.onap.policy.common.utils.network.NetworkUtil;
41 import org.onap.policy.drools.utils.logging.LoggerUtil;
43 public class DMaaPSimulatorTest {
45 private static final int DMAAPSIM_SERVER_PORT = 6670;
48 * Setup the simulator.
51 public static void setUpSimulator() {
52 LoggerUtil.setLevel("ROOT", "INFO");
53 LoggerUtil.setLevel("org.eclipse.jetty", "WARN");
55 final HttpServletServer testServer =
56 HttpServletServer.factory.build("dmaapSim", "localhost", DMAAPSIM_SERVER_PORT, "/", false, true);
57 testServer.addServletClass("/*", DMaaPSimulatorJaxRs.class.getName());
58 testServer.waitedStart(5000);
59 if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) {
60 throw new IllegalStateException("cannot connect to port " + testServer.getPort());
62 } catch (final Exception e) {
68 public static void tearDownSimulator() {
69 HttpServletServer.factory.destroy();
73 public void testGetNoData() {
75 Pair<Integer, String> response = dmaapGet("myTopicNoData", timeout);
76 assertNotNull(response);
77 assertNotNull(response.first);
78 assertEquals("No topic", response.second);
82 public void testSinglePost() {
83 String myTopic = "myTopicSinglePost";
84 String testData = "This is some test data";
85 Pair<Integer, String> response = dmaapPost(myTopic, testData);
86 assertNotNull(response);
87 assertNotNull(response.first);
88 assertNotNull(response.second);
90 response = dmaapGet(myTopic, 1000);
91 assertNotNull(response);
92 assertNotNull(response.first);
93 assertEquals(testData, response.second);
97 public void testOneTopicMultiPost() {
98 String[] data = {"data point 1", "data point 2", "something random"};
99 String myTopic = "myTopicMultiPost";
100 Pair<Integer, String> response = dmaapPost(myTopic, data[0]);
101 assertNotNull(response);
102 assertNotNull(response.first);
103 assertNotNull(response.second);
105 response = dmaapPost(myTopic, data[1]);
106 assertNotNull(response);
107 assertNotNull(response.first);
108 assertNotNull(response.second);
110 response = dmaapPost(myTopic, data[2]);
111 assertNotNull(response);
112 assertNotNull(response.first);
113 assertNotNull(response.second);
115 response = dmaapGet(myTopic, 1000);
116 assertNotNull(response);
117 assertNotNull(response.first);
118 assertEquals(data[0], response.second);
120 response = dmaapGet(myTopic, 1000);
121 assertNotNull(response);
122 assertNotNull(response.first);
123 assertEquals(data[1], response.second);
125 response = dmaapGet(myTopic, 1000);
126 assertNotNull(response);
127 assertNotNull(response.first);
128 assertEquals(data[2], response.second);
132 public void testMultiTopic() {
133 String[][] data = {{"Topic one message one", "Topic one message two"},
134 {"Topic two message one", "Topic two message two"}};
135 String[] topics = {"topic1", "topic2"};
137 Pair<Integer, String> response = dmaapPost(topics[0], data[0][0]);
138 assertNotNull(response);
139 assertNotNull(response.first);
140 assertNotNull(response.second);
142 response = dmaapGet(topics[0], 1000);
143 assertNotNull(response);
144 assertNotNull(response.first);
145 assertEquals(data[0][0], response.second);
147 response = dmaapGet(topics[1], 1000);
148 assertNotNull(response);
149 assertNotNull(response.first);
150 assertEquals("No topic", response.second);
152 response = dmaapPost(topics[1], data[1][0]);
153 assertNotNull(response);
154 assertNotNull(response.first);
155 assertNotNull(response.second);
157 response = dmaapPost(topics[1], data[1][1]);
158 assertNotNull(response);
159 assertNotNull(response.first);
160 assertNotNull(response.second);
162 response = dmaapPost(topics[0], data[0][1]);
163 assertNotNull(response);
164 assertNotNull(response.first);
165 assertNotNull(response.second);
167 response = dmaapGet(topics[1], 1000);
168 assertNotNull(response);
169 assertNotNull(response.first);
170 assertEquals(data[1][0], response.second);
172 response = dmaapGet(topics[0], 1000);
173 assertNotNull(response);
174 assertNotNull(response.first);
175 assertEquals(data[0][1], response.second);
177 response = dmaapGet(topics[1], 1000);
178 assertNotNull(response);
179 assertNotNull(response.first);
180 assertEquals(data[1][1], response.second);
182 response = dmaapGet(topics[0], 1000);
183 assertNotNull(response);
184 assertNotNull(response.first);
185 assertEquals("No Data", response.second);
189 public void testResponseCode() {
190 Pair<Integer, String> response = dmaapPost("myTopic", "myTopicData");
191 assertNotNull(response);
192 assertNotNull(response.first);
193 assertNotNull(response.second);
195 response = setStatus(503);
196 assertNotNull(response);
197 assertNotNull(response.first);
198 assertNotNull(response.second);
200 response = dmaapGet("myTopic", 500);
201 assertNotNull(response);
202 assertEquals(503, response.first.intValue());
203 assertEquals("You got response code: 503", response.second);
205 response = setStatus(202);
206 assertNotNull(response);
207 assertNotNull(response.first);
208 assertNotNull(response.second);
210 response = dmaapGet("myTopic", 500);
211 assertNotNull(response);
212 assertEquals(202, response.first.intValue());
213 assertEquals("myTopicData", response.second);
216 private static Pair<Integer, String> dmaapGet(String topic, int timeout) {
217 return dmaapGet(topic, "1", "1", timeout);
220 private static Pair<Integer, String> dmaapGet(String topic, String consumerGroup, String consumerId, int timeout) {
221 String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic + "/" + consumerGroup + "/"
222 + consumerId + "?timeout=" + timeout;
224 URLConnection conn = new URL(url).openConnection();
225 HttpURLConnection httpConn = null;
226 if (conn instanceof HttpURLConnection) {
227 httpConn = (HttpURLConnection) conn;
229 fail("connection not set up right");
231 httpConn.setRequestMethod("GET");
233 String response = "";
234 try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) {
236 while ((line = connReader.readLine()) != null) {
239 httpConn.disconnect();
240 return new Pair<Integer, String>(httpConn.getResponseCode(), response);
241 } catch (IOException e) {
242 if (e.getMessage().startsWith("Server returned HTTP response code")) {
243 System.out.println("hi");
244 BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
246 while ((line = connReader.readLine()) != null) {
249 httpConn.disconnect();
250 return new Pair<Integer, String>(httpConn.getResponseCode(), response);
252 fail("we got an exception: " + e);
255 } catch (Exception e) {
256 fail("we got an exception" + e);
262 private static Pair<Integer, String> dmaapPost(String topic, String data) {
263 String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic;
264 byte[] postData = data.getBytes(StandardCharsets.UTF_8);
266 URLConnection conn = new URL(url).openConnection();
267 HttpURLConnection httpConn = null;
268 if (conn instanceof HttpURLConnection) {
269 httpConn = (HttpURLConnection) conn;
271 fail("connection not set up right");
273 httpConn.setRequestMethod("POST");
274 httpConn.setDoOutput(true);
275 httpConn.setRequestProperty("Content-Type", "text/plain");
276 httpConn.setRequestProperty("Content-Length", "" + postData.length);
278 String response = "";
279 try (DataOutputStream connWriter = new DataOutputStream(httpConn.getOutputStream())) {
280 connWriter.write(postData);
283 try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) {
285 while ((line = connReader.readLine()) != null) {
288 httpConn.disconnect();
289 return new Pair<Integer, String>(httpConn.getResponseCode(), response);
290 } catch (IOException e) {
291 if (e.getMessage().startsWith("Server returned HTTP response code")) {
292 System.out.println("hi");
293 BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
295 while ((line = connReader.readLine()) != null) {
298 httpConn.disconnect();
299 return new Pair<Integer, String>(httpConn.getResponseCode(), response);
301 fail("we got an exception: " + e);
304 } catch (Exception e) {
305 fail("we got an exception: " + e);
310 private static Pair<Integer, String> setStatus(int status) {
311 String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/setStatus?statusCode=" + status;
313 URLConnection conn = new URL(url).openConnection();
314 HttpURLConnection httpConn = null;
315 if (conn instanceof HttpURLConnection) {
316 httpConn = (HttpURLConnection) conn;
318 fail("connection not set up right");
320 httpConn.setRequestMethod("POST");
322 String response = "";
323 try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) {
325 while ((line = connReader.readLine()) != null) {
328 httpConn.disconnect();
329 return new Pair<Integer, String>(httpConn.getResponseCode(), response);
330 } catch (IOException e) {
331 if (e.getMessage().startsWith("Server returned HTTP response code")) {
332 System.out.println("hi");
333 BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
335 while ((line = connReader.readLine()) != null) {
338 httpConn.disconnect();
339 return new Pair<Integer, String>(httpConn.getResponseCode(), response);
341 fail("we got an exception: " + e);
344 } catch (Exception e) {
345 fail("we got an exception" + e);
350 private static class Pair<A, B> {
351 public final A first;
352 public final B second;
354 public Pair(A first, B second) {
356 this.second = second;