5ac7146736792380ba671b80f2b13e55c4f90b49
[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         // 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);
65
66             final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
67                     .request("application/json").put(Entity.json(getEvent()));
68
69             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
70             final String responseString = response.readEntity(String.class);
71
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"));
76         }
77
78         apexMain.shutdown();
79     }
80
81     @Test
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);
85
86         final Client client = ClientBuilder.newClient();
87
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);
91
92             final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
93                     .request("application/json").post(Entity.json(getEvent()));
94
95             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
96             final String responseString = response.readEntity(String.class);
97
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"));
102         }
103
104         apexMain.shutdown();
105     }
106
107     @Test
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);
111
112         final Client client = ClientBuilder.newClient();
113
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);
117
118             final Response firstResponse = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
119                     .request("application/json").post(Entity.json(getEvent()));
120
121             assertEquals(Response.Status.OK.getStatusCode(), firstResponse.getStatus());
122             final String firstResponseString = firstResponse.readEntity(String.class);
123
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"));
128
129             final Response secondResponse = client.target("http://localhost:23324/apex/SecondConsumer/EventIn")
130                     .request("application/json").post(Entity.json(getEvent()));
131
132             assertEquals(Response.Status.OK.getStatusCode(), secondResponse.getStatus());
133             final String secondResponseString = secondResponse.readEntity(String.class);
134
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"));
139         }
140
141         apexMain.shutdown();
142     }
143
144     @Test
145     public void testRESTServerProducerStandalone() throws MessagingException, ApexException, IOException {
146         System.setOut(new PrintStream(outContent));
147         System.setErr(new PrintStream(errContent));
148
149         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerStandalone.json"};
150
151         final ApexMain apexMain = new ApexMain(args);
152         ThreadUtilities.sleep(200);
153         apexMain.shutdown();
154
155         final String outString = outContent.toString();
156
157         System.setOut(stdout);
158         System.setErr(stderr);
159
160         assertTrue(outString
161                 .contains("the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer"));
162     }
163
164     @Test
165     public void testRESTServerProducerHost() throws MessagingException, ApexException, IOException {
166         System.setOut(new PrintStream(outContent));
167         System.setErr(new PrintStream(errContent));
168
169         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerHost.json"};
170
171         final ApexMain apexMain = new ApexMain(args);
172         ThreadUtilities.sleep(200);
173         apexMain.shutdown();
174
175         final String outString = outContent.toString();
176
177         System.setOut(stdout);
178         System.setErr(stderr);
179
180         assertTrue(outString.contains("  host and port are specified only in standalone mode"));
181     }
182
183     @Test
184     public void testRESTServerProducerPort() throws MessagingException, ApexException, IOException {
185         System.setOut(new PrintStream(outContent));
186         System.setErr(new PrintStream(errContent));
187
188         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerPort.json"};
189
190         final ApexMain apexMain = new ApexMain(args);
191         ThreadUtilities.sleep(200);
192         apexMain.shutdown();
193
194         final String outString = outContent.toString();
195
196         System.setOut(stdout);
197         System.setErr(stderr);
198
199         assertTrue(outString.contains("  host and port are specified only in standalone mode"));
200     }
201
202     @Test
203     public void testRESTServerConsumerStandaloneNoHost() throws MessagingException, ApexException, IOException {
204         System.setOut(new PrintStream(outContent));
205         System.setErr(new PrintStream(errContent));
206
207         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoHost.json"};
208
209         final ApexMain apexMain = new ApexMain(args);
210         ThreadUtilities.sleep(200);
211         apexMain.shutdown();
212
213         final String outString = outContent.toString();
214
215         System.setOut(stdout);
216         System.setErr(stderr);
217
218         assertTrue(outString.contains(
219                 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
220     }
221
222     @Test
223     public void testRESTServerConsumerStandaloneNoPort() throws MessagingException, ApexException, IOException {
224         System.setOut(new PrintStream(outContent));
225         System.setErr(new PrintStream(errContent));
226
227         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerStandaloneNoPort.json"};
228
229         final ApexMain apexMain = new ApexMain(args);
230         ThreadUtilities.sleep(200);
231         apexMain.shutdown();
232
233         final String outString = outContent.toString();
234
235         System.setOut(stdout);
236         System.setErr(stderr);
237
238         assertTrue(outString.contains(
239                 "the parameters \"host\" and \"port\" must be defined for REST Server consumer (FirstConsumer) in standalone mode"));
240     }
241
242     @Test
243     public void testRESTServerProducerNotSync() throws MessagingException, ApexException, IOException {
244         System.setOut(new PrintStream(outContent));
245         System.setErr(new PrintStream(errContent));
246
247         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventProducerNotSync.json"};
248
249         final ApexMain apexMain = new ApexMain(args);
250         ThreadUtilities.sleep(200);
251         apexMain.shutdown();
252
253         final String outString = outContent.toString();
254
255         System.setOut(stdout);
256         System.setErr(stderr);
257
258         assertTrue(outString.contains(
259                 "REST Server producer (FirstProducer) must run in synchronous mode with a REST Server consumer"));
260     }
261
262     @Test
263     public void testRESTServerConsumerNotSync() throws MessagingException, ApexException, IOException {
264         System.setOut(new PrintStream(outContent));
265         System.setErr(new PrintStream(errContent));
266
267         final String[] args = {"src/test/resources/prodcons/RESTServerJsonEventConsumerNotSync.json"};
268
269         final ApexMain apexMain = new ApexMain(args);
270         ThreadUtilities.sleep(200);
271         apexMain.shutdown();
272
273         final String outString = outContent.toString();
274
275         System.setOut(stdout);
276         System.setErr(stderr);
277
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"));
280     }
281
282     @Test
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);
286
287         final Client client = ClientBuilder.newClient();
288
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);
292
293             final Response response = client.target("http://localhost:23324/apex/FirstConsumer/EventIn")
294                     .request("application/json").put(Entity.json(getEvent()));
295
296             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
297             final String responseString = response.readEntity(String.class);
298
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"));
304
305         }
306
307
308         apexMain.shutdown();
309     }
310
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";
315
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" + "}";
321
322         return eventString;
323     }
324 }