69bcf8706ee3e3d685464c86dcb289e87614a697
[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.testsuites.integration.uservice.adapt.restclient;
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.IOException;
30 import java.io.PrintStream;
31 import java.net.URI;
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.glassfish.grizzly.http.server.HttpServer;
39 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
40 import org.glassfish.jersey.server.ResourceConfig;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
46 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
47 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
48 import org.onap.policy.apex.service.engine.main.ApexMain;
49 import org.slf4j.ext.XLogger;
50 import org.slf4j.ext.XLoggerFactory;
51
52 /**
53  * The Class TestFile2Rest.
54  */
55 public class TestFile2Rest {
56     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestFile2Rest.class);
57
58     private static final String BASE_URI = "http://localhost:32801/TestFile2Rest";
59     private static HttpServer server;
60
61     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
62     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
63
64     private final PrintStream stdout = System.out;
65     private final PrintStream stderr = System.err;
66
67     /**
68      * Sets the up.
69      *
70      * @throws Exception the exception
71      */
72     @BeforeClass
73     public static void setUp() throws Exception {
74         final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
75         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
76
77         while (!server.isStarted()) {
78             ThreadUtilities.sleep(50);
79         }
80     }
81
82     /**
83      * Tear down.
84      *
85      * @throws Exception the exception
86      */
87     @AfterClass
88     public static void tearDown() throws Exception {
89         server.shutdown();
90     }
91
92     /**
93      * Clear relative file root environment variable.
94      */
95     @Before
96     public void clearRelativeFileRoot() {
97         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
98     }
99
100     /**
101      * Test file events post.
102      *
103      * @throws MessagingException the messaging exception
104      * @throws ApexException the apex exception
105      * @throws IOException Signals that an I/O exception has occurred.
106      */
107     @Test
108     public void testFileEventsPost() throws MessagingException, ApexException, IOException {
109         final Client client = ClientBuilder.newClient();
110
111         final String[] args =
112             { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2RESTJsonEventPost.json" };
113         final ApexMain apexMain = new ApexMain(args);
114
115         Response response = null;
116
117         // Wait for the required amount of events to be received or for 10 seconds
118         for (int i = 0; i < 100; i++) {
119             ThreadUtilities.sleep(100);
120             response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
121                             .request("application/json").get();
122
123             if (Response.Status.OK.getStatusCode() != response.getStatus()) {
124                 break;
125             }
126
127             final String responseString = response.readEntity(String.class);
128
129             @SuppressWarnings("unchecked")
130             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
131             if ((double) jsonMap.get("POST") == 100) {
132                 break;
133             }
134         }
135
136         apexMain.shutdown();
137
138         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
139     }
140
141     /**
142      * Test file events put.
143      *
144      * @throws MessagingException the messaging exception
145      * @throws ApexException the apex exception
146      * @throws IOException Signals that an I/O exception has occurred.
147      */
148     @Test
149     public void testFileEventsPut() throws MessagingException, ApexException, IOException {
150         final String[] args =
151             { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2RESTJsonEventPut.json" };
152         final ApexMain apexMain = new ApexMain(args);
153
154         final Client client = ClientBuilder.newClient();
155
156         Response response = null;
157
158         // Wait for the required amount of events to be received or for 10 seconds
159         for (int i = 0; i < 20; i++) {
160             ThreadUtilities.sleep(300);
161             response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
162                             .request("application/json").get();
163
164             if (Response.Status.OK.getStatusCode() != response.getStatus()) {
165                 break;
166             }
167
168             final String responseString = response.readEntity(String.class);
169
170             @SuppressWarnings("unchecked")
171             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
172             if ((double) jsonMap.get("PUT") == 20) {
173                 break;
174             }
175         }
176
177         apexMain.shutdown();
178
179         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
180     }
181
182     /**
183      * Test file events no url.
184      *
185      * @throws MessagingException the messaging exception
186      * @throws ApexException the apex exception
187      * @throws IOException Signals that an I/O exception has occurred.
188      */
189     @Test
190     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
191         System.setOut(new PrintStream(outContent));
192         System.setErr(new PrintStream(errContent));
193
194         final String[] args =
195             { "src/test/resources/prodcons/File2RESTJsonEventNoURL.json" };
196         final ApexMain apexMain = new ApexMain(args);
197
198         ThreadUtilities.sleep(200);
199         apexMain.shutdown();
200
201         final String outString = outContent.toString();
202
203         System.setOut(stdout);
204         System.setErr(stderr);
205
206         LOGGER.info("NoUrl-OUTSTRING=\n" + outString + "\nEnd-NoUrl");
207         assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
208     }
209
210     /**
211      * Test file events bad url.
212      *
213      * @throws MessagingException the messaging exception
214      * @throws ApexException the apex exception
215      * @throws IOException Signals that an I/O exception has occurred.
216      */
217     @Test
218     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
219         System.setOut(new PrintStream(outContent));
220         System.setErr(new PrintStream(errContent));
221
222         final String[] args =
223             { "src/test/resources/prodcons/File2RESTJsonEventBadURL.json" };
224         final ApexMain apexMain = new ApexMain(args);
225
226         ThreadUtilities.sleep(2000);
227         apexMain.shutdown();
228
229         final String outString = outContent.toString();
230
231         System.setOut(stdout);
232         System.setErr(stderr);
233
234         LOGGER.info("BadUrl-OUTSTRING=\n" + outString + "\nEnd-BadUrl");
235         assertTrue(outString.contains(
236                         "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
237     }
238
239     /**
240      * Test file events bad http method.
241      *
242      * @throws MessagingException the messaging exception
243      * @throws ApexException the apex exception
244      * @throws IOException Signals that an I/O exception has occurred.
245      */
246     @Test
247     public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
248         System.setOut(new PrintStream(outContent));
249         System.setErr(new PrintStream(errContent));
250
251         final String[] args =
252             { "src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json" };
253         final ApexMain apexMain = new ApexMain(args);
254
255         ThreadUtilities.sleep(200);
256         apexMain.shutdown();
257
258         final String outString = outContent.toString();
259
260         System.setOut(stdout);
261         System.setErr(stderr);
262
263         LOGGER.info("BadHttpMethod-OUTSTRING=\n" + outString + "\nEnd-BadHttpMethod");
264         assertTrue(outString.contains(
265                         "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
266                                         + "are supproted for event sending on REST client producer"));
267     }
268
269     /**
270      * Test file events bad response.
271      *
272      * @throws MessagingException the messaging exception
273      * @throws ApexException the apex exception
274      * @throws IOException Signals that an I/O exception has occurred.
275      */
276     @Test
277     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
278         System.setOut(new PrintStream(outContent));
279         System.setErr(new PrintStream(errContent));
280
281         final String[] args =
282             { "src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json" };
283         final ApexMain apexMain = new ApexMain(args);
284
285         ThreadUtilities.sleep(2000);
286         apexMain.shutdown();
287
288         final String outString = outContent.toString();
289
290         System.setOut(stdout);
291         System.setErr(stderr);
292
293         LOGGER.info("BadResponse-OUTSTRING=\n" + outString + "\nEnd-BadResponse");
294         assertTrue(outString.contains(
295                         "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\""
296                                         + " using HTTP \"POST\" failed with status code 400"));
297     }
298 }