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