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