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