2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
25 import static org.junit.Assert.fail;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
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;
46 * The Class TestRest2File.
48 public class TestRest2File {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
51 private static final int PORT = 32801;
52 private static HttpServletServer server;
54 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
55 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
57 private final PrintStream stdout = System.out;
58 private final PrintStream stderr = System.err;
61 * Clear relative file root environment variable.
64 public void clearRelativeFileRoot() {
65 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
71 * @throws Exception the exception
74 public void setUp() throws Exception {
75 server = HttpServletServerFactoryInstance.getServerFactory().build("TestRest2File", false, null, PORT,
76 "/TestRest2File", false, false);
78 server.addServletClass(null, TestRestClientEndpoint.class.getName());
79 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
83 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
84 throw new IllegalStateException("port " + PORT + " is still not in use");
91 * @throws Exception the exception
94 public void tearDown() throws Exception {
101 * Test rest events in.
103 * @throws MessagingException the messaging exception
104 * @throws ApexException the apex exception
105 * @throws IOException Signals that an I/O exception has occurred.
108 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
109 final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/REST2FileJsonEvent.json"};
111 final ApexMain apexMain = new ApexMain(args);
113 ThreadUtilities.sleep(5000);
116 final String outputEventText =
117 TextFileUtils.getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
119 checkRequiredString(outputEventText,
120 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
124 * Test file empty events.
126 * @throws MessagingException the messaging exception
127 * @throws ApexException the apex exception
128 * @throws IOException Signals that an I/O exception has occurred.
131 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
132 System.setOut(new PrintStream(outContent));
133 System.setErr(new PrintStream(errContent));
135 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEmptyEvents.json"};
136 final ApexMain apexMain = new ApexMain(args);
138 ThreadUtilities.sleep(5000);
141 final String outString = outContent.toString();
143 System.setOut(stdout);
144 System.setErr(stderr);
146 checkRequiredString(outString,
147 "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
151 * Test file events no url.
153 * @throws MessagingException the messaging exception
154 * @throws ApexException the apex exception
155 * @throws IOException Signals that an I/O exception has occurred.
158 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
159 System.setOut(new PrintStream(outContent));
160 System.setErr(new PrintStream(errContent));
162 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventNoURL.json"};
163 final ApexMain apexMain = new ApexMain(args);
165 ThreadUtilities.sleep(5000);
168 final String outString = outContent.toString();
170 System.setOut(stdout);
171 System.setErr(stderr);
173 checkRequiredString(outString, " no URL has been set for event sending on RESTCLIENT");
177 * Test file events bad url.
179 * @throws MessagingException the messaging exception
180 * @throws ApexException the apex exception
181 * @throws IOException Signals that an I/O exception has occurred.
184 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
185 System.setOut(new PrintStream(outContent));
186 System.setErr(new PrintStream(errContent));
188 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadURL.json"};
189 final ApexMain apexMain = new ApexMain(args);
191 ThreadUtilities.sleep(5000);
194 final String outString = outContent.toString();
196 System.setOut(stdout);
197 System.setErr(stderr);
199 checkRequiredString(outString, "reception of event from URL "
200 + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
204 * Test file events bad http method.
206 * @throws MessagingException the messaging exception
207 * @throws ApexException the apex exception
208 * @throws IOException Signals that an I/O exception has occurred.
211 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
212 System.setOut(new PrintStream(outContent));
213 System.setErr(new PrintStream(errContent));
215 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json"};
216 final ApexMain apexMain = new ApexMain(args);
218 ThreadUtilities.sleep(5000);
221 final String outString = outContent.toString();
223 System.setOut(stdout);
224 System.setErr(stderr);
226 checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
227 + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
231 * Test file events bad response.
233 * @throws MessagingException the messaging exception
234 * @throws ApexException the apex exception
235 * @throws IOException Signals that an I/O exception has occurred.
238 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
239 System.setOut(new PrintStream(outContent));
240 System.setErr(new PrintStream(errContent));
242 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadResponse.json"};
243 final ApexMain apexMain = new ApexMain(args);
245 ThreadUtilities.sleep(5000);
248 final String outString = outContent.toString();
250 System.setOut(stdout);
251 System.setErr(stderr);
253 checkRequiredString(outString,
254 "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
255 + "failed with status code 400 and message \"");
259 * Check if a required string exists in the output.
261 * @param outputEventText the text to examine
262 * @param requiredString the string to search for
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***");