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;
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;
45 * The Class TestRest2File.
47 public class TestRest2File {
49 private static final String BASE_URI = "http://localhost:32801/TestRest2File";
50 private HttpServer server;
52 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
53 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
55 private final PrintStream stdout = System.out;
56 private final PrintStream stderr = System.err;
59 * Clear relative file root environment variable.
62 public void clearRelativeFileRoot() {
63 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
69 * @throws Exception the exception
72 public void setUp() throws Exception {
73 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
74 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
76 while (!server.isStarted()) {
77 ThreadUtilities.sleep(50);
84 * @throws Exception the exception
87 public void tearDown() throws Exception {
95 public static void deleteTempFiles() {
96 new File("src/test/resources/events/EventsOut.json").delete();
100 * Test rest events in.
102 * @throws MessagingException the messaging exception
103 * @throws ApexException the apex exception
104 * @throws IOException Signals that an I/O exception has occurred.
107 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
108 final String[] args =
109 { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
111 final ApexMain apexMain = new ApexMain(args);
113 ThreadUtilities.sleep(1000);
116 final String outputEventText = TextFileUtils
117 .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
118 assertTrue(outputEventText.contains("04\",\n" + " \"version\": \"0.0.1\",\n"
119 + " \"nameSpace\": \"org.onap.policy.apex.sample.events\""));
123 * Test file empty events.
125 * @throws MessagingException the messaging exception
126 * @throws ApexException the apex exception
127 * @throws IOException Signals that an I/O exception has occurred.
130 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
131 System.setOut(new PrintStream(outContent));
132 System.setErr(new PrintStream(errContent));
134 final String[] args =
135 { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
136 final ApexMain apexMain = new ApexMain(args);
138 ThreadUtilities.sleep(1000);
141 final String outString = outContent.toString();
143 System.setOut(stdout);
144 System.setErr(stderr);
146 assertTrue(outString.contains(
147 "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\""));
151 * Test file events no url.
153 * @throws MessagingException the messaging exception
154 * @throws ApexException the apex exception
155 * @throws IOException Signals that an I/O exception has occurred.
158 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
159 System.setOut(new PrintStream(outContent));
160 System.setErr(new PrintStream(errContent));
162 final String[] args =
163 { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
164 final ApexMain apexMain = new ApexMain(args);
166 ThreadUtilities.sleep(1000);
169 final String outString = outContent.toString();
171 System.setOut(stdout);
172 System.setErr(stderr);
174 assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
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 =
190 { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
191 final ApexMain apexMain = new ApexMain(args);
193 ThreadUtilities.sleep(1000);
196 final String outString = outContent.toString();
198 System.setOut(stdout);
199 System.setErr(stderr);
201 assertTrue(outString.contains(
202 "reception of event from URL \"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 =
218 { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
219 final ApexMain apexMain = new ApexMain(args);
221 ThreadUtilities.sleep(1000);
224 final String outString = outContent.toString();
226 System.setOut(stdout);
227 System.setErr(stderr);
229 assertTrue(outString.contains("specified HTTP method of \"POST\" is invalid, "
230 + "only HTTP method \"GET\" is supported for event reception on REST client consumer"));
234 * Test file events bad response.
236 * @throws MessagingException the messaging exception
237 * @throws ApexException the apex exception
238 * @throws IOException Signals that an I/O exception has occurred.
241 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
242 System.setOut(new PrintStream(outContent));
243 System.setErr(new PrintStream(errContent));
245 final String[] args =
246 { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
247 final ApexMain apexMain = new ApexMain(args);
249 ThreadUtilities.sleep(1000);
252 final String outString = outContent.toString();
254 System.setOut(stdout);
255 System.setErr(stderr);
257 assertTrue(outString.contains(
258 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\""));