c44950b9e2e3364adcac3d85de5e4ceac6a79d46
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * 
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * 
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
23
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.utilities.TextFileUtils;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.onap.policy.common.utils.network.NetworkUtil;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
39
40 import java.io.ByteArrayOutputStream;
41 import java.io.File;
42 import java.io.IOException;
43 import java.io.PrintStream;
44
45 import static org.junit.Assert.fail;
46
47 /**
48  * The Class TestRest2File.
49  */
50 public class TestRest2File {
51     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
52
53     private static final String BASE_URI = "http://localhost:32801/TestRest2File";
54     private static final int PORT = 32801;
55     private static HttpServletServer server;
56
57     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
58     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
59
60     private final PrintStream stdout = System.out;
61     private final PrintStream stderr = System.err;
62
63     /**
64      * Clear relative file root environment variable.
65      */
66     @Before
67     public void clearRelativeFileRoot() {
68         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
69     }
70
71     /**
72      * Sets the up.
73      *
74      * @throws Exception the exception
75      */
76     @Before
77     public void setUp() throws Exception {
78         server = HttpServletServerFactoryInstance.getServerFactory().build(
79             "TestRest2File", false, null, PORT, "/TestRest2File", false, false);
80
81         server.addServletClass(null, TestRestClientEndpoint.class.getName());
82         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
83
84         server.start();
85
86         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 2000, 1L)) {
87             throw new IllegalStateException("port " + PORT + " is still not in use");
88         }
89     }
90
91     /**
92      * Tear down.
93      *
94      * @throws Exception the exception
95      */
96     @After
97     public void tearDown() throws Exception {
98         if (server != null) {
99             server.stop();
100         }
101     }
102
103     /**
104      * Delete temp files.
105      */
106     @AfterClass
107     public static void deleteTempFiles() {
108         new File("src/test/resources/events/EventsOut.json").delete();
109     }
110
111     /**
112      * Test rest events in.
113      *
114      * @throws MessagingException the messaging exception
115      * @throws ApexException the apex exception
116      * @throws IOException Signals that an I/O exception has occurred.
117      */
118     @Test
119     public void testRestEventsIn() throws MessagingException, ApexException, IOException {
120         final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
121
122         final ApexMain apexMain = new ApexMain(args);
123
124         ThreadUtilities.sleep(5000);
125         apexMain.shutdown();
126
127         final String outputEventText = TextFileUtils
128             .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
129
130         checkRequiredString(outputEventText,
131             "04\",\n" + "  \"version\": \"0.0.1\",\n" + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
132     }
133
134     /**
135      * Test file empty events.
136      *
137      * @throws MessagingException the messaging exception
138      * @throws ApexException the apex exception
139      * @throws IOException Signals that an I/O exception has occurred.
140      */
141     @Test
142     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
143         System.setOut(new PrintStream(outContent));
144         System.setErr(new PrintStream(errContent));
145
146         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
147         final ApexMain apexMain = new ApexMain(args);
148
149         ThreadUtilities.sleep(5000);
150         apexMain.shutdown();
151
152         final String outString = outContent.toString();
153
154         System.setOut(stdout);
155         System.setErr(stderr);
156
157         checkRequiredString(outString,
158             "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
159     }
160
161     /**
162      * Test file events no url.
163      *
164      * @throws MessagingException the messaging exception
165      * @throws ApexException the apex exception
166      * @throws IOException Signals that an I/O exception has occurred.
167      */
168     @Test
169     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
170         System.setOut(new PrintStream(outContent));
171         System.setErr(new PrintStream(errContent));
172
173         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
174         final ApexMain apexMain = new ApexMain(args);
175
176         ThreadUtilities.sleep(5000);
177         apexMain.shutdown();
178
179         final String outString = outContent.toString();
180
181         System.setOut(stdout);
182         System.setErr(stderr);
183
184         checkRequiredString(outString, " no URL has been set for event sending on REST client");
185     }
186
187     /**
188      * Test file events bad url.
189      *
190      * @throws MessagingException the messaging exception
191      * @throws ApexException the apex exception
192      * @throws IOException Signals that an I/O exception has occurred.
193      */
194     @Test
195     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
196         System.setOut(new PrintStream(outContent));
197         System.setErr(new PrintStream(errContent));
198
199         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
200         final ApexMain apexMain = new ApexMain(args);
201
202         ThreadUtilities.sleep(5000);
203         apexMain.shutdown();
204
205         final String outString = outContent.toString();
206
207         System.setOut(stdout);
208         System.setErr(stderr);
209
210         checkRequiredString(outString, "reception of event from URL "
211             + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
212     }
213
214     /**
215      * Test file events bad http method.
216      *
217      * @throws MessagingException the messaging exception
218      * @throws ApexException the apex exception
219      * @throws IOException Signals that an I/O exception has occurred.
220      */
221     @Test
222     public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
223         System.setOut(new PrintStream(outContent));
224         System.setErr(new PrintStream(errContent));
225
226         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
227         final ApexMain apexMain = new ApexMain(args);
228
229         ThreadUtilities.sleep(5000);
230         apexMain.shutdown();
231
232         final String outString = outContent.toString();
233
234         System.setOut(stdout);
235         System.setErr(stderr);
236
237         checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
238             + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
239     }
240
241     /**
242      * Test file events bad response.
243      *
244      * @throws MessagingException the messaging exception
245      * @throws ApexException the apex exception
246      * @throws IOException Signals that an I/O exception has occurred.
247      */
248     @Test
249     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
250         System.setOut(new PrintStream(outContent));
251         System.setErr(new PrintStream(errContent));
252
253         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
254         final ApexMain apexMain = new ApexMain(args);
255
256         ThreadUtilities.sleep(5000);
257         apexMain.shutdown();
258
259         final String outString = outContent.toString();
260
261         System.setOut(stdout);
262         System.setErr(stderr);
263
264         checkRequiredString(outString,
265             "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
266                 + "failed with status code 400 and message \"");
267     }
268
269     /**
270      * Check if a required string exists in the output.
271      * 
272      * @param outputEventText the text to examine
273      * @param requiredString the string to search for
274      */
275     private void checkRequiredString(String outputEventText, String requiredString) {
276         if (!outputEventText.contains(requiredString)) {
277             LOGGER.error("\n***output text:\n" + outputEventText + "\n***");
278             fail("\n***test output did not contain required string:\n" + requiredString + "\n***");
279         }
280     }
281 }