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;
61 * @throws Exception the exception
64 public void setUp() throws Exception {
65 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
66 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
68 while (!server.isStarted()) {
69 ThreadUtilities.sleep(50);
76 * @throws Exception the exception
79 public void tearDown() throws Exception {
87 public static void deleteTempFiles() {
88 new File("src/test/resources/events/EventsOut.json").delete();
92 * Test rest events in.
94 * @throws MessagingException the messaging exception
95 * @throws ApexException the apex exception
96 * @throws IOException Signals that an I/O exception has occurred.
99 public void testRestEventsIn() throws MessagingException, ApexException, IOException {
100 final String[] args =
101 { "src/test/resources/prodcons/REST2FileJsonEvent.json" };
103 final ApexMain apexMain = new ApexMain(args);
105 ThreadUtilities.sleep(1000);
108 final String outputEventText = TextFileUtils.getTextFileAsString("src/test/resources/events/EventsOut.json");
109 assertTrue(outputEventText.contains("04\",\n" + " \"version\": \"0.0.1\",\n"
110 + " \"nameSpace\": \"org.onap.policy.apex.sample.events\""));
114 * Test file empty events.
116 * @throws MessagingException the messaging exception
117 * @throws ApexException the apex exception
118 * @throws IOException Signals that an I/O exception has occurred.
121 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
122 System.setOut(new PrintStream(outContent));
123 System.setErr(new PrintStream(errContent));
125 final String[] args =
126 { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
127 final ApexMain apexMain = new ApexMain(args);
129 ThreadUtilities.sleep(1000);
132 final String outString = outContent.toString();
134 System.setOut(stdout);
135 System.setErr(stderr);
137 assertTrue(outString.contains(
138 "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\""));
142 * Test file events no url.
144 * @throws MessagingException the messaging exception
145 * @throws ApexException the apex exception
146 * @throws IOException Signals that an I/O exception has occurred.
149 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
150 System.setOut(new PrintStream(outContent));
151 System.setErr(new PrintStream(errContent));
153 final String[] args =
154 { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
155 final ApexMain apexMain = new ApexMain(args);
157 ThreadUtilities.sleep(1000);
160 final String outString = outContent.toString();
162 System.setOut(stdout);
163 System.setErr(stderr);
165 assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
169 * Test file events bad url.
171 * @throws MessagingException the messaging exception
172 * @throws ApexException the apex exception
173 * @throws IOException Signals that an I/O exception has occurred.
176 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
177 System.setOut(new PrintStream(outContent));
178 System.setErr(new PrintStream(errContent));
180 final String[] args =
181 { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
182 final ApexMain apexMain = new ApexMain(args);
184 ThreadUtilities.sleep(1000);
187 final String outString = outContent.toString();
189 System.setOut(stdout);
190 System.setErr(stderr);
192 assertTrue(outString.contains(
193 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404"));
197 * Test file events bad http method.
199 * @throws MessagingException the messaging exception
200 * @throws ApexException the apex exception
201 * @throws IOException Signals that an I/O exception has occurred.
204 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
205 System.setOut(new PrintStream(outContent));
206 System.setErr(new PrintStream(errContent));
208 final String[] args =
209 { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
210 final ApexMain apexMain = new ApexMain(args);
212 ThreadUtilities.sleep(1000);
215 final String outString = outContent.toString();
217 System.setOut(stdout);
218 System.setErr(stderr);
220 assertTrue(outString.contains("specified HTTP method of \"POST\" is invalid, "
221 + "only HTTP method \"GET\" is supported for event reception on REST client consumer"));
225 * Test file events bad response.
227 * @throws MessagingException the messaging exception
228 * @throws ApexException the apex exception
229 * @throws IOException Signals that an I/O exception has occurred.
232 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
233 System.setOut(new PrintStream(outContent));
234 System.setErr(new PrintStream(errContent));
236 final String[] args =
237 { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
238 final ApexMain apexMain = new ApexMain(args);
240 ThreadUtilities.sleep(1000);
243 final String outString = outContent.toString();
245 System.setOut(stdout);
246 System.setErr(stderr);
248 assertTrue(outString.contains(
249 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\""));