0516473394f9a5944a66147e8894f278411fc441
[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.apps.uservice.test.adapt.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 TestRestRequestor {
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(TestRestRequestorEndpoint.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         TestRestRequestorEndpoint.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         // Wait for the required amount of events to be received or for 10 seconds
117         Double getsSoFar = 0.0;
118         for (int i = 0; i < 40; i++) {
119             ThreadUtilities.sleep(100);
120
121             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
122                             .request("application/json").get();
123
124             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
125             final String responseString = response.readEntity(String.class);
126
127             @SuppressWarnings("unchecked")
128             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
129             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
130
131             if (getsSoFar >= 50.0) {
132                 break;
133             }
134         }
135
136         apexMain.shutdown();
137         client.close();
138
139         assertEquals(Double.valueOf(50.0), getsSoFar);
140     }
141
142     /**
143      * Test REST requestor put.
144      *
145      * @throws MessagingException the messaging exception
146      * @throws ApexException the apex exception
147      * @throws IOException Signals that an I/O exception has occurred.
148      */
149     @Test
150     public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
151         final Client client = ClientBuilder.newClient();
152
153         final String[] args =
154             { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
155         final ApexMain apexMain = new ApexMain(args);
156
157         // Wait for the required amount of events to be received or for 10 seconds
158         Double putsSoFar = 0.0;
159         for (int i = 0; i < 40; i++) {
160             ThreadUtilities.sleep(100);
161
162             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
163                             .request("application/json").get();
164
165             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
166             final String responseString = response.readEntity(String.class);
167
168             @SuppressWarnings("unchecked")
169             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
170             putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
171
172             if (putsSoFar >= 50.0) {
173                 break;
174             }
175         }
176
177         apexMain.shutdown();
178         client.close();
179
180         assertEquals(Double.valueOf(50.0), putsSoFar);
181     }
182
183     /**
184      * Test REST requestor post.
185      *
186      * @throws MessagingException the messaging exception
187      * @throws ApexException the apex exception
188      * @throws IOException Signals that an I/O exception has occurred.
189      */
190     @Test
191     public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
192         final Client client = ClientBuilder.newClient();
193
194         final String[] args =
195             { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
196         final ApexMain apexMain = new ApexMain(args);
197
198         // Wait for the required amount of events to be received or for 10 seconds
199         Double postsSoFar = 0.0;
200         for (int i = 0; i < 40; i++) {
201             ThreadUtilities.sleep(100);
202
203             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
204                             .request("application/json").get();
205
206             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
207             final String responseString = response.readEntity(String.class);
208
209             @SuppressWarnings("unchecked")
210             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
211             postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
212
213             if (postsSoFar >= 50.0) {
214                 break;
215             }
216         }
217
218         apexMain.shutdown();
219         client.close();
220
221         assertEquals(Double.valueOf(50.0), postsSoFar);
222     }
223
224     /**
225      * Test REST requestor delete.
226      *
227      * @throws MessagingException the messaging exception
228      * @throws ApexException the apex exception
229      * @throws IOException Signals that an I/O exception has occurred.
230      */
231     @Test
232     public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
233         final Client client = ClientBuilder.newClient();
234
235         final String[] args =
236             { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
237         final ApexMain apexMain = new ApexMain(args);
238
239         // Wait for the required amount of events to be received or for 10 seconds
240         Double deletesSoFar = 0.0;
241         for (int i = 0; i < 40; i++) {
242             ThreadUtilities.sleep(100);
243
244             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
245                             .request("application/json").get();
246
247             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
248             final String responseString = response.readEntity(String.class);
249
250             @SuppressWarnings("unchecked")
251             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
252             deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
253
254             if (deletesSoFar >= 50.0) {
255                 break;
256             }
257         }
258
259         apexMain.shutdown();
260         client.close();
261
262         assertEquals(Double.valueOf(50.0), deletesSoFar);
263     }
264
265     /**
266      * Test REST requestor multi inputs.
267      *
268      * @throws MessagingException the messaging exception
269      * @throws ApexException the apex exception
270      * @throws IOException Signals that an I/O exception has occurred.
271      */
272     @Test
273     public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
274         final Client client = ClientBuilder.newClient();
275
276         final String[] args =
277             { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
278         final ApexMain apexMain = new ApexMain(args);
279
280         // Wait for the required amount of events to be received or for 10 seconds
281         Double getsSoFar = 0.0;
282         for (int i = 0; i < 40; i++) {
283             ThreadUtilities.sleep(100);
284
285             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
286                             .request("application/json").get();
287
288             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
289             final String responseString = response.readEntity(String.class);
290
291             @SuppressWarnings("unchecked")
292             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
293             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
294
295             if (getsSoFar >= 8.0) {
296                 break;
297             }
298         }
299
300         apexMain.shutdown();
301         client.close();
302
303         assertEquals(Double.valueOf(8.0), getsSoFar);
304
305         ThreadUtilities.sleep(1000);
306     }
307
308     /**
309      * Test REST requestor producer alone.
310      *
311      * @throws MessagingException the messaging exception
312      * @throws ApexException the apex exception
313      * @throws IOException Signals that an I/O exception has occurred.
314      */
315     @Test
316     public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
317         System.setOut(new PrintStream(outContent));
318         System.setErr(new PrintStream(errContent));
319
320         final String[] args =
321             { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
322
323         final ApexMain apexMain = new ApexMain(args);
324         ThreadUtilities.sleep(200);
325         apexMain.shutdown();
326
327         final String outString = outContent.toString();
328
329         System.setOut(stdout);
330         System.setErr(stderr);
331
332         assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
333                         + "must run in peered requestor mode with a REST Requestor consumer"));
334     }
335
336     /**
337      * Test REST requestor consumer alone.
338      *
339      * @throws MessagingException the messaging exception
340      * @throws ApexException the apex exception
341      * @throws IOException Signals that an I/O exception has occurred.
342      */
343     @Test
344     public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
345         System.setOut(new PrintStream(outContent));
346         System.setErr(new PrintStream(errContent));
347
348         final String[] args =
349             { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
350
351         final ApexMain apexMain = new ApexMain(args);
352         ThreadUtilities.sleep(200);
353         apexMain.shutdown();
354
355         final String outString = outContent.toString();
356
357         System.setOut(stdout);
358         System.setErr(stderr);
359
360         assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
361                         + "does not exist or is not defined with the same peered mode"));
362     }
363 }