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.apps.uservice.test.adapt.restserver;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
30 import java.util.Random;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.core.Response;
37 import org.junit.Test;
38 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
39 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.service.engine.main.ApexMain;
43 import com.google.gson.Gson;
46 public class TestRESTServer {
47 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
48 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
50 private final PrintStream stdout = System.out;
51 private final PrintStream stderr = System.err;
53 private static int eventsSent = 0;
56 public void testRESTServerPut() throws MessagingException, ApexException, IOException {
57 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEvent.json"};
58 final ApexMain apexMain = new ApexMain(args);
60 final Client client = ClientBuilder.newClient();
62 // Wait for the required amount of events to be received or for 10 seconds
63 for (int i = 0; i < 20; i++) {
64 ThreadUtilities.sleep(100);
66 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
67 .request("application/json").put(Entity.json(getEvent()));
69 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
70 final String responseString = response.readEntity(String.class);
72 @SuppressWarnings("unchecked")
73 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
74 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
75 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
82 public void testRESTServerPost() throws MessagingException, ApexException, IOException {
83 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEvent.json"};
84 final ApexMain apexMain = new ApexMain(args);
86 final Client client = ClientBuilder.newClient();
88 // Wait for the required amount of events to be received or for 10 seconds
89 for (int i = 0; i < 20; i++) {
90 ThreadUtilities.sleep(100);
92 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
93 .request("application/json").post(Entity.json(getEvent()));
95 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
96 final String responseString = response.readEntity(String.class);
98 @SuppressWarnings("unchecked")
99 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
100 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
101 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
108 public void testRESTServerMultiInputs() throws MessagingException, ApexException, IOException {
109 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventMultiIn.json"};
110 final ApexMain apexMain = new ApexMain(args);
112 final Client client = ClientBuilder.newClient();
114 // Wait for the required amount of events to be received or for 10 seconds
115 for (int i = 0; i < 20; i++) {
116 ThreadUtilities.sleep(100);
118 final Response firstResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
119 .request("application/json").post(Entity.json(getEvent()));
121 assertEquals(Response.Status.OK.getStatusCode(), firstResponse.getStatus());
122 final String firstResponseString = firstResponse.readEntity(String.class);
124 @SuppressWarnings("unchecked")
125 final Map<String, Object> firstJsonMap = new Gson().fromJson(firstResponseString, Map.class);
126 assertEquals("org.onap.policy.apex.sample.events", firstJsonMap.get("nameSpace"));
127 assertEquals("Test slogan for External Event0", firstJsonMap.get("TestSlogan"));
129 final Response secondResponse = client.target("http://localhost:23324/apex/SecondConsumer/EventIn")
130 .request("application/json").post(Entity.json(getEvent()));
132 assertEquals(Response.Status.OK.getStatusCode(), secondResponse.getStatus());
133 final String secondResponseString = secondResponse.readEntity(String.class);
135 @SuppressWarnings("unchecked")
136 final Map<String, Object> secondJsonMap = new Gson().fromJson(secondResponseString, Map.class);
137 assertEquals("org.onap.policy.apex.sample.events", secondJsonMap.get("nameSpace"));
138 assertEquals("Test slogan for External Event0", secondJsonMap.get("TestSlogan"));
145 public void testRESTServerProducerStandalone() throws MessagingException, ApexException, IOException {
146 System.setOut(new PrintStream(outContent));
147 System.setErr(new PrintStream(errContent));
149 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json"};
151 final ApexMain apexMain = new ApexMain(args);
152 ThreadUtilities.sleep(200);
155 final String outString = outContent.toString();
157 System.setOut(stdout);
158 System.setErr(stderr);
161 .contains("the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer"));
165 public void testRESTServerProducerHost() throws MessagingException, ApexException, IOException {
166 System.setOut(new PrintStream(outContent));
167 System.setErr(new PrintStream(errContent));
169 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerHost.json"};
171 final ApexMain apexMain = new ApexMain(args);
172 ThreadUtilities.sleep(200);
175 final String outString = outContent.toString();
177 System.setOut(stdout);
178 System.setErr(stderr);
180 assertTrue(outString.contains(" host and port are specified only in standalone mode"));
184 public void testRESTServerProducerPort() throws MessagingException, ApexException, IOException {
185 System.setOut(new PrintStream(outContent));
186 System.setErr(new PrintStream(errContent));
188 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerPort.json"};
190 final ApexMain apexMain = new ApexMain(args);
191 ThreadUtilities.sleep(200);
194 final String outString = outContent.toString();
196 System.setOut(stdout);
197 System.setErr(stderr);
199 assertTrue(outString.contains(" host and port are specified only in standalone mode"));
203 public void testRESTServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
204 System.setOut(new PrintStream(outContent));
205 System.setErr(new PrintStream(errContent));
207 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json"};
209 final ApexMain apexMain = new ApexMain(args);
210 ThreadUtilities.sleep(200);
213 final String outString = outContent.toString();
215 System.setOut(stdout);
216 System.setErr(stderr);
218 assertTrue(outString.contains(
219 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
223 public void testRESTServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
224 System.setOut(new PrintStream(outContent));
225 System.setErr(new PrintStream(errContent));
227 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json"};
229 final ApexMain apexMain = new ApexMain(args);
230 ThreadUtilities.sleep(200);
233 final String outString = outContent.toString();
235 System.setOut(stdout);
236 System.setErr(stderr);
238 assertTrue(outString.contains(
239 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
243 public void testRESTServerProducerNotSync() throws MessagingException, ApexException, IOException {
244 System.setOut(new PrintStream(outContent));
245 System.setErr(new PrintStream(errContent));
247 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json"};
249 final ApexMain apexMain = new ApexMain(args);
250 ThreadUtilities.sleep(200);
253 final String outString = outContent.toString();
255 System.setOut(stdout);
256 System.setErr(stderr);
258 assertTrue(outString.contains(
259 "REST Server producer (FirstProducer) must run in synchronous mode with a REST Server consumer"));
263 public void testRESTServerConsumerNotSync() throws MessagingException, ApexException, IOException {
264 System.setOut(new PrintStream(outContent));
265 System.setErr(new PrintStream(errContent));
267 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json"};
269 final ApexMain apexMain = new ApexMain(args);
270 ThreadUtilities.sleep(200);
273 final String outString = outContent.toString();
275 System.setOut(stdout);
276 System.setErr(stderr);
278 assertTrue(outString.contains(
279 "event output for peered mode \"SYNCHRONOUS\": peer \"FirstConsumer\" for event handler \"FirstProducer\" does not exist or is not defined as being synchronous"));
283 public void testRESTServerDivideByZero() throws MessagingException, ApexException, IOException {
284 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventDivideByZero.json"};
285 final ApexMain apexMain = new ApexMain(args);
287 final Client client = ClientBuilder.newClient();
289 // Wait for the required amount of events to be received or for 10 seconds
290 for (int i = 0; i < 20; i++) {
291 ThreadUtilities.sleep(100);
293 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
294 .request("application/json").put(Entity.json(getEvent()));
296 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
297 final String responseString = response.readEntity(String.class);
299 @SuppressWarnings("unchecked")
300 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
301 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
302 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
303 assertTrue(((String) jsonMap.get("exceptionMessage")).contains("caused by: / by zero"));
311 private String getEvent() {
312 final Random rand = new Random();
313 final int nextMatchCase = rand.nextInt(4);
314 final String nextEventName = "Event0" + rand.nextInt(2) + "00";
316 final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
317 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + eventsSent++ + "\",\n"
318 + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
319 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
320 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";