Fix checkstyle for features submodules.
[policy/drools-pdp.git] / feature-simulators / src / test / java / org / onap / policy / drools / simulators / DMaaPSimulatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-simulators
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.simulators;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
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;
32 import java.net.URL;
33 import java.net.URLConnection;
34 import java.nio.charset.StandardCharsets;
35
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;
42
43 public class DMaaPSimulatorTest {
44
45     private static final int DMAAPSIM_SERVER_PORT = 6670;
46
47     /**
48      * Setup the simulator.
49      */
50     @BeforeClass
51     public static void setUpSimulator() {
52         LoggerUtil.setLevel("ROOT", "INFO");
53         LoggerUtil.setLevel("org.eclipse.jetty", "WARN");
54         try {
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());
61             }
62         } catch (final Exception e) {
63             fail(e.getMessage());
64         }
65     }
66
67     @AfterClass
68     public static void tearDownSimulator() {
69         HttpServletServer.factory.destroy();
70     }
71
72     @Test
73     public void testGetNoData() {
74         int timeout = 1000;
75         Pair<Integer, String> response = dmaapGet("myTopicNoData", timeout);
76         assertNotNull(response);
77         assertNotNull(response.first);
78         assertEquals("No topic", response.second);
79     }
80
81     @Test
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);
89
90         response = dmaapGet(myTopic, 1000);
91         assertNotNull(response);
92         assertNotNull(response.first);
93         assertEquals(testData, response.second);
94     }
95
96     @Test
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);
104
105         response = dmaapPost(myTopic, data[1]);
106         assertNotNull(response);
107         assertNotNull(response.first);
108         assertNotNull(response.second);
109
110         response = dmaapPost(myTopic, data[2]);
111         assertNotNull(response);
112         assertNotNull(response.first);
113         assertNotNull(response.second);
114
115         response = dmaapGet(myTopic, 1000);
116         assertNotNull(response);
117         assertNotNull(response.first);
118         assertEquals(data[0], response.second);
119
120         response = dmaapGet(myTopic, 1000);
121         assertNotNull(response);
122         assertNotNull(response.first);
123         assertEquals(data[1], response.second);
124
125         response = dmaapGet(myTopic, 1000);
126         assertNotNull(response);
127         assertNotNull(response.first);
128         assertEquals(data[2], response.second);
129     }
130
131     @Test
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"};
136
137         Pair<Integer, String> response = dmaapPost(topics[0], data[0][0]);
138         assertNotNull(response);
139         assertNotNull(response.first);
140         assertNotNull(response.second);
141
142         response = dmaapGet(topics[0], 1000);
143         assertNotNull(response);
144         assertNotNull(response.first);
145         assertEquals(data[0][0], response.second);
146
147         response = dmaapGet(topics[1], 1000);
148         assertNotNull(response);
149         assertNotNull(response.first);
150         assertEquals("No topic", response.second);
151
152         response = dmaapPost(topics[1], data[1][0]);
153         assertNotNull(response);
154         assertNotNull(response.first);
155         assertNotNull(response.second);
156
157         response = dmaapPost(topics[1], data[1][1]);
158         assertNotNull(response);
159         assertNotNull(response.first);
160         assertNotNull(response.second);
161
162         response = dmaapPost(topics[0], data[0][1]);
163         assertNotNull(response);
164         assertNotNull(response.first);
165         assertNotNull(response.second);
166
167         response = dmaapGet(topics[1], 1000);
168         assertNotNull(response);
169         assertNotNull(response.first);
170         assertEquals(data[1][0], response.second);
171
172         response = dmaapGet(topics[0], 1000);
173         assertNotNull(response);
174         assertNotNull(response.first);
175         assertEquals(data[0][1], response.second);
176
177         response = dmaapGet(topics[1], 1000);
178         assertNotNull(response);
179         assertNotNull(response.first);
180         assertEquals(data[1][1], response.second);
181
182         response = dmaapGet(topics[0], 1000);
183         assertNotNull(response);
184         assertNotNull(response.first);
185         assertEquals("No Data", response.second);
186     }
187
188     @Test
189     public void testResponseCode() {
190         Pair<Integer, String> response = dmaapPost("myTopic", "myTopicData");
191         assertNotNull(response);
192         assertNotNull(response.first);
193         assertNotNull(response.second);
194
195         response = setStatus(503);
196         assertNotNull(response);
197         assertNotNull(response.first);
198         assertNotNull(response.second);
199
200         response = dmaapGet("myTopic", 500);
201         assertNotNull(response);
202         assertEquals(503, response.first.intValue());
203         assertEquals("You got response code: 503", response.second);
204
205         response = setStatus(202);
206         assertNotNull(response);
207         assertNotNull(response.first);
208         assertNotNull(response.second);
209
210         response = dmaapGet("myTopic", 500);
211         assertNotNull(response);
212         assertEquals(202, response.first.intValue());
213         assertEquals("myTopicData", response.second);
214     }
215
216     private static Pair<Integer, String> dmaapGet(String topic, int timeout) {
217         return dmaapGet(topic, "1", "1", timeout);
218     }
219
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;
223         try {
224             URLConnection conn = new URL(url).openConnection();
225             HttpURLConnection httpConn = null;
226             if (conn instanceof HttpURLConnection) {
227                 httpConn = (HttpURLConnection) conn;
228             } else {
229                 fail("connection not set up right");
230             }
231             httpConn.setRequestMethod("GET");
232             httpConn.connect();
233             String response = "";
234             try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) {
235                 String line;
236                 while ((line = connReader.readLine()) != null) {
237                     response += line;
238                 }
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()));
245                     String line;
246                     while ((line = connReader.readLine()) != null) {
247                         response += line;
248                     }
249                     httpConn.disconnect();
250                     return new Pair<Integer, String>(httpConn.getResponseCode(), response);
251                 } else {
252                     fail("we got an exception: " + e);
253                 }
254             }
255         } catch (Exception e) {
256             fail("we got an exception" + e);
257         }
258
259         return null;
260     }
261
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);
265         try {
266             URLConnection conn = new URL(url).openConnection();
267             HttpURLConnection httpConn = null;
268             if (conn instanceof HttpURLConnection) {
269                 httpConn = (HttpURLConnection) conn;
270             } else {
271                 fail("connection not set up right");
272             }
273             httpConn.setRequestMethod("POST");
274             httpConn.setDoOutput(true);
275             httpConn.setRequestProperty("Content-Type", "text/plain");
276             httpConn.setRequestProperty("Content-Length", "" + postData.length);
277             httpConn.connect();
278             String response = "";
279             try (DataOutputStream connWriter = new DataOutputStream(httpConn.getOutputStream())) {
280                 connWriter.write(postData);
281                 connWriter.flush();
282             }
283             try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) {
284                 String line;
285                 while ((line = connReader.readLine()) != null) {
286                     response += line;
287                 }
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()));
294                     String line;
295                     while ((line = connReader.readLine()) != null) {
296                         response += line;
297                     }
298                     httpConn.disconnect();
299                     return new Pair<Integer, String>(httpConn.getResponseCode(), response);
300                 } else {
301                     fail("we got an exception: " + e);
302                 }
303             }
304         } catch (Exception e) {
305             fail("we got an exception: " + e);
306         }
307         return null;
308     }
309
310     private static Pair<Integer, String> setStatus(int status) {
311         String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/setStatus?statusCode=" + status;
312         try {
313             URLConnection conn = new URL(url).openConnection();
314             HttpURLConnection httpConn = null;
315             if (conn instanceof HttpURLConnection) {
316                 httpConn = (HttpURLConnection) conn;
317             } else {
318                 fail("connection not set up right");
319             }
320             httpConn.setRequestMethod("POST");
321             httpConn.connect();
322             String response = "";
323             try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) {
324                 String line;
325                 while ((line = connReader.readLine()) != null) {
326                     response += line;
327                 }
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()));
334                     String line;
335                     while ((line = connReader.readLine()) != null) {
336                         response += line;
337                     }
338                     httpConn.disconnect();
339                     return new Pair<Integer, String>(httpConn.getResponseCode(), response);
340                 } else {
341                     fail("we got an exception: " + e);
342                 }
343             }
344         } catch (Exception e) {
345             fail("we got an exception" + e);
346         }
347         return null;
348     }
349
350     private static class Pair<A, B> {
351         public final A first;
352         public final B second;
353
354         public Pair(A first, B second) {
355             this.first = first;
356             this.second = second;
357         }
358     }
359 }