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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
36 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.utilities.TextFileUtils;
39 import org.onap.policy.apex.service.engine.main.ApexMain;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
42 import org.onap.policy.common.gson.GsonMessageBodyHandler;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.slf4j.ext.XLogger;
45 import org.slf4j.ext.XLoggerFactory;
48 * The Class TestRest2File.
50 public class TestRest2File {
51 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
53 private static final int PORT = 32801;
54 private static HttpServletServer server;
56 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
59 private final PrintStream stdout = System.out;
60 private final PrintStream stderr = System.err;
63 * Clear relative file root environment variable.
66 public void clearRelativeFileRoot() {
67 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
73 * @throws Exception the exception
76 public void setUp() throws Exception {
77 server = HttpServletServerFactoryInstance.getServerFactory().build(
78 "TestRest2File", false, null, PORT, "/TestRest2File", false, false);
80 server.addServletClass(null, TestRestClientEndpoint.class.getName());
81 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
85 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
86 throw new IllegalStateException("port " + PORT + " is still not in use");
93 * @throws Exception the exception
96 public void tearDown() throws Exception {
106 public static void deleteTempFiles() {
107 new File("src/test/resources/events/EventsOut.json").delete();
111 * Test rest events in.
113 * @throws MessagingException the messaging exception
114 * @throws ApexException the apex exception
115 * @throws IOException Signals that an I/O exception has occurred.
118 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
119 final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
121 final ApexMain apexMain = new ApexMain(args);
123 ThreadUtilities.sleep(5000);
126 final String outputEventText = TextFileUtils
127 .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
129 checkRequiredString(outputEventText,
130 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
134 * Test file empty events.
136 * @throws MessagingException the messaging exception
137 * @throws ApexException the apex exception
138 * @throws IOException Signals that an I/O exception has occurred.
141 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
142 System.setOut(new PrintStream(outContent));
143 System.setErr(new PrintStream(errContent));
145 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
146 final ApexMain apexMain = new ApexMain(args);
148 ThreadUtilities.sleep(5000);
151 final String outString = outContent.toString();
153 System.setOut(stdout);
154 System.setErr(stderr);
156 checkRequiredString(outString,
157 "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
161 * Test file events no url.
163 * @throws MessagingException the messaging exception
164 * @throws ApexException the apex exception
165 * @throws IOException Signals that an I/O exception has occurred.
168 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
169 System.setOut(new PrintStream(outContent));
170 System.setErr(new PrintStream(errContent));
172 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
173 final ApexMain apexMain = new ApexMain(args);
175 ThreadUtilities.sleep(5000);
178 final String outString = outContent.toString();
180 System.setOut(stdout);
181 System.setErr(stderr);
183 checkRequiredString(outString, " no URL has been set for event sending on REST client");
187 * Test file events bad url.
189 * @throws MessagingException the messaging exception
190 * @throws ApexException the apex exception
191 * @throws IOException Signals that an I/O exception has occurred.
194 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
195 System.setOut(new PrintStream(outContent));
196 System.setErr(new PrintStream(errContent));
198 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
199 final ApexMain apexMain = new ApexMain(args);
201 ThreadUtilities.sleep(5000);
204 final String outString = outContent.toString();
206 System.setOut(stdout);
207 System.setErr(stderr);
209 checkRequiredString(outString, "reception of event from URL "
210 + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
214 * Test file events bad http method.
216 * @throws MessagingException the messaging exception
217 * @throws ApexException the apex exception
218 * @throws IOException Signals that an I/O exception has occurred.
221 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
222 System.setOut(new PrintStream(outContent));
223 System.setErr(new PrintStream(errContent));
225 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
226 final ApexMain apexMain = new ApexMain(args);
228 ThreadUtilities.sleep(5000);
231 final String outString = outContent.toString();
233 System.setOut(stdout);
234 System.setErr(stderr);
236 checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
237 + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
241 * Test file events bad response.
243 * @throws MessagingException the messaging exception
244 * @throws ApexException the apex exception
245 * @throws IOException Signals that an I/O exception has occurred.
248 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
249 System.setOut(new PrintStream(outContent));
250 System.setErr(new PrintStream(errContent));
252 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
253 final ApexMain apexMain = new ApexMain(args);
255 ThreadUtilities.sleep(5000);
258 final String outString = outContent.toString();
260 System.setOut(stdout);
261 System.setErr(stderr);
263 checkRequiredString(outString,
264 "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
265 + "failed with status code 400 and message \"");
269 * Check if a required string exists in the output.
271 * @param outputEventText the text to examine
272 * @param requiredString the string to search for
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***");