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