424689cf4d1b69eaa0760a0569ef26527d82f121
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.gson.Gson;
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.util.Map;
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
35 import javax.ws.rs.core.Response;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
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 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
45 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
46 import org.onap.policy.common.gson.GsonMessageBodyHandler;
47 import org.onap.policy.common.utils.network.NetworkUtil;
48
49 /**
50  * The Class TestRestRequestor.
51  */
52 public class RestRequestorTest {
53     private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor";
54     private static final int PORT = 32801;
55     private static HttpServletServer server;
56
57     private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
58     private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
59
60     private final PrintStream stdout = System.out;
61     private final PrintStream stderr = System.err;
62
63     /**
64      * Sets the up.
65      *
66      * @throws Exception the exception
67      */
68     @BeforeClass
69     public static void setUp() throws Exception {
70         server = HttpServletServerFactoryInstance.getServerFactory().build(
71             null, false, null, PORT, "/TestRESTRequestor", false, false);
72
73         server.addServletClass(null, SupportRestRequestorEndpoint.class.getName());
74         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
75
76         server.start();
77
78         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
79             throw new IllegalStateException("port " + PORT + " is still not in use");
80         }
81     }
82
83     /**
84      * Tear down.
85      *
86      * @throws Exception the exception
87      */
88     @AfterClass
89     public static void tearDown() throws Exception {
90         if (server != null) {
91             server.stop();
92         }
93
94         new File("src/test/resources/events/EventsOut.json").delete();
95         new File("src/test/resources/events/EventsOutMulti0.json").delete();
96         new File("src/test/resources/events/EventsOutMulti1.json").delete();
97     }
98
99     /**
100      * Reset counters.
101      */
102     @Before
103     public void resetCounters() {
104         SupportRestRequestorEndpoint.resetCounters();
105     }
106
107     /**
108      * Test rest requestor get.
109      *
110      * @throws MessagingException the messaging exception
111      * @throws ApexException the apex exception
112      * @throws IOException Signals that an I/O exception has occurred.
113      */
114     @Test
115     public void testRestRequestorGet() throws MessagingException, ApexException, IOException {
116         final Client client = ClientBuilder.newClient();
117
118         final String[] args =
119             { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
120         final ApexMain apexMain = new ApexMain(args);
121
122         Response response = null;
123
124         // Wait for the required amount of events to be received or for 10 seconds
125         Double getsSoFar = 0.0;
126         for (int i = 0; i < 40; i++) {
127             ThreadUtilities.sleep(100);
128
129             response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
130                             .request("application/json").get();
131
132             if (Response.Status.OK.getStatusCode() != response.getStatus()) {
133                 break;
134             }
135
136             final String responseString = response.readEntity(String.class);
137
138             @SuppressWarnings("unchecked")
139             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
140             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
141
142             if (getsSoFar >= 50.0) {
143                 break;
144             }
145         }
146
147         apexMain.shutdown();
148         client.close();
149
150         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
151
152         assertEquals(Double.valueOf(50.0), getsSoFar);
153     }
154
155     /**
156      * Test rest requestor get empty.
157      *
158      * @throws MessagingException the messaging exception
159      * @throws ApexException the apex exception
160      * @throws IOException Signals that an I/O exception has occurred.
161      */
162     @Test
163     public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
164         final Client client = ClientBuilder.newClient();
165
166         final String[] args =
167             { "src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json" };
168         final ApexMain apexMain = new ApexMain(args);
169
170         Response response = null;
171
172         // Wait for the required amount of events to be received or for 10 seconds
173         Double getsSoFar = 0.0;
174         for (int i = 0; i < 40; i++) {
175             ThreadUtilities.sleep(100);
176
177             response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
178                             .request("application/json").get();
179
180             if (Response.Status.OK.getStatusCode() != response.getStatus()) {
181                 break;
182             }
183
184             final String responseString = response.readEntity(String.class);
185
186             @SuppressWarnings("unchecked")
187             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
188             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
189
190             if (getsSoFar >= 50.0) {
191                 break;
192             }
193         }
194
195         apexMain.shutdown();
196         client.close();
197
198         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
199     }
200
201     /**
202      * Test REST requestor put.
203      *
204      * @throws MessagingException the messaging exception
205      * @throws ApexException the apex exception
206      * @throws IOException Signals that an I/O exception has occurred.
207      */
208     @Test
209     public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
210         final Client client = ClientBuilder.newClient();
211
212         final String[] args =
213             { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
214         final ApexMain apexMain = new ApexMain(args);
215
216         // Wait for the required amount of events to be received or for 10 seconds
217         Double putsSoFar = 0.0;
218
219         Response response = null;
220         for (int i = 0; i < 40; i++) {
221             ThreadUtilities.sleep(100);
222
223             response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
224                             .request("application/json").get();
225
226             if (Response.Status.OK.getStatusCode() != response.getStatus()) {
227                 break;
228             }
229
230             final String responseString = response.readEntity(String.class);
231
232             @SuppressWarnings("unchecked")
233             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
234             putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
235
236             if (putsSoFar >= 50.0) {
237                 break;
238             }
239         }
240
241         apexMain.shutdown();
242         client.close();
243
244         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
245         assertEquals(Double.valueOf(50.0), putsSoFar);
246     }
247
248     /**
249      * Test REST requestor post.
250      *
251      * @throws MessagingException the messaging exception
252      * @throws ApexException the apex exception
253      * @throws IOException Signals that an I/O exception has occurred.
254      */
255     @Test
256     public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
257         final Client client = ClientBuilder.newClient();
258
259         final String[] args =
260             { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
261         final ApexMain apexMain = new ApexMain(args);
262
263         // Wait for the required amount of events to be received or for 10 seconds
264         Double postsSoFar = 0.0;
265         for (int i = 0; i < 40; i++) {
266             ThreadUtilities.sleep(100);
267
268             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
269                             .request("application/json").get();
270
271             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
272             final String responseString = response.readEntity(String.class);
273
274             @SuppressWarnings("unchecked")
275             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
276             postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
277
278             if (postsSoFar >= 50.0) {
279                 break;
280             }
281         }
282
283         apexMain.shutdown();
284         client.close();
285
286         assertEquals(Double.valueOf(50.0), postsSoFar);
287     }
288
289     /**
290      * Test REST requestor delete.
291      *
292      * @throws MessagingException the messaging exception
293      * @throws ApexException the apex exception
294      * @throws IOException Signals that an I/O exception has occurred.
295      */
296     @Test
297     public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
298         final Client client = ClientBuilder.newClient();
299
300         final String[] args =
301             { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
302         final ApexMain apexMain = new ApexMain(args);
303
304         // Wait for the required amount of events to be received or for 10 seconds
305         Double deletesSoFar = 0.0;
306         for (int i = 0; i < 40; i++) {
307             ThreadUtilities.sleep(100);
308
309             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
310                             .request("application/json").get();
311
312             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
313             final String responseString = response.readEntity(String.class);
314
315             @SuppressWarnings("unchecked")
316             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
317             deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
318
319             if (deletesSoFar >= 50.0) {
320                 break;
321             }
322         }
323
324         apexMain.shutdown();
325         client.close();
326
327         assertEquals(Double.valueOf(50.0), deletesSoFar);
328     }
329
330     /**
331      * Test REST requestor multi inputs.
332      *
333      * @throws MessagingException the messaging exception
334      * @throws ApexException the apex exception
335      * @throws IOException Signals that an I/O exception has occurred.
336      */
337     @Test
338     public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
339         final Client client = ClientBuilder.newClient();
340
341         final String[] args =
342             { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
343         final ApexMain apexMain = new ApexMain(args);
344
345         // Wait for the required amount of events to be received or for 10 seconds
346         Double getsSoFar = 0.0;
347         for (int i = 0; i < 40; i++) {
348             ThreadUtilities.sleep(100);
349
350             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
351                             .request("application/json").get();
352
353             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
354             final String responseString = response.readEntity(String.class);
355
356             @SuppressWarnings("unchecked")
357             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
358             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
359
360             if (getsSoFar >= 8.0) {
361                 break;
362             }
363         }
364
365         apexMain.shutdown();
366         client.close();
367
368         assertEquals(Double.valueOf(8.0), getsSoFar);
369
370         ThreadUtilities.sleep(1000);
371     }
372
373     /**
374      * Test REST requestor producer alone.
375      *
376      * @throws MessagingException the messaging exception
377      * @throws ApexException the apex exception
378      * @throws IOException Signals that an I/O exception has occurred.
379      */
380     @Test
381     public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
382         System.setOut(new PrintStream(outContent));
383         System.setErr(new PrintStream(errContent));
384
385         final String[] args =
386             { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
387
388         final ApexMain apexMain = new ApexMain(args);
389         ThreadUtilities.sleep(200);
390         apexMain.shutdown();
391
392         final String outString = outContent.toString();
393
394         System.setOut(stdout);
395         System.setErr(stderr);
396
397         assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
398                         + "must run in peered requestor mode with a REST Requestor consumer"));
399     }
400
401     /**
402      * Test REST requestor consumer alone.
403      *
404      * @throws MessagingException the messaging exception
405      * @throws ApexException the apex exception
406      * @throws IOException Signals that an I/O exception has occurred.
407      */
408     @Test
409     public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
410         System.setOut(new PrintStream(outContent));
411         System.setErr(new PrintStream(errContent));
412
413         final String[] args =
414             { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
415
416         final ApexMain apexMain = new ApexMain(args);
417         ThreadUtilities.sleep(200);
418         apexMain.shutdown();
419
420         final String outString = outContent.toString();
421
422         System.setOut(stdout);
423         System.setErr(stderr);
424
425         assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
426                         + "does not exist or is not defined with the same peered mode"));
427     }
428 }