Update for SNI Chcecking
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restrequestor / src / test / java / org / onap / policy / apex / plugins / event / carrier / restrequestor / RestRequestorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020,2023 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertEquals;
28
29 import com.google.gson.Gson;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.util.Map;
34 import java.util.concurrent.TimeUnit;
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.core.Response;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
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, false,
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      * Before test.
98      */
99     @Before
100     public void beforeTest() {
101         SupportRestRequestorEndpoint.resetCounters();
102         System.setOut(new PrintStream(outContent));
103         System.setErr(new PrintStream(errContent));
104     }
105
106     /**
107      * After test.
108      */
109     @After
110     public void afterTest() {
111         System.setOut(stdout);
112         System.setErr(stderr);
113     }
114
115     /**
116      * Test rest requestor get.
117      *
118      * @throws MessagingException the messaging exception
119      * @throws Exception an exception
120      */
121     @Test
122     public void testRestRequestorGet() throws Exception {
123         final Client client = ClientBuilder.newClient();
124
125         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGet.json"};
126         final ApexMain apexMain = new ApexMain(args);
127         await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
128
129         await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
130             .until(() -> getStatsFromServer(client, "GET") >= 50.0);
131
132         apexMain.shutdown();
133         await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
134
135         client.close();
136     }
137
138     /**
139      * Test rest requestor get empty.
140      *
141      * @throws MessagingException the messaging exception
142      * @throws ApexException the apex exception
143      * @throws IOException Signals that an I/O exception has occurred.
144      */
145     @Test
146     public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
147         final Client client = ClientBuilder.newClient();
148
149         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json"};
150         final ApexMain apexMain = new ApexMain(args);
151         await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
152
153         Response response = null;
154
155         // Wait for the required amount of events to be received or for 10 seconds
156         double getsSoFar = 0.0;
157         for (int i = 0; i < 40; i++) {
158             response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
159                 .request("application/json").get();
160
161             if (Response.Status.OK.getStatusCode() != response.getStatus()) {
162                 break;
163             }
164
165             final String responseString = response.readEntity(String.class);
166
167             @SuppressWarnings("unchecked")
168             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
169             getsSoFar = Double.parseDouble(jsonMap.get("GET").toString());
170
171             if (getsSoFar >= 50.0) {
172                 break;
173             }
174         }
175
176         apexMain.shutdown();
177         await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
178
179         client.close();
180
181         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
182     }
183
184     /**
185      * Test REST requestor put.
186      *
187      * @throws MessagingException the messaging exception
188      * @throws ApexException the apex exception
189      * @throws IOException Signals that an I/O exception has occurred.
190      */
191     @Test
192     public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
193         final Client client = ClientBuilder.newClient();
194
195         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePut.json"};
196         final ApexMain apexMain = new ApexMain(args);
197         await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
198
199         await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
200             .until(() -> getStatsFromServer(client, "PUT") >= 50.0);
201
202         apexMain.shutdown();
203         await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
204
205         client.close();
206     }
207
208     /**
209      * Test REST requestor post.
210      *
211      * @throws MessagingException the messaging exception
212      * @throws ApexException the apex exception
213      * @throws IOException Signals that an I/O exception has occurred.
214      */
215     @Test
216     public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
217         final Client client = ClientBuilder.newClient();
218
219         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePost.json"};
220         final ApexMain apexMain = new ApexMain(args);
221         await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
222
223         await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
224             .until(() -> getStatsFromServer(client, "POST") >= 50.0);
225
226         apexMain.shutdown();
227         await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
228
229         client.close();
230     }
231
232     /**
233      * Test REST requestor delete.
234      *
235      * @throws MessagingException the messaging exception
236      * @throws ApexException the apex exception
237      * @throws IOException Signals that an I/O exception has occurred.
238      */
239     @Test
240     public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
241         final Client client = ClientBuilder.newClient();
242
243         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileDelete.json"};
244         final ApexMain apexMain = new ApexMain(args);
245         await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
246
247         // Wait for the required amount of events to be received
248         await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
249             .until(() -> getStatsFromServer(client, "DELETE") >= 50.0);
250
251         apexMain.shutdown();
252         await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
253
254         client.close();
255     }
256
257     /**
258      * Test REST requestor multi inputs.
259      *
260      * @throws MessagingException the messaging exception
261      * @throws ApexException the apex exception
262      * @throws IOException Signals that an I/O exception has occurred.
263      */
264     @Test
265     public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
266         final Client client = ClientBuilder.newClient();
267
268         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json"};
269         final ApexMain apexMain = new ApexMain(args);
270         await().atMost(10, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
271
272         await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
273             .until(() -> getStatsFromServer(client, "GET") >= 8.0);
274
275         apexMain.shutdown();
276         await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
277
278         client.close();
279     }
280
281     /**
282      * Test REST requestor producer alone.
283      *
284      * @throws MessagingException the messaging exception
285      * @throws ApexException the apex exception
286      * @throws IOException Signals that an I/O exception has occurred.
287      */
288     @Test
289     public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
290
291         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"};
292
293         ApexMain apexMain = new ApexMain(args);
294         apexMain.shutdown();
295
296         final String outString = outContent.toString();
297
298         assertThat(outString).contains("REST Requestor producer (RestRequestorProducer) "
299             + "must run in peered requestor mode with a REST Requestor consumer");
300     }
301
302     /**
303      * Test REST requestor consumer alone.
304      *
305      * @throws MessagingException the messaging exception
306      * @throws ApexException the apex exception
307      * @throws IOException Signals that an I/O exception has occurred.
308      */
309     @Test
310     public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
311         final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"};
312         ApexMain apexMain = new ApexMain(args);
313         apexMain.shutdown();
314         final String outString = outContent.toString();
315         assertThat(outString).contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
316             + "does not exist or is not defined with the same peered mode");
317     }
318
319     private double getStatsFromServer(final Client client, final String statToGet) {
320         final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
321             .request("application/json").get();
322
323         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
324         final String responseString = response.readEntity(String.class);
325
326         @SuppressWarnings("unchecked")
327         final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
328         return Double.parseDouble(jsonMap.get(statToGet).toString());
329     }
330 }