ec41de5fb2821033d76624f6389f2b0272c09035
[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.assertTrue;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.net.URI;
30
31 import org.glassfish.grizzly.http.server.HttpServer;
32 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
39 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.model.utilities.TextFileUtils;
42 import org.onap.policy.apex.service.engine.main.ApexMain;
43
44 /**
45  * The Class TestRest2File.
46  */
47 public class TestRest2File {
48
49     private static final String BASE_URI = "http://localhost:32801/TestRest2File";
50     private HttpServer server;
51
52     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
53     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
54
55     private final PrintStream stdout = System.out;
56     private final PrintStream stderr = System.err;
57
58     /**
59      * Sets the up.
60      *
61      * @throws Exception the exception
62      */
63     @Before
64     public void setUp() throws Exception {
65         final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
66         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
67
68         while (!server.isStarted()) {
69             ThreadUtilities.sleep(50);
70         }
71     }
72
73     /**
74      * Tear down.
75      *
76      * @throws Exception the exception
77      */
78     @After
79     public void tearDown() throws Exception {
80         server.shutdown();
81     }
82
83     /**
84      * Delete temp files.
85      */
86     @AfterClass
87     public static void deleteTempFiles() {
88         new File("src/test/resources/events/EventsOut.json").delete();
89     }
90
91     /**
92      * Test rest events in.
93      *
94      * @throws MessagingException the messaging exception
95      * @throws ApexException the apex exception
96      * @throws IOException Signals that an I/O exception has occurred.
97      */
98     @Test
99     public void testRestEventsIn() throws MessagingException, ApexException, IOException {
100         final String[] args =
101             { "src/test/resources/prodcons/REST2FileJsonEvent.json" };
102
103         final ApexMain apexMain = new ApexMain(args);
104
105         ThreadUtilities.sleep(1000);
106         apexMain.shutdown();
107
108         final String outputEventText = TextFileUtils.getTextFileAsString("src/test/resources/events/EventsOut.json");
109         assertTrue(outputEventText.contains("04\",\n" + "  \"version\": \"0.0.1\",\n"
110                         + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\""));
111     }
112
113     /**
114      * Test file empty events.
115      *
116      * @throws MessagingException the messaging exception
117      * @throws ApexException the apex exception
118      * @throws IOException Signals that an I/O exception has occurred.
119      */
120     @Test
121     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
122         System.setOut(new PrintStream(outContent));
123         System.setErr(new PrintStream(errContent));
124
125         final String[] args =
126             { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
127         final ApexMain apexMain = new ApexMain(args);
128
129         ThreadUtilities.sleep(1000);
130         apexMain.shutdown();
131
132         final String outString = outContent.toString();
133
134         System.setOut(stdout);
135         System.setErr(stderr);
136
137         assertTrue(outString.contains(
138                         "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\""));
139     }
140
141     /**
142      * Test file events no url.
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 testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
150         System.setOut(new PrintStream(outContent));
151         System.setErr(new PrintStream(errContent));
152
153         final String[] args =
154             { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
155         final ApexMain apexMain = new ApexMain(args);
156
157         ThreadUtilities.sleep(1000);
158         apexMain.shutdown();
159
160         final String outString = outContent.toString();
161
162         System.setOut(stdout);
163         System.setErr(stderr);
164
165         assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
166     }
167
168     /**
169      * Test file events bad url.
170      *
171      * @throws MessagingException the messaging exception
172      * @throws ApexException the apex exception
173      * @throws IOException Signals that an I/O exception has occurred.
174      */
175     @Test
176     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
177         System.setOut(new PrintStream(outContent));
178         System.setErr(new PrintStream(errContent));
179
180         final String[] args =
181             { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
182         final ApexMain apexMain = new ApexMain(args);
183
184         ThreadUtilities.sleep(1000);
185         apexMain.shutdown();
186
187         final String outString = outContent.toString();
188
189         System.setOut(stdout);
190         System.setErr(stderr);
191
192         assertTrue(outString.contains(
193                         "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404"));
194     }
195
196     /**
197      * Test file events bad http method.
198      *
199      * @throws MessagingException the messaging exception
200      * @throws ApexException the apex exception
201      * @throws IOException Signals that an I/O exception has occurred.
202      */
203     @Test
204     public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
205         System.setOut(new PrintStream(outContent));
206         System.setErr(new PrintStream(errContent));
207
208         final String[] args =
209             { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
210         final ApexMain apexMain = new ApexMain(args);
211
212         ThreadUtilities.sleep(1000);
213         apexMain.shutdown();
214
215         final String outString = outContent.toString();
216
217         System.setOut(stdout);
218         System.setErr(stderr);
219
220         assertTrue(outString.contains("specified HTTP method of \"POST\" is invalid, "
221                         + "only HTTP method \"GET\" is supported for event reception on REST client consumer"));
222     }
223
224     /**
225      * Test file events bad response.
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 testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
233         System.setOut(new PrintStream(outContent));
234         System.setErr(new PrintStream(errContent));
235
236         final String[] args =
237             { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
238         final ApexMain apexMain = new ApexMain(args);
239
240         ThreadUtilities.sleep(1000);
241         apexMain.shutdown();
242
243         final String outString = outContent.toString();
244
245         System.setOut(stdout);
246         System.setErr(stderr);
247
248         assertTrue(outString.contains(
249                         "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\""));
250     }
251 }