2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
23 import static org.junit.Assert.fail;
25 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
31 import org.glassfish.grizzly.http.server.HttpServer;
32 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
39 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.model.utilities.TextFileUtils;
42 import org.onap.policy.apex.service.engine.main.ApexMain;
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 String BASE_URI = "http://localhost:32801/TestRest2File";
53 private HttpServer 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 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
77 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
79 while (!server.isStarted()) {
80 ThreadUtilities.sleep(50);
87 * @throws Exception the exception
90 public void tearDown() throws Exception {
98 public static void deleteTempFiles() {
99 new File("src/test/resources/events/EventsOut.json").delete();
103 * Test rest events in.
105 * @throws MessagingException the messaging exception
106 * @throws ApexException the apex exception
107 * @throws IOException Signals that an I/O exception has occurred.
110 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
111 final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
113 final ApexMain apexMain = new ApexMain(args);
115 ThreadUtilities.sleep(5000);
118 final String outputEventText = TextFileUtils
119 .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
121 checkRequiredString(outputEventText,
122 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
126 * Test file empty events.
128 * @throws MessagingException the messaging exception
129 * @throws ApexException the apex exception
130 * @throws IOException Signals that an I/O exception has occurred.
133 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
134 System.setOut(new PrintStream(outContent));
135 System.setErr(new PrintStream(errContent));
137 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
138 final ApexMain apexMain = new ApexMain(args);
140 ThreadUtilities.sleep(5000);
143 final String outString = outContent.toString();
145 System.setOut(stdout);
146 System.setErr(stderr);
148 checkRequiredString(outString,
149 "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
153 * Test file events no url.
155 * @throws MessagingException the messaging exception
156 * @throws ApexException the apex exception
157 * @throws IOException Signals that an I/O exception has occurred.
160 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
161 System.setOut(new PrintStream(outContent));
162 System.setErr(new PrintStream(errContent));
164 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
165 final ApexMain apexMain = new ApexMain(args);
167 ThreadUtilities.sleep(5000);
170 final String outString = outContent.toString();
172 System.setOut(stdout);
173 System.setErr(stderr);
175 checkRequiredString(outString, " no URL has been set for event sending on REST client");
179 * Test file events bad url.
181 * @throws MessagingException the messaging exception
182 * @throws ApexException the apex exception
183 * @throws IOException Signals that an I/O exception has occurred.
186 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
187 System.setOut(new PrintStream(outContent));
188 System.setErr(new PrintStream(errContent));
190 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
191 final ApexMain apexMain = new ApexMain(args);
193 ThreadUtilities.sleep(5000);
196 final String outString = outContent.toString();
198 System.setOut(stdout);
199 System.setErr(stderr);
201 checkRequiredString(outString, "reception of event from URL "
202 + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
206 * Test file events bad http method.
208 * @throws MessagingException the messaging exception
209 * @throws ApexException the apex exception
210 * @throws IOException Signals that an I/O exception has occurred.
213 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
214 System.setOut(new PrintStream(outContent));
215 System.setErr(new PrintStream(errContent));
217 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
218 final ApexMain apexMain = new ApexMain(args);
220 ThreadUtilities.sleep(5000);
223 final String outString = outContent.toString();
225 System.setOut(stdout);
226 System.setErr(stderr);
228 checkRequiredString(outString, "specified HTTP method of \"POST\" is invalid, "
229 + "only HTTP method \"GET\" is supported for event reception on REST client consumer");
233 * Test file events bad response.
235 * @throws MessagingException the messaging exception
236 * @throws ApexException the apex exception
237 * @throws IOException Signals that an I/O exception has occurred.
240 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
241 System.setOut(new PrintStream(outContent));
242 System.setErr(new PrintStream(errContent));
244 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
245 final ApexMain apexMain = new ApexMain(args);
247 ThreadUtilities.sleep(5000);
250 final String outString = outContent.toString();
252 System.setOut(stdout);
253 System.setErr(stderr);
255 checkRequiredString(outString,
256 "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
257 + "failed with status code 400 and message \"\"");
261 * Check if a required string exists in the output.
263 * @param outputEventText the text to examine
264 * @param requiredString the string to search for
266 private void checkRequiredString(String outputText, String requiredString) {
267 if (!outputText.contains(requiredString)) {
268 LOGGER.error("\n***output text:\n" + outputText + "\n***");
269 fail("\n***test output did not contain required string:\n" + requiredString + "\n***");