59cc2ceeadfc8f49d4e3fd324c251988857cbaba
[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.testsuites.integration.uservice.adapt.restserver;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import com.google.gson.Gson;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.util.Map;
32 import java.util.Random;
33
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;
38
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;
44
45 /**
46  * The Class TestRestServer.
47  */
48 public class TestRestServer {
49     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
50     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
51
52     private final PrintStream stdout = System.out;
53     private final PrintStream stderr = System.err;
54
55     private static int eventsSent = 0;
56
57     /**
58      * Test rest server put.
59      *
60      * @throws MessagingException the messaging exception
61      * @throws ApexException the apex exception
62      * @throws IOException Signals that an I/O exception has occurred.
63      */
64     @Test
65     public void testRestServerPut() throws MessagingException, ApexException, IOException {
66         final String[] args =
67             { "src/test/resources/prodcons/RESTServerJsonEvent.json" };
68         final ApexMain apexMain = new ApexMain(args);
69
70         final Client client = ClientBuilder.newClient();
71
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()));
75
76             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
77             final String responseString = response.readEntity(String.class);
78
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"));
83         }
84
85         apexMain.shutdown();
86     }
87
88     /**
89      * Test rest server post.
90      *
91      * @throws MessagingException the messaging exception
92      * @throws ApexException the apex exception
93      * @throws IOException Signals that an I/O exception has occurred.
94      */
95     @Test
96     public void testRestServerPost() throws MessagingException, ApexException, IOException {
97         final String[] args =
98             { "src/test/resources/prodcons/RESTServerJsonEvent.json" };
99         final ApexMain apexMain = new ApexMain(args);
100
101         final Client client = ClientBuilder.newClient();
102
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()));
106
107             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
108             final String responseString = response.readEntity(String.class);
109
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"));
114         }
115
116         apexMain.shutdown();
117     }
118
119     /**
120      * Test rest server get status.
121      *
122      * @throws MessagingException the messaging exception
123      * @throws ApexException the apex exception
124      * @throws IOException Signals that an I/O exception has occurred.
125      */
126     @Test
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);
131
132         final Client client = ClientBuilder.newClient();
133
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());
142         }
143
144         final Response statResponse = client.target("http://localhost:23324/apex/FirstConsumer/Status")
145                         .request("application/json").get();
146
147         assertEquals(Response.Status.OK.getStatusCode(), statResponse.getStatus());
148         final String responseString = statResponse.readEntity(String.class);
149
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);
156
157         apexMain.shutdown();
158     }
159
160     /**
161      * Test rest server multi inputs.
162      *
163      * @throws MessagingException the messaging exception
164      * @throws ApexException the apex exception
165      * @throws IOException Signals that an I/O exception has occurred.
166      */
167     @Test
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);
172
173         final Client client = ClientBuilder.newClient();
174
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()));
178
179             assertEquals(Response.Status.OK.getStatusCode(), firstResponse.getStatus());
180             final String firstResponseString = firstResponse.readEntity(String.class);
181
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"));
186
187             final Response secondResponse = client.target("http://localhost:23324/apex/SecondConsumer/EventIn")
188                             .request("application/json").post(Entity.json(getEvent()));
189
190             assertEquals(Response.Status.OK.getStatusCode(), secondResponse.getStatus());
191             final String secondResponseString = secondResponse.readEntity(String.class);
192
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"));
197         }
198
199         apexMain.shutdown();
200     }
201
202     /**
203      * Test rest server producer standalone.
204      *
205      * @throws MessagingException the messaging exception
206      * @throws ApexException the apex exception
207      * @throws IOException Signals that an I/O exception has occurred.
208      */
209     @Test
210     public void testRestServerProducerStandalone() throws MessagingException, ApexException, IOException {
211         System.setOut(new PrintStream(outContent));
212         System.setErr(new PrintStream(errContent));
213
214         final String[] args =
215             { "src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json" };
216
217         final ApexMain apexMain = new ApexMain(args);
218         ThreadUtilities.sleep(200);
219         apexMain.shutdown();
220
221         final String outString = outContent.toString();
222
223         System.setOut(stdout);
224         System.setErr(stderr);
225
226         assertTrue(outString.contains(
227                         "the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer"));
228     }
229
230     /**
231      * Test rest server producer host.
232      *
233      * @throws MessagingException the messaging exception
234      * @throws ApexException the apex exception
235      * @throws IOException Signals that an I/O exception has occurred.
236      */
237     @Test
238     public void testRestServerProducerHost() throws MessagingException, ApexException, IOException {
239         System.setOut(new PrintStream(outContent));
240         System.setErr(new PrintStream(errContent));
241
242         final String[] args =
243             { "src/test/resources/prodcons/RESTServerJsonEventProducerHost.json" };
244
245         final ApexMain apexMain = new ApexMain(args);
246         ThreadUtilities.sleep(200);
247         apexMain.shutdown();
248
249         final String outString = outContent.toString();
250
251         System.setOut(stdout);
252         System.setErr(stderr);
253
254         assertTrue(outString.contains(" host is specified only in standalone mode"));
255     }
256
257     /**
258      * Test rest server producer port.
259      *
260      * @throws MessagingException the messaging exception
261      * @throws ApexException the apex exception
262      * @throws IOException Signals that an I/O exception has occurred.
263      */
264     @Test
265     public void testRestServerProducerPort() throws MessagingException, ApexException, IOException {
266         System.setOut(new PrintStream(outContent));
267         System.setErr(new PrintStream(errContent));
268
269         final String[] args =
270             { "src/test/resources/prodcons/RESTServerJsonEventProducerPort.json" };
271
272         final ApexMain apexMain = new ApexMain(args);
273         ThreadUtilities.sleep(200);
274         apexMain.shutdown();
275
276         final String outString = outContent.toString();
277
278         System.setOut(stdout);
279         System.setErr(stderr);
280
281         assertTrue(outString.contains(" port is specified only in standalone mode"));
282     }
283
284     /**
285      * Test rest server consumer standalone no host.
286      *
287      * @throws MessagingException the messaging exception
288      * @throws ApexException the apex exception
289      * @throws IOException Signals that an I/O exception has occurred.
290      */
291     @Test
292     public void testRestServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
293         System.setOut(new PrintStream(outContent));
294         System.setErr(new PrintStream(errContent));
295
296         final String[] args =
297             { "src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json" };
298
299         final ApexMain apexMain = new ApexMain(args);
300         ThreadUtilities.sleep(200);
301         apexMain.shutdown();
302
303         final String outString = outContent.toString();
304
305         System.setOut(stdout);
306         System.setErr(stderr);
307
308         assertTrue(outString.contains("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
309                         + "(FirstConsumer) in standalone mode"));
310     }
311
312     /**
313      * Test rest server consumer standalone no port.
314      *
315      * @throws MessagingException the messaging exception
316      * @throws ApexException the apex exception
317      * @throws IOException Signals that an I/O exception has occurred.
318      */
319     @Test
320     public void testRestServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
321         System.setOut(new PrintStream(outContent));
322         System.setErr(new PrintStream(errContent));
323
324         final String[] args =
325             { "src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json" };
326
327         final ApexMain apexMain = new ApexMain(args);
328         ThreadUtilities.sleep(200);
329         apexMain.shutdown();
330
331         final String outString = outContent.toString();
332
333         System.setOut(stdout);
334         System.setErr(stderr);
335
336         assertTrue(outString.contains("the parameters \"host\" and \"port\" must be defined for REST Server consumer "
337                         + "(FirstConsumer) in standalone mode"));
338     }
339
340     /**
341      * Test rest server producer not sync.
342      *
343      * @throws MessagingException the messaging exception
344      * @throws ApexException the apex exception
345      * @throws IOException Signals that an I/O exception has occurred.
346      */
347     @Test
348     public void testRestServerProducerNotSync() throws MessagingException, ApexException, IOException {
349         System.setOut(new PrintStream(outContent));
350         System.setErr(new PrintStream(errContent));
351
352         final String[] args =
353             { "src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json" };
354
355         final ApexMain apexMain = new ApexMain(args);
356         ThreadUtilities.sleep(200);
357         apexMain.shutdown();
358
359         final String outString = outContent.toString();
360
361         System.setOut(stdout);
362         System.setErr(stderr);
363
364         assertTrue(outString.contains("REST Server producer (FirstProducer) must run in synchronous mode "
365                         + "with a REST Server consumer"));
366     }
367
368     /**
369      * Test rest server consumer not sync.
370      *
371      * @throws MessagingException the messaging exception
372      * @throws ApexException the apex exception
373      * @throws IOException Signals that an I/O exception has occurred.
374      */
375     @Test
376     public void testRestServerConsumerNotSync() throws MessagingException, ApexException, IOException {
377         System.setOut(new PrintStream(outContent));
378         System.setErr(new PrintStream(errContent));
379
380         final String[] args =
381             { "src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json" };
382
383         final ApexMain apexMain = new ApexMain(args);
384         ThreadUtilities.sleep(200);
385         apexMain.shutdown();
386
387         final String outString = outContent.toString();
388
389         System.setOut(stdout);
390         System.setErr(stderr);
391
392         assertTrue(outString
393                         .contains("peer \"FirstConsumer for peered mode SYNCHRONOUS does not exist or is not defined "
394                                         + "with the same peered mode"));
395     }
396
397     /**
398      * Test rest server divide by zero.
399      *
400      * @throws MessagingException the messaging exception
401      * @throws ApexException the apex exception
402      * @throws IOException Signals that an I/O exception has occurred.
403      */
404     @Test
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);
409
410         final Client client = ClientBuilder.newClient();
411
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()));
415
416             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
417             final String responseString = response.readEntity(String.class);
418
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"));
424         }
425
426         apexMain.shutdown();
427     }
428
429     /**
430      * Gets the event.
431      *
432      * @return the event
433      */
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";
438
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" + "}";
444
445         return eventString;
446     }
447 }