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-2021 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.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.fail;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import org.junit.After;
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.service.engine.main.ApexMain;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
40 import org.onap.policy.common.gson.GsonMessageBodyHandler;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
43 import org.slf4j.ext.XLogger;
44 import org.slf4j.ext.XLoggerFactory;
47 * The Class TestRest2File.
49 public class TestRest2File {
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
52 private static final int PORT = 32801;
53 private static HttpServletServer server;
55 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
58 private final PrintStream stdout = System.out;
59 private final PrintStream stderr = System.err;
62 * Clear relative file root environment variable.
65 public void clearRelativeFileRoot() {
66 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
72 * @throws Exception the exception
75 public void setUp() throws Exception {
76 server = HttpServletServerFactoryInstance.getServerFactory().build("TestRest2File", false, null, PORT,
77 "/TestRest2File", false, false);
79 server.addServletClass(null, TestRestClientEndpoint.class.getName());
80 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
84 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
85 throw new IllegalStateException("port " + PORT + " is still not in use");
92 * @throws Exception the exception
95 public void tearDown() throws Exception {
102 * Test rest events in.
104 * @throws MessagingException the messaging exception
105 * @throws ApexException the apex exception
106 * @throws IOException Signals that an I/O exception has occurred.
109 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
110 final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/REST2FileJsonEvent.json"};
112 final ApexMain apexMain = new ApexMain(args);
114 ThreadUtilities.sleep(5000);
117 final String outputEventText =
118 TextFileUtils.getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
120 checkRequiredString(outputEventText,
121 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
125 * Test file empty events.
127 * @throws MessagingException the messaging exception
128 * @throws ApexException the apex exception
129 * @throws IOException Signals that an I/O exception has occurred.
132 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
133 System.setOut(new PrintStream(outContent));
134 System.setErr(new PrintStream(errContent));
136 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEmptyEvents.json"};
137 final ApexMain apexMain = new ApexMain(args);
139 ThreadUtilities.sleep(5000);
142 final String outString = outContent.toString();
144 System.setOut(stdout);
145 System.setErr(stderr);
147 checkRequiredString(outString,
148 "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
152 * Test file events no url.
154 * @throws MessagingException the messaging exception
155 * @throws ApexException the apex exception
156 * @throws IOException Signals that an I/O exception has occurred.
159 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
160 System.setOut(new PrintStream(outContent));
161 System.setErr(new PrintStream(errContent));
163 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventNoURL.json"};
164 final ApexMain apexMain = new ApexMain(args);
166 ThreadUtilities.sleep(5000);
169 final String outString = outContent.toString();
171 System.setOut(stdout);
172 System.setErr(stderr);
174 checkRequiredString(outString, " no URL has been set for event sending on RESTCLIENT");
178 * Test file events bad url.
180 * @throws MessagingException the messaging exception
181 * @throws ApexException the apex exception
182 * @throws IOException Signals that an I/O exception has occurred.
185 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
186 System.setOut(new PrintStream(outContent));
187 System.setErr(new PrintStream(errContent));
189 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadURL.json"};
190 final ApexMain apexMain = new ApexMain(args);
192 ThreadUtilities.sleep(5000);
195 final String outString = outContent.toString();
197 System.setOut(stdout);
198 System.setErr(stderr);
200 checkRequiredString(outString, "reception of event from URL "
201 + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
205 * Test file events bad http method.
208 public void testFileEventsBadHttpMethod() {
209 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json"};
210 assertThatThrownBy(() -> new ApexMain(args))
211 .hasRootCauseMessage("specified HTTP method of \"POST\" is invalid, "
212 + "only HTTP method \"GET\" is supported for event reception on REST client consumer (FirstConsumer)");
217 * Test file events bad response.
219 * @throws MessagingException the messaging exception
220 * @throws ApexException the apex exception
221 * @throws IOException Signals that an I/O exception has occurred.
224 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
225 System.setOut(new PrintStream(outContent));
226 System.setErr(new PrintStream(errContent));
228 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadResponse.json"};
229 final ApexMain apexMain = new ApexMain(args);
231 ThreadUtilities.sleep(5000);
234 final String outString = outContent.toString();
236 System.setOut(stdout);
237 System.setErr(stderr);
239 checkRequiredString(outString,
240 "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
241 + "failed with status code 400 and message \"");
245 * Check if a required string exists in the output.
247 * @param outputEventText the text to examine
248 * @param requiredString the string to search for
250 private void checkRequiredString(String outputEventText, String requiredString) {
251 if (!outputEventText.contains(requiredString)) {
252 LOGGER.error("\n***output text:\n" + outputEventText + "\n***");
253 fail("\n***test output did not contain required string:\n" + requiredString + "\n***");