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 org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.utilities.TextFileUtils;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.onap.policy.common.utils.network.NetworkUtil;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
40 import java.io.ByteArrayOutputStream;
42 import java.io.IOException;
43 import java.io.PrintStream;
45 import static org.junit.Assert.fail;
48 * The Class TestRest2File.
50 public class TestRest2File {
51 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
53 private static final String BASE_URI = "http://localhost:32801/TestRest2File";
54 private static final int PORT = 32801;
55 private static HttpServletServer server;
57 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
58 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
60 private final PrintStream stdout = System.out;
61 private final PrintStream stderr = System.err;
64 * Clear relative file root environment variable.
67 public void clearRelativeFileRoot() {
68 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
74 * @throws Exception the exception
77 public void setUp() throws Exception {
78 server = HttpServletServerFactoryInstance.getServerFactory().build(
79 "TestRest2File", false, null, PORT, "/TestRest2File", false, false);
81 server.addServletClass(null, TestRestClientEndpoint.class.getName());
82 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
86 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 2000, 1L)) {
87 throw new IllegalStateException("port " + PORT + " is still not in use");
94 * @throws Exception the exception
97 public void tearDown() throws Exception {
107 public static void deleteTempFiles() {
108 new File("src/test/resources/events/EventsOut.json").delete();
112 * Test rest events in.
114 * @throws MessagingException the messaging exception
115 * @throws ApexException the apex exception
116 * @throws IOException Signals that an I/O exception has occurred.
119 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
120 final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
122 final ApexMain apexMain = new ApexMain(args);
124 ThreadUtilities.sleep(5000);
127 final String outputEventText = TextFileUtils
128 .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
130 checkRequiredString(outputEventText,
131 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
135 * Test file empty events.
137 * @throws MessagingException the messaging exception
138 * @throws ApexException the apex exception
139 * @throws IOException Signals that an I/O exception has occurred.
142 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
143 System.setOut(new PrintStream(outContent));
144 System.setErr(new PrintStream(errContent));
146 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
147 final ApexMain apexMain = new ApexMain(args);
149 ThreadUtilities.sleep(5000);
152 final String outString = outContent.toString();
154 System.setOut(stdout);
155 System.setErr(stderr);
157 checkRequiredString(outString,
158 "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
162 * Test file events no url.
164 * @throws MessagingException the messaging exception
165 * @throws ApexException the apex exception
166 * @throws IOException Signals that an I/O exception has occurred.
169 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
170 System.setOut(new PrintStream(outContent));
171 System.setErr(new PrintStream(errContent));
173 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
174 final ApexMain apexMain = new ApexMain(args);
176 ThreadUtilities.sleep(5000);
179 final String outString = outContent.toString();
181 System.setOut(stdout);
182 System.setErr(stderr);
184 checkRequiredString(outString, " no URL has been set for event sending on REST client");
188 * Test file events bad url.
190 * @throws MessagingException the messaging exception
191 * @throws ApexException the apex exception
192 * @throws IOException Signals that an I/O exception has occurred.
195 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
196 System.setOut(new PrintStream(outContent));
197 System.setErr(new PrintStream(errContent));
199 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
200 final ApexMain apexMain = new ApexMain(args);
202 ThreadUtilities.sleep(5000);
205 final String outString = outContent.toString();
207 System.setOut(stdout);
208 System.setErr(stderr);
210 checkRequiredString(outString, "reception of event from URL "
211 + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
215 * Test file events bad http method.
217 * @throws MessagingException the messaging exception
218 * @throws ApexException the apex exception
219 * @throws IOException Signals that an I/O exception has occurred.
222 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
223 System.setOut(new PrintStream(outContent));
224 System.setErr(new PrintStream(errContent));
226 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
227 final ApexMain apexMain = new ApexMain(args);
229 ThreadUtilities.sleep(5000);
232 final String outString = outContent.toString();
234 System.setOut(stdout);
235 System.setErr(stderr);
237 checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
238 + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
242 * Test file events bad response.
244 * @throws MessagingException the messaging exception
245 * @throws ApexException the apex exception
246 * @throws IOException Signals that an I/O exception has occurred.
249 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
250 System.setOut(new PrintStream(outContent));
251 System.setErr(new PrintStream(errContent));
253 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
254 final ApexMain apexMain = new ApexMain(args);
256 ThreadUtilities.sleep(5000);
259 final String outString = outContent.toString();
261 System.setOut(stdout);
262 System.setErr(stderr);
264 checkRequiredString(outString,
265 "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
266 + "failed with status code 400 and message \"");
270 * Check if a required string exists in the output.
272 * @param outputEventText the text to examine
273 * @param requiredString the string to search for
275 private void checkRequiredString(String outputEventText, String requiredString) {
276 if (!outputEventText.contains(requiredString)) {
277 LOGGER.error("\n***output text:\n" + outputEventText + "\n***");
278 fail("\n***test output did not contain required string:\n" + requiredString + "\n***");