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.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
32 import org.glassfish.grizzly.http.server.HttpServer;
33 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
34 import org.glassfish.jersey.server.ResourceConfig;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
40 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
41 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
42 import org.onap.policy.apex.model.utilities.TextFileUtils;
43 import org.onap.policy.apex.service.engine.main.ApexMain;
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 String BASE_URI = "http://localhost:32801/TestRest2File";
54 private HttpServer 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 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
78 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
80 while (!server.isStarted()) {
81 ThreadUtilities.sleep(50);
88 * @throws Exception the exception
91 public void tearDown() throws Exception {
99 public static void deleteTempFiles() {
100 new File("src/test/resources/events/EventsOut.json").delete();
104 * Test rest events in.
106 * @throws MessagingException the messaging exception
107 * @throws ApexException the apex exception
108 * @throws IOException Signals that an I/O exception has occurred.
111 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
112 final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
114 final ApexMain apexMain = new ApexMain(args);
116 ThreadUtilities.sleep(1000);
119 final String outputEventText = TextFileUtils
120 .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
122 if (!outputEventText.contains(
123 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\"")) {
124 LOGGER.error(outputEventText);
125 fail("test output did not contain required string");
130 * Test file empty events.
132 * @throws MessagingException the messaging exception
133 * @throws ApexException the apex exception
134 * @throws IOException Signals that an I/O exception has occurred.
137 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
138 System.setOut(new PrintStream(outContent));
139 System.setErr(new PrintStream(errContent));
141 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
142 final ApexMain apexMain = new ApexMain(args);
144 ThreadUtilities.sleep(1000);
147 final String outString = outContent.toString();
149 System.setOut(stdout);
150 System.setErr(stderr);
152 if (!outString.contains(
153 "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"")) {
154 LOGGER.error(outString);
155 fail("test output did not contain required string");
160 * Test file events no url.
162 * @throws MessagingException the messaging exception
163 * @throws ApexException the apex exception
164 * @throws IOException Signals that an I/O exception has occurred.
167 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
168 System.setOut(new PrintStream(outContent));
169 System.setErr(new PrintStream(errContent));
171 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
172 final ApexMain apexMain = new ApexMain(args);
174 ThreadUtilities.sleep(1000);
177 final String outString = outContent.toString();
179 System.setOut(stdout);
180 System.setErr(stderr);
182 if (!outString.contains(" no URL has been set for event sending on REST client")) {
183 LOGGER.error(outString);
184 fail("test output did not contain required string");
189 * Test file events bad url.
191 * @throws MessagingException the messaging exception
192 * @throws ApexException the apex exception
193 * @throws IOException Signals that an I/O exception has occurred.
196 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
197 System.setOut(new PrintStream(outContent));
198 System.setErr(new PrintStream(errContent));
200 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
201 final ApexMain apexMain = new ApexMain(args);
203 ThreadUtilities.sleep(1000);
206 final String outString = outContent.toString();
208 System.setOut(stdout);
209 System.setErr(stderr);
211 if (!outString.contains(
212 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404")) {
213 LOGGER.error(outString);
214 fail("test output did not contain required string");
219 * Test file events bad http method.
221 * @throws MessagingException the messaging exception
222 * @throws ApexException the apex exception
223 * @throws IOException Signals that an I/O exception has occurred.
226 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
227 System.setOut(new PrintStream(outContent));
228 System.setErr(new PrintStream(errContent));
230 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
231 final ApexMain apexMain = new ApexMain(args);
233 ThreadUtilities.sleep(1000);
236 final String outString = outContent.toString();
238 System.setOut(stdout);
239 System.setErr(stderr);
241 if (!outString.contains("specified HTTP method of \"POST\" is invalid, "
242 + "only HTTP method \"GET\" is supported for event reception on REST client consumer")) {
243 LOGGER.error(outString);
244 fail("test output did not contain required string");
249 * Test file events bad response.
251 * @throws MessagingException the messaging exception
252 * @throws ApexException the apex exception
253 * @throws IOException Signals that an I/O exception has occurred.
256 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
257 System.setOut(new PrintStream(outContent));
258 System.setErr(new PrintStream(errContent));
260 final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
261 final ApexMain apexMain = new ApexMain(args);
263 ThreadUtilities.sleep(1000);
266 final String outString = outContent.toString();
268 System.setOut(stdout);
269 System.setErr(stderr);
271 if (!outString.contains(
272 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\"")) {
273 LOGGER.error(outString);
274 fail("test output did not contain required string");