216b566ff269a86791aa380b925126f483a10d6d
[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.fail;
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 import org.slf4j.ext.XLogger;
44 import org.slf4j.ext.XLoggerFactory;
45
46 /**
47  * The Class TestRest2File.
48  */
49 public class TestRest2File {
50     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
51
52     private static final String BASE_URI = "http://localhost:32801/TestRest2File";
53     private HttpServer server;
54
55     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
57
58     private final PrintStream stdout = System.out;
59     private final PrintStream stderr = System.err;
60
61     /**
62      * Clear relative file root environment variable.
63      */
64     @Before
65     public void clearRelativeFileRoot() {
66         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
67     }
68
69     /**
70      * Sets the up.
71      *
72      * @throws Exception the exception
73      */
74     @Before
75     public void setUp() throws Exception {
76         final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
77         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
78
79         while (!server.isStarted()) {
80             ThreadUtilities.sleep(50);
81         }
82     }
83
84     /**
85      * Tear down.
86      *
87      * @throws Exception the exception
88      */
89     @After
90     public void tearDown() throws Exception {
91         server.shutdown();
92     }
93
94     /**
95      * Delete temp files.
96      */
97     @AfterClass
98     public static void deleteTempFiles() {
99         new File("src/test/resources/events/EventsOut.json").delete();
100     }
101
102     /**
103      * Test rest events in.
104      *
105      * @throws MessagingException the messaging exception
106      * @throws ApexException the apex exception
107      * @throws IOException Signals that an I/O exception has occurred.
108      */
109     @Test
110     public void testRestEventsIn() throws MessagingException, ApexException, IOException {
111         final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
112
113         final ApexMain apexMain = new ApexMain(args);
114
115         ThreadUtilities.sleep(5000);
116         apexMain.shutdown();
117
118         final String outputEventText = TextFileUtils
119             .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
120
121         checkRequiredString(outputEventText,
122             "04\",\n" + "  \"version\": \"0.0.1\",\n" + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
123     }
124
125     /**
126      * Test file empty events.
127      *
128      * @throws MessagingException the messaging exception
129      * @throws ApexException the apex exception
130      * @throws IOException Signals that an I/O exception has occurred.
131      */
132     @Test
133     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
134         System.setOut(new PrintStream(outContent));
135         System.setErr(new PrintStream(errContent));
136
137         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
138         final ApexMain apexMain = new ApexMain(args);
139
140         ThreadUtilities.sleep(5000);
141         apexMain.shutdown();
142
143         final String outString = outContent.toString();
144
145         System.setOut(stdout);
146         System.setErr(stderr);
147
148         checkRequiredString(outString,
149             "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
150     }
151
152     /**
153      * Test file events no url.
154      *
155      * @throws MessagingException the messaging exception
156      * @throws ApexException the apex exception
157      * @throws IOException Signals that an I/O exception has occurred.
158      */
159     @Test
160     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
161         System.setOut(new PrintStream(outContent));
162         System.setErr(new PrintStream(errContent));
163
164         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
165         final ApexMain apexMain = new ApexMain(args);
166
167         ThreadUtilities.sleep(5000);
168         apexMain.shutdown();
169
170         final String outString = outContent.toString();
171
172         System.setOut(stdout);
173         System.setErr(stderr);
174
175         checkRequiredString(outString, " no URL has been set for event sending on REST client");
176     }
177
178     /**
179      * Test file events bad url.
180      *
181      * @throws MessagingException the messaging exception
182      * @throws ApexException the apex exception
183      * @throws IOException Signals that an I/O exception has occurred.
184      */
185     @Test
186     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
187         System.setOut(new PrintStream(outContent));
188         System.setErr(new PrintStream(errContent));
189
190         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
191         final ApexMain apexMain = new ApexMain(args);
192
193         ThreadUtilities.sleep(5000);
194         apexMain.shutdown();
195
196         final String outString = outContent.toString();
197
198         System.setOut(stdout);
199         System.setErr(stderr);
200
201         checkRequiredString(outString, "reception of event from URL "
202             + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
203     }
204
205     /**
206      * Test file events bad http method.
207      *
208      * @throws MessagingException the messaging exception
209      * @throws ApexException the apex exception
210      * @throws IOException Signals that an I/O exception has occurred.
211      */
212     @Test
213     public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
214         System.setOut(new PrintStream(outContent));
215         System.setErr(new PrintStream(errContent));
216
217         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
218         final ApexMain apexMain = new ApexMain(args);
219
220         ThreadUtilities.sleep(5000);
221         apexMain.shutdown();
222
223         final String outString = outContent.toString();
224
225         System.setOut(stdout);
226         System.setErr(stderr);
227
228         checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
229             + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
230     }
231
232     /**
233      * Test file events bad response.
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 testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
241         System.setOut(new PrintStream(outContent));
242         System.setErr(new PrintStream(errContent));
243
244         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
245         final ApexMain apexMain = new ApexMain(args);
246
247         ThreadUtilities.sleep(5000);
248         apexMain.shutdown();
249
250         final String outString = outContent.toString();
251
252         System.setOut(stdout);
253         System.setErr(stderr);
254
255         checkRequiredString(outString,
256             "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
257                 + "failed with status code 400 and message \"\"");
258     }
259
260     /**
261      * Check if a required string exists in the output.
262      * 
263      * @param outputEventText the text to examine
264      * @param requiredString the string to search for
265      */
266     private void checkRequiredString(String outputText, String requiredString) {
267         if (!outputText.contains(requiredString)) {
268             LOGGER.error("\n***output text:\n" + outputText + "\n***");
269             fail("\n***test output did not contain required string:\n" + requiredString + "\n***");
270         }
271     }
272 }