2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restserver;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import com.google.gson.Gson;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
32 import java.util.Random;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.core.Response;
39 import org.junit.Test;
40 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
41 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
42 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
43 import org.onap.policy.apex.service.engine.main.ApexMain;
46 * The Class TestRestServer.
48 public class TestRestServer {
49 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
50 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
52 private final PrintStream stdout = System.out;
53 private final PrintStream stderr = System.err;
55 private static int eventsSent = 0;
58 * Test rest server put.
60 * @throws MessagingException the messaging exception
61 * @throws ApexException the apex exception
62 * @throws IOException Signals that an I/O exception has occurred.
65 public void testRestServerPut() throws MessagingException, ApexException, IOException {
67 { "src/test/resources/prodcons/RESTServerJsonEvent.json" };
68 final ApexMain apexMain = new ApexMain(args);
70 final Client client = ClientBuilder.newClient();
72 for (int i = 0; i < 20; i++) {
73 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
74 .request("application/json").put(Entity.json(getEvent()));
76 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
77 final String responseString = response.readEntity(String.class);
79 @SuppressWarnings("unchecked")
80 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
81 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
82 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
89 * Test rest server post.
91 * @throws MessagingException the messaging exception
92 * @throws ApexException the apex exception
93 * @throws IOException Signals that an I/O exception has occurred.
96 public void testRestServerPost() throws MessagingException, ApexException, IOException {
98 { "src/test/resources/prodcons/RESTServerJsonEvent.json" };
99 final ApexMain apexMain = new ApexMain(args);
101 final Client client = ClientBuilder.newClient();
103 for (int i = 0; i < 20; i++) {
104 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
105 .request("application/json").post(Entity.json(getEvent()));
107 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
108 final String responseString = response.readEntity(String.class);
110 @SuppressWarnings("unchecked")
111 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
112 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
113 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
120 * Test rest server get status.
122 * @throws MessagingException the messaging exception
123 * @throws ApexException the apex exception
124 * @throws IOException Signals that an I/O exception has occurred.
127 public void testRestServerGetStatus() throws MessagingException, ApexException, IOException {
128 final String[] args =
129 { "src/test/resources/prodcons/RESTServerJsonEvent.json" };
130 final ApexMain apexMain = new ApexMain(args);
132 final Client client = ClientBuilder.newClient();
134 // trigger 10 POST & PUT events
135 for (int i = 0; i < 10; i++) {
136 final Response postResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
137 .request("application/json").post(Entity.json(getEvent()));
138 final Response putResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
139 .request("application/json").put(Entity.json(getEvent()));
140 assertEquals(Response.Status.OK.getStatusCode(), postResponse.getStatus());
141 assertEquals(Response.Status.OK.getStatusCode(), putResponse.getStatus());
144 final Response statResponse = client.target("http://localhost:23324/apex/FirstConsumer/Status")
145 .request("application/json").get();
147 assertEquals(Response.Status.OK.getStatusCode(), statResponse.getStatus());
148 final String responseString = statResponse.readEntity(String.class);
150 @SuppressWarnings("unchecked")
151 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
152 assertEquals("[FirstConsumer", ((String)jsonMap.get("INPUTS")).substring(0, 14));
153 assertEquals(1.0, jsonMap.get("STAT"));
154 assertTrue((double)jsonMap.get("POST") >= 10.0);
155 assertTrue((double)jsonMap.get("PUT") >= 10.0);
161 * Test rest server multi inputs.
163 * @throws MessagingException the messaging exception
164 * @throws ApexException the apex exception
165 * @throws IOException Signals that an I/O exception has occurred.
168 public void testRestServerMultiInputs() throws MessagingException, ApexException, IOException {
169 final String[] args =
170 { "src/test/resources/prodcons/RESTServerJsonEventMultiIn.json" };
171 final ApexMain apexMain = new ApexMain(args);
173 final Client client = ClientBuilder.newClient();
175 for (int i = 0; i < 20; i++) {
176 final Response firstResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
177 .request("application/json").post(Entity.json(getEvent()));
179 assertEquals(Response.Status.OK.getStatusCode(), firstResponse.getStatus());
180 final String firstResponseString = firstResponse.readEntity(String.class);
182 @SuppressWarnings("unchecked")
183 final Map<String, Object> firstJsonMap = new Gson().fromJson(firstResponseString, Map.class);
184 assertEquals("org.onap.policy.apex.sample.events", firstJsonMap.get("nameSpace"));
185 assertEquals("Test slogan for External Event0", firstJsonMap.get("TestSlogan"));
187 final Response secondResponse = client.target("http://localhost:23324/apex/SecondConsumer/EventIn")
188 .request("application/json").post(Entity.json(getEvent()));
190 assertEquals(Response.Status.OK.getStatusCode(), secondResponse.getStatus());
191 final String secondResponseString = secondResponse.readEntity(String.class);
193 @SuppressWarnings("unchecked")
194 final Map<String, Object> secondJsonMap = new Gson().fromJson(secondResponseString, Map.class);
195 assertEquals("org.onap.policy.apex.sample.events", secondJsonMap.get("nameSpace"));
196 assertEquals("Test slogan for External Event0", secondJsonMap.get("TestSlogan"));
203 * Test rest server producer standalone.
205 * @throws MessagingException the messaging exception
206 * @throws ApexException the apex exception
207 * @throws IOException Signals that an I/O exception has occurred.
210 public void testRestServerProducerStandalone() throws MessagingException, ApexException, IOException {
211 System.setOut(new PrintStream(outContent));
212 System.setErr(new PrintStream(errContent));
214 final String[] args =
215 { "src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json" };
217 final ApexMain apexMain = new ApexMain(args);
218 ThreadUtilities.sleep(200);
221 final String outString = outContent.toString();
223 System.setOut(stdout);
224 System.setErr(stderr);
226 assertTrue(outString.contains(
227 "the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer"));
231 * Test rest server producer host.
233 * @throws MessagingException the messaging exception
234 * @throws ApexException the apex exception
235 * @throws IOException Signals that an I/O exception has occurred.
238 public void testRestServerProducerHost() throws MessagingException, ApexException, IOException {
239 System.setOut(new PrintStream(outContent));
240 System.setErr(new PrintStream(errContent));
242 final String[] args =
243 { "src/test/resources/prodcons/RESTServerJsonEventProducerHost.json" };
245 final ApexMain apexMain = new ApexMain(args);
246 ThreadUtilities.sleep(200);
249 final String outString = outContent.toString();
251 System.setOut(stdout);
252 System.setErr(stderr);
254 assertTrue(outString.contains(" host is specified only in standalone mode"));
258 * Test rest server producer port.
260 * @throws MessagingException the messaging exception
261 * @throws ApexException the apex exception
262 * @throws IOException Signals that an I/O exception has occurred.
265 public void testRestServerProducerPort() throws MessagingException, ApexException, IOException {
266 System.setOut(new PrintStream(outContent));
267 System.setErr(new PrintStream(errContent));
269 final String[] args =
270 { "src/test/resources/prodcons/RESTServerJsonEventProducerPort.json" };
272 final ApexMain apexMain = new ApexMain(args);
273 ThreadUtilities.sleep(200);
276 final String outString = outContent.toString();
278 System.setOut(stdout);
279 System.setErr(stderr);
281 assertTrue(outString.contains(" port is specified only in standalone mode"));
285 * Test rest server consumer standalone no host.
287 * @throws MessagingException the messaging exception
288 * @throws ApexException the apex exception
289 * @throws IOException Signals that an I/O exception has occurred.
292 public void testRestServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
293 System.setOut(new PrintStream(outContent));
294 System.setErr(new PrintStream(errContent));
296 final String[] args =
297 { "src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json" };
299 final ApexMain apexMain = new ApexMain(args);
300 ThreadUtilities.sleep(200);
303 final String outString = outContent.toString();
305 System.setOut(stdout);
306 System.setErr(stderr);
308 assertTrue(outString.contains("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
309 + "(FirstConsumer) in standalone mode"));
313 * Test rest server consumer standalone no port.
315 * @throws MessagingException the messaging exception
316 * @throws ApexException the apex exception
317 * @throws IOException Signals that an I/O exception has occurred.
320 public void testRestServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
321 System.setOut(new PrintStream(outContent));
322 System.setErr(new PrintStream(errContent));
324 final String[] args =
325 { "src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json" };
327 final ApexMain apexMain = new ApexMain(args);
328 ThreadUtilities.sleep(200);
331 final String outString = outContent.toString();
333 System.setOut(stdout);
334 System.setErr(stderr);
336 assertTrue(outString.contains("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
337 + "(FirstConsumer) in standalone mode"));
341 * Test rest server producer not sync.
343 * @throws MessagingException the messaging exception
344 * @throws ApexException the apex exception
345 * @throws IOException Signals that an I/O exception has occurred.
348 public void testRestServerProducerNotSync() throws MessagingException, ApexException, IOException {
349 System.setOut(new PrintStream(outContent));
350 System.setErr(new PrintStream(errContent));
352 final String[] args =
353 { "src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json" };
355 final ApexMain apexMain = new ApexMain(args);
356 ThreadUtilities.sleep(200);
359 final String outString = outContent.toString();
361 System.setOut(stdout);
362 System.setErr(stderr);
364 assertTrue(outString.contains("REST Server producer (FirstProducer) must run in synchronous mode "
365 + "with a REST Server consumer"));
369 * Test rest server consumer not sync.
371 * @throws MessagingException the messaging exception
372 * @throws ApexException the apex exception
373 * @throws IOException Signals that an I/O exception has occurred.
376 public void testRestServerConsumerNotSync() throws MessagingException, ApexException, IOException {
377 System.setOut(new PrintStream(outContent));
378 System.setErr(new PrintStream(errContent));
380 final String[] args =
381 { "src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json" };
383 final ApexMain apexMain = new ApexMain(args);
384 ThreadUtilities.sleep(200);
387 final String outString = outContent.toString();
389 System.setOut(stdout);
390 System.setErr(stderr);
393 .contains("peer \"FirstConsumer for peered mode SYNCHRONOUS does not exist or is not defined "
394 + "with the same peered mode"));
398 * Test rest server divide by zero.
400 * @throws MessagingException the messaging exception
401 * @throws ApexException the apex exception
402 * @throws IOException Signals that an I/O exception has occurred.
405 public void testRestServerDivideByZero() throws MessagingException, ApexException, IOException {
406 final String[] args =
407 { "src/test/resources/prodcons/RESTServerJsonEventDivideByZero.json" };
408 final ApexMain apexMain = new ApexMain(args);
410 final Client client = ClientBuilder.newClient();
412 for (int i = 0; i < 20; i++) {
413 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
414 .request("application/json").put(Entity.json(getEvent()));
416 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
417 final String responseString = response.readEntity(String.class);
419 @SuppressWarnings("unchecked")
420 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
421 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
422 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
423 assertTrue(((String) jsonMap.get("exceptionMessage")).contains("caused by: / by zero"));
434 private String getEvent() {
435 final Random rand = new Random();
436 final int nextMatchCase = rand.nextInt(4);
437 final String nextEventName = "Event0" + rand.nextInt(2) + "00";
439 final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
440 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + eventsSent++
441 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
442 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": "
443 + System.currentTimeMillis() + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";