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 for (int i = 0; i < 20; i++) {
63 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
64 .request("application/json").put(Entity.json(getEvent()));
66 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
67 final String responseString = response.readEntity(String.class);
69 @SuppressWarnings("unchecked")
70 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
71 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
72 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
79 public void testRESTServerPost() throws MessagingException, ApexException, IOException {
80 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEvent.json"};
81 final ApexMain apexMain = new ApexMain(args);
83 final Client client = ClientBuilder.newClient();
85 for (int i = 0; i < 20; i++) {
86 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
87 .request("application/json").post(Entity.json(getEvent()));
89 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
90 final String responseString = response.readEntity(String.class);
92 @SuppressWarnings("unchecked")
93 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
94 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
95 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
102 public void testRESTServerGetStatus() throws MessagingException, ApexException, IOException {
103 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEvent.json"};
104 final ApexMain apexMain = new ApexMain(args);
106 final Client client = ClientBuilder.newClient();
108 // trigger 10 POST & PUT events
109 for (int i = 0; i < 10; i++) {
110 final Response postResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
111 .request("application/json").post(Entity.json(getEvent()));
112 final Response putResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
113 .request("application/json").put(Entity.json(getEvent()));
114 assertEquals(Response.Status.OK.getStatusCode(), postResponse.getStatus());
115 assertEquals(Response.Status.OK.getStatusCode(), putResponse.getStatus());
118 final Response statResponse =
119 client.target("http://localhost:23324/apex/FirstConsumer/Status").request("application/json").get();
121 assertEquals(Response.Status.OK.getStatusCode(), statResponse.getStatus());
122 final String responseString = statResponse.readEntity(String.class);
124 @SuppressWarnings("unchecked")
125 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
126 assertEquals("[FirstConsumer]", jsonMap.get("INPUTS"));
127 assertEquals(1.0, jsonMap.get("STAT"));
128 assertEquals(10.0, jsonMap.get("POST"));
129 assertEquals(10.0, jsonMap.get("PUT"));
135 public void testRESTServerMultiInputs() throws MessagingException, ApexException, IOException {
136 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventMultiIn.json"};
137 final ApexMain apexMain = new ApexMain(args);
139 final Client client = ClientBuilder.newClient();
141 for (int i = 0; i < 20; i++) {
142 final Response firstResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
143 .request("application/json").post(Entity.json(getEvent()));
145 assertEquals(Response.Status.OK.getStatusCode(), firstResponse.getStatus());
146 final String firstResponseString = firstResponse.readEntity(String.class);
148 @SuppressWarnings("unchecked")
149 final Map<String, Object> firstJsonMap = new Gson().fromJson(firstResponseString, Map.class);
150 assertEquals("org.onap.policy.apex.sample.events", firstJsonMap.get("nameSpace"));
151 assertEquals("Test slogan for External Event0", firstJsonMap.get("TestSlogan"));
153 final Response secondResponse = client.target("http://localhost:23324/apex/SecondConsumer/EventIn")
154 .request("application/json").post(Entity.json(getEvent()));
156 assertEquals(Response.Status.OK.getStatusCode(), secondResponse.getStatus());
157 final String secondResponseString = secondResponse.readEntity(String.class);
159 @SuppressWarnings("unchecked")
160 final Map<String, Object> secondJsonMap = new Gson().fromJson(secondResponseString, Map.class);
161 assertEquals("org.onap.policy.apex.sample.events", secondJsonMap.get("nameSpace"));
162 assertEquals("Test slogan for External Event0", secondJsonMap.get("TestSlogan"));
169 public void testRESTServerProducerStandalone() throws MessagingException, ApexException, IOException {
170 System.setOut(new PrintStream(outContent));
171 System.setErr(new PrintStream(errContent));
173 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json"};
175 final ApexMain apexMain = new ApexMain(args);
176 ThreadUtilities.sleep(200);
179 final String outString = outContent.toString();
181 System.setOut(stdout);
182 System.setErr(stderr);
185 .contains("the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer"));
189 public void testRESTServerProducerHost() throws MessagingException, ApexException, IOException {
190 System.setOut(new PrintStream(outContent));
191 System.setErr(new PrintStream(errContent));
193 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerHost.json"};
195 final ApexMain apexMain = new ApexMain(args);
196 ThreadUtilities.sleep(200);
199 final String outString = outContent.toString();
201 System.setOut(stdout);
202 System.setErr(stderr);
204 assertTrue(outString.contains(" host is specified only in standalone mode"));
208 public void testRESTServerProducerPort() throws MessagingException, ApexException, IOException {
209 System.setOut(new PrintStream(outContent));
210 System.setErr(new PrintStream(errContent));
212 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerPort.json"};
214 final ApexMain apexMain = new ApexMain(args);
215 ThreadUtilities.sleep(200);
218 final String outString = outContent.toString();
220 System.setOut(stdout);
221 System.setErr(stderr);
223 assertTrue(outString.contains(" port is specified only in standalone mode"));
227 public void testRESTServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
228 System.setOut(new PrintStream(outContent));
229 System.setErr(new PrintStream(errContent));
231 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json"};
233 final ApexMain apexMain = new ApexMain(args);
234 ThreadUtilities.sleep(200);
237 final String outString = outContent.toString();
239 System.setOut(stdout);
240 System.setErr(stderr);
242 assertTrue(outString.contains(
243 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
247 public void testRESTServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
248 System.setOut(new PrintStream(outContent));
249 System.setErr(new PrintStream(errContent));
251 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json"};
253 final ApexMain apexMain = new ApexMain(args);
254 ThreadUtilities.sleep(200);
257 final String outString = outContent.toString();
259 System.setOut(stdout);
260 System.setErr(stderr);
262 assertTrue(outString.contains(
263 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
267 public void testRESTServerProducerNotSync() throws MessagingException, ApexException, IOException {
268 System.setOut(new PrintStream(outContent));
269 System.setErr(new PrintStream(errContent));
271 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json"};
273 final ApexMain apexMain = new ApexMain(args);
274 ThreadUtilities.sleep(200);
277 final String outString = outContent.toString();
279 System.setOut(stdout);
280 System.setErr(stderr);
282 assertTrue(outString.contains(
283 "REST Server producer (FirstProducer) must run in synchronous mode with a REST Server consumer"));
287 public void testRESTServerConsumerNotSync() throws MessagingException, ApexException, IOException {
288 System.setOut(new PrintStream(outContent));
289 System.setErr(new PrintStream(errContent));
291 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json"};
293 final ApexMain apexMain = new ApexMain(args);
294 ThreadUtilities.sleep(200);
297 final String outString = outContent.toString();
299 System.setOut(stdout);
300 System.setErr(stderr);
302 assertTrue(outString.contains(
303 "peer \"FirstConsumer for peered mode SYNCHRONOUS does not exist or is not defined with the same peered mode"));
307 public void testRESTServerDivideByZero() throws MessagingException, ApexException, IOException {
308 final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventDivideByZero.json"};
309 final ApexMain apexMain = new ApexMain(args);
311 final Client client = ClientBuilder.newClient();
313 for (int i = 0; i < 20; i++) {
314 final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
315 .request("application/json").put(Entity.json(getEvent()));
317 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
318 final String responseString = response.readEntity(String.class);
320 @SuppressWarnings("unchecked")
321 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
322 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
323 assertEquals("Test slogan for External Event0", jsonMap.get("TestSlogan"));
324 assertTrue(((String) jsonMap.get("exceptionMessage")).contains("caused by: / by zero"));
330 private String getEvent() {
331 final Random rand = new Random();
332 final int nextMatchCase = rand.nextInt(4);
333 final String nextEventName = "Event0" + rand.nextInt(2) + "00";
335 final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
336 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + eventsSent++ + "\",\n"
337 + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
338 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
339 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";