b0bcac43997c8b04d6d58d375982998c74fb0259
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.apps.uservice.test.adapt.restserver;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.util.Map;
30 import java.util.Random;
31
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;
36
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;
42
43 import com.google.gson.Gson;
44
45
46 public class TestRESTServer {
47     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
48     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
49
50     private final PrintStream stdout = System.out;
51     private final PrintStream stderr = System.err;
52
53     private static int eventsSent = 0;
54
55     @Test
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);
59
60         final Client client = ClientBuilder.newClient();
61
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()));
65
66             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
67             final String responseString = response.readEntity(String.class);
68
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"));
73         }
74
75         apexMain.shutdown();
76     }
77
78     @Test
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);
82
83         final Client client = ClientBuilder.newClient();
84
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()));
88
89             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
90             final String responseString = response.readEntity(String.class);
91
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"));
96         }
97
98         apexMain.shutdown();
99     }
100
101     @Test
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);
105
106         final Client client = ClientBuilder.newClient();
107
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());
116         }
117
118         final Response statResponse =
119                 client.target("http://localhost:23324/apex/FirstConsumer/Status").request("application/json").get();
120
121         assertEquals(Response.Status.OK.getStatusCode(), statResponse.getStatus());
122         final String responseString = statResponse.readEntity(String.class);
123
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"));
130
131         apexMain.shutdown();
132     }
133
134     @Test
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);
138
139         final Client client = ClientBuilder.newClient();
140
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()));
144
145             assertEquals(Response.Status.OK.getStatusCode(), firstResponse.getStatus());
146             final String firstResponseString = firstResponse.readEntity(String.class);
147
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"));
152
153             final Response secondResponse = client.target("http://localhost:23324/apex/SecondConsumer/EventIn")
154                     .request("application/json").post(Entity.json(getEvent()));
155
156             assertEquals(Response.Status.OK.getStatusCode(), secondResponse.getStatus());
157             final String secondResponseString = secondResponse.readEntity(String.class);
158
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"));
163         }
164
165         apexMain.shutdown();
166     }
167
168     @Test
169     public void testRESTServerProducerStandalone() throws MessagingException, ApexException, IOException {
170         System.setOut(new PrintStream(outContent));
171         System.setErr(new PrintStream(errContent));
172
173         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json"};
174
175         final ApexMain apexMain = new ApexMain(args);
176         ThreadUtilities.sleep(200);
177         apexMain.shutdown();
178
179         final String outString = outContent.toString();
180
181         System.setOut(stdout);
182         System.setErr(stderr);
183
184         assertTrue(outString
185                 .contains("the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer"));
186     }
187
188     @Test
189     public void testRESTServerProducerHost() throws MessagingException, ApexException, IOException {
190         System.setOut(new PrintStream(outContent));
191         System.setErr(new PrintStream(errContent));
192
193         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerHost.json"};
194
195         final ApexMain apexMain = new ApexMain(args);
196         ThreadUtilities.sleep(200);
197         apexMain.shutdown();
198
199         final String outString = outContent.toString();
200
201         System.setOut(stdout);
202         System.setErr(stderr);
203
204         assertTrue(outString.contains("  host and port are specified only in standalone mode"));
205     }
206
207     @Test
208     public void testRESTServerProducerPort() throws MessagingException, ApexException, IOException {
209         System.setOut(new PrintStream(outContent));
210         System.setErr(new PrintStream(errContent));
211
212         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerPort.json"};
213
214         final ApexMain apexMain = new ApexMain(args);
215         ThreadUtilities.sleep(200);
216         apexMain.shutdown();
217
218         final String outString = outContent.toString();
219
220         System.setOut(stdout);
221         System.setErr(stderr);
222
223         assertTrue(outString.contains("  host and port are specified only in standalone mode"));
224     }
225
226     @Test
227     public void testRESTServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
228         System.setOut(new PrintStream(outContent));
229         System.setErr(new PrintStream(errContent));
230
231         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json"};
232
233         final ApexMain apexMain = new ApexMain(args);
234         ThreadUtilities.sleep(200);
235         apexMain.shutdown();
236
237         final String outString = outContent.toString();
238
239         System.setOut(stdout);
240         System.setErr(stderr);
241
242         assertTrue(outString.contains(
243                 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
244     }
245
246     @Test
247     public void testRESTServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
248         System.setOut(new PrintStream(outContent));
249         System.setErr(new PrintStream(errContent));
250
251         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json"};
252
253         final ApexMain apexMain = new ApexMain(args);
254         ThreadUtilities.sleep(200);
255         apexMain.shutdown();
256
257         final String outString = outContent.toString();
258
259         System.setOut(stdout);
260         System.setErr(stderr);
261
262         assertTrue(outString.contains(
263                 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
264     }
265
266     @Test
267     public void testRESTServerProducerNotSync() throws MessagingException, ApexException, IOException {
268         System.setOut(new PrintStream(outContent));
269         System.setErr(new PrintStream(errContent));
270
271         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json"};
272
273         final ApexMain apexMain = new ApexMain(args);
274         ThreadUtilities.sleep(200);
275         apexMain.shutdown();
276
277         final String outString = outContent.toString();
278
279         System.setOut(stdout);
280         System.setErr(stderr);
281
282         assertTrue(outString.contains(
283                 "REST Server producer (FirstProducer) must run in synchronous mode with a REST Server consumer"));
284     }
285
286     @Test
287     public void testRESTServerConsumerNotSync() throws MessagingException, ApexException, IOException {
288         System.setOut(new PrintStream(outContent));
289         System.setErr(new PrintStream(errContent));
290
291         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json"};
292
293         final ApexMain apexMain = new ApexMain(args);
294         ThreadUtilities.sleep(200);
295         apexMain.shutdown();
296
297         final String outString = outContent.toString();
298
299         System.setOut(stdout);
300         System.setErr(stderr);
301
302         assertTrue(outString.contains(
303                 "event output for peered mode \"SYNCHRONOUS\": peer \"FirstConsumer\" for event handler \"FirstProducer\" does not exist or is not defined as being synchronous"));
304     }
305
306     @Test
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);
310
311         final Client client = ClientBuilder.newClient();
312
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()));
316
317             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
318             final String responseString = response.readEntity(String.class);
319
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"));
325         }
326
327         apexMain.shutdown();
328     }
329
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";
334
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" + "}";
340
341         return eventString;
342     }
343 }