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