5dd2f054636a2cd85e2c411bdf3e5e660cc169ad
[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 static org.junit.Assert.fail;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.model.utilities.TextFileUtils;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
41 import org.onap.policy.common.gson.GsonMessageBodyHandler;
42 import org.onap.policy.common.utils.network.NetworkUtil;
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 static final int PORT = 32801;
54     private static HttpServletServer 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      * Clear relative file root environment variable.
64      */
65     @Before
66     public void clearRelativeFileRoot() {
67         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
68     }
69
70     /**
71      * Sets the up.
72      *
73      * @throws Exception the exception
74      */
75     @Before
76     public void setUp() throws Exception {
77         server = HttpServletServerFactoryInstance.getServerFactory().build(
78             "TestRest2File", false, null, PORT, "/TestRest2File", false, false);
79
80         server.addServletClass(null, TestRestClientEndpoint.class.getName());
81         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
82
83         server.start();
84
85         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
86             throw new IllegalStateException("port " + PORT + " is still not in use");
87         }
88     }
89
90     /**
91      * Tear down.
92      *
93      * @throws Exception the exception
94      */
95     @After
96     public void tearDown() throws Exception {
97         if (server != null) {
98             server.stop();
99         }
100     }
101
102     /**
103      * Delete temp files.
104      */
105     @AfterClass
106     public static void deleteTempFiles() {
107         new File("src/test/resources/events/EventsOut.json").delete();
108     }
109
110     /**
111      * Test rest events in.
112      *
113      * @throws MessagingException the messaging exception
114      * @throws ApexException the apex exception
115      * @throws IOException Signals that an I/O exception has occurred.
116      */
117     @Test
118     public void testRestEventsIn() throws MessagingException, ApexException, IOException {
119         final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
120
121         final ApexMain apexMain = new ApexMain(args);
122
123         ThreadUtilities.sleep(5000);
124         apexMain.shutdown();
125
126         final String outputEventText = TextFileUtils
127             .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
128
129         checkRequiredString(outputEventText,
130             "04\",\n" + "  \"version\": \"0.0.1\",\n" + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
131     }
132
133     /**
134      * Test file empty events.
135      *
136      * @throws MessagingException the messaging exception
137      * @throws ApexException the apex exception
138      * @throws IOException Signals that an I/O exception has occurred.
139      */
140     @Test
141     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
142         System.setOut(new PrintStream(outContent));
143         System.setErr(new PrintStream(errContent));
144
145         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
146         final ApexMain apexMain = new ApexMain(args);
147
148         ThreadUtilities.sleep(5000);
149         apexMain.shutdown();
150
151         final String outString = outContent.toString();
152
153         System.setOut(stdout);
154         System.setErr(stderr);
155
156         checkRequiredString(outString,
157             "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
158     }
159
160     /**
161      * Test file events no url.
162      *
163      * @throws MessagingException the messaging exception
164      * @throws ApexException the apex exception
165      * @throws IOException Signals that an I/O exception has occurred.
166      */
167     @Test
168     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
169         System.setOut(new PrintStream(outContent));
170         System.setErr(new PrintStream(errContent));
171
172         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
173         final ApexMain apexMain = new ApexMain(args);
174
175         ThreadUtilities.sleep(5000);
176         apexMain.shutdown();
177
178         final String outString = outContent.toString();
179
180         System.setOut(stdout);
181         System.setErr(stderr);
182
183         checkRequiredString(outString, " no URL has been set for event sending on REST client");
184     }
185
186     /**
187      * Test file events bad url.
188      *
189      * @throws MessagingException the messaging exception
190      * @throws ApexException the apex exception
191      * @throws IOException Signals that an I/O exception has occurred.
192      */
193     @Test
194     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
195         System.setOut(new PrintStream(outContent));
196         System.setErr(new PrintStream(errContent));
197
198         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
199         final ApexMain apexMain = new ApexMain(args);
200
201         ThreadUtilities.sleep(5000);
202         apexMain.shutdown();
203
204         final String outString = outContent.toString();
205
206         System.setOut(stdout);
207         System.setErr(stderr);
208
209         checkRequiredString(outString, "reception of event from URL "
210             + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
211     }
212
213     /**
214      * Test file events bad http method.
215      *
216      * @throws MessagingException the messaging exception
217      * @throws ApexException the apex exception
218      * @throws IOException Signals that an I/O exception has occurred.
219      */
220     @Test
221     public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
222         System.setOut(new PrintStream(outContent));
223         System.setErr(new PrintStream(errContent));
224
225         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
226         final ApexMain apexMain = new ApexMain(args);
227
228         ThreadUtilities.sleep(5000);
229         apexMain.shutdown();
230
231         final String outString = outContent.toString();
232
233         System.setOut(stdout);
234         System.setErr(stderr);
235
236         checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
237             + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
238     }
239
240     /**
241      * Test file events bad response.
242      *
243      * @throws MessagingException the messaging exception
244      * @throws ApexException the apex exception
245      * @throws IOException Signals that an I/O exception has occurred.
246      */
247     @Test
248     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
249         System.setOut(new PrintStream(outContent));
250         System.setErr(new PrintStream(errContent));
251
252         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
253         final ApexMain apexMain = new ApexMain(args);
254
255         ThreadUtilities.sleep(5000);
256         apexMain.shutdown();
257
258         final String outString = outContent.toString();
259
260         System.setOut(stdout);
261         System.setErr(stderr);
262
263         checkRequiredString(outString,
264             "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
265                 + "failed with status code 400 and message \"");
266     }
267
268     /**
269      * Check if a required string exists in the output.
270      *
271      * @param outputEventText the text to examine
272      * @param requiredString the string to search for
273      */
274     private void checkRequiredString(String outputEventText, String requiredString) {
275         if (!outputEventText.contains(requiredString)) {
276             LOGGER.error("\n***output text:\n" + outputEventText + "\n***");
277             fail("\n***test output did not contain required string:\n" + requiredString + "\n***");
278         }
279     }
280 }