d681fd47181e291044fcdd506e3b7d0f114ff93c
[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.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
45 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
46 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
47 import org.onap.policy.apex.service.engine.main.ApexMain;
48
49 /**
50  * The Class TestFile2Rest.
51  */
52 public class TestFile2Rest {
53     private static final String BASE_URI = "http://localhost:32801/TestFile2Rest";
54     private static HttpServer server;
55
56     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
58
59     private final PrintStream stdout = System.out;
60     private final PrintStream stderr = System.err;
61
62     /**
63      * Sets the up.
64      *
65      * @throws Exception the exception
66      */
67     @BeforeClass
68     public static void setUp() throws Exception {
69         final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
70         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
71
72         while (!server.isStarted()) {
73             ThreadUtilities.sleep(50);
74         }
75     }
76
77     /**
78      * Tear down.
79      *
80      * @throws Exception the exception
81      */
82     @AfterClass
83     public static void tearDown() throws Exception {
84         server.shutdown();
85     }
86
87     /**
88      * Test file events post.
89      *
90      * @throws MessagingException the messaging exception
91      * @throws ApexException the apex exception
92      * @throws IOException Signals that an I/O exception has occurred.
93      */
94     @Test
95     public void testFileEventsPost() throws MessagingException, ApexException, IOException {
96         final Client client = ClientBuilder.newClient();
97
98         final String[] args =
99             { "src/test/resources/prodcons/File2RESTJsonEventPost.json" };
100         final ApexMain apexMain = new ApexMain(args);
101
102         // Wait for the required amount of events to be received or for 10 seconds
103         for (int i = 0; i < 100; i++) {
104             ThreadUtilities.sleep(100);
105             final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
106                             .request("application/json").get();
107
108             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
109             final String responseString = response.readEntity(String.class);
110
111             @SuppressWarnings("unchecked")
112             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
113             if ((double) jsonMap.get("POST") == 100) {
114                 break;
115             }
116         }
117
118         apexMain.shutdown();
119     }
120
121     /**
122      * Test file events put.
123      *
124      * @throws MessagingException the messaging exception
125      * @throws ApexException the apex exception
126      * @throws IOException Signals that an I/O exception has occurred.
127      */
128     @Test
129     public void testFileEventsPut() throws MessagingException, ApexException, IOException {
130         final String[] args =
131             { "src/test/resources/prodcons/File2RESTJsonEventPut.json" };
132         final ApexMain apexMain = new ApexMain(args);
133
134         final Client client = ClientBuilder.newClient();
135
136         // Wait for the required amount of events to be received or for 10 seconds
137         for (int i = 0; i < 100; i++) {
138             ThreadUtilities.sleep(100);
139             final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
140                             .request("application/json").get();
141
142             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
143             final String responseString = response.readEntity(String.class);
144
145             @SuppressWarnings("unchecked")
146             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
147             if ((double) jsonMap.get("PUT") == 100) {
148                 break;
149             }
150         }
151
152         apexMain.shutdown();
153     }
154
155     /**
156      * Test file events no url.
157      *
158      * @throws MessagingException the messaging exception
159      * @throws ApexException the apex exception
160      * @throws IOException Signals that an I/O exception has occurred.
161      */
162     @Test
163     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
164         System.setOut(new PrintStream(outContent));
165         System.setErr(new PrintStream(errContent));
166
167         final String[] args =
168             { "src/test/resources/prodcons/File2RESTJsonEventNoURL.json" };
169         final ApexMain apexMain = new ApexMain(args);
170
171         ThreadUtilities.sleep(200);
172         apexMain.shutdown();
173
174         final String outString = outContent.toString();
175
176         System.setOut(stdout);
177         System.setErr(stderr);
178
179         assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
180     }
181
182     /**
183      * Test file events bad 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 testFileEventsBadUrl() 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/File2RESTJsonEventBadURL.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         assertTrue(outString.contains(
207                         "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
208     }
209
210     /**
211      * Test file events bad http method.
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 testFileEventsBadHttpMethod() 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/File2RESTJsonEventBadHTTPMethod.json" };
224         final ApexMain apexMain = new ApexMain(args);
225
226         ThreadUtilities.sleep(200);
227         apexMain.shutdown();
228
229         final String outString = outContent.toString();
230
231         System.setOut(stdout);
232         System.setErr(stderr);
233
234         assertTrue(outString.contains(
235                         "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
236                                         + "are supproted for event sending on REST client producer"));
237     }
238
239     /**
240      * Test file events bad response.
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 testFileEventsBadResponse() 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/File2RESTJsonEventPostBadResponse.json" };
253         final ApexMain apexMain = new ApexMain(args);
254
255         ThreadUtilities.sleep(500);
256         apexMain.shutdown();
257
258         final String outString = outContent.toString();
259
260         System.setOut(stdout);
261         System.setErr(stderr);
262
263         assertTrue(outString.contains(
264                         "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\" using HTTP \"POST\" failed with status code 400"));
265     }
266 }