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.apps.uservice.test.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 public class TestREST2File {
47 private static final String BASE_URI = "http://localhost:32801/TestRest2File";
48 private HttpServer server;
50 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
51 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
53 private final PrintStream stdout = System.out;
54 private final PrintStream stderr = System.err;
57 public void setUp() throws Exception {
58 final ResourceConfig rc = new ResourceConfig(TestRESTClientEndpoint.class);
59 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
61 while (!server.isStarted()) {
62 ThreadUtilities.sleep(50);
67 public void tearDown() throws Exception {
72 public static void deleteTempFiles() {
73 new File("src/test/resources/events/EventsOut.json").delete();
77 public void testRESTEventsIn() throws MessagingException, ApexException, IOException {
78 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEvent.json"};
80 final ApexMain apexMain = new ApexMain(args);
82 ThreadUtilities.sleep(1000);
85 final String outputEventText = TextFileUtils.getTextFileAsString("src/test/resources/events/EventsOut.json");
86 assertTrue(outputEventText.contains(
87 "04\",\n" + " \"version\": \"0.0.1\",\n" + " \"nameSpace\": \"org.onap.policy.apex.sample.events\""));
91 public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
92 System.setOut(new PrintStream(outContent));
93 System.setErr(new PrintStream(errContent));
95 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEmptyEvents.json"};
96 final ApexMain apexMain = new ApexMain(args);
98 ThreadUtilities.sleep(1000);
101 final String outString = outContent.toString();
103 System.setOut(stdout);
104 System.setErr(stderr);
106 assertTrue(outString.contains(
107 "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\""));
111 public void testFileEventsNoURL() throws MessagingException, ApexException, IOException {
112 System.setOut(new PrintStream(outContent));
113 System.setErr(new PrintStream(errContent));
115 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventNoURL.json"};
116 final ApexMain apexMain = new ApexMain(args);
118 ThreadUtilities.sleep(1000);
121 final String outString = outContent.toString();
123 System.setOut(stdout);
124 System.setErr(stderr);
126 assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
130 public void testFileEventsBadURL() throws MessagingException, ApexException, IOException {
131 System.setOut(new PrintStream(outContent));
132 System.setErr(new PrintStream(errContent));
134 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadURL.json"};
135 final ApexMain apexMain = new ApexMain(args);
137 ThreadUtilities.sleep(1000);
140 final String outString = outContent.toString();
142 System.setOut(stdout);
143 System.setErr(stderr);
145 assertTrue(outString.contains(
146 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404"));
150 public void testFileEventsBadHTTPMethod() throws MessagingException, ApexException, IOException {
151 System.setOut(new PrintStream(outContent));
152 System.setErr(new PrintStream(errContent));
154 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.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(
166 "specified HTTP method of \"POST\" is invalid, only HTTP method \"GET\" is supported for event reception on REST client consumer"));
170 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
171 System.setOut(new PrintStream(outContent));
172 System.setErr(new PrintStream(errContent));
174 final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadResponse.json"};
175 final ApexMain apexMain = new ApexMain(args);
177 ThreadUtilities.sleep(1000);
180 final String outString = outContent.toString();
182 System.setOut(stdout);
183 System.setErr(stderr);
185 assertTrue(outString.contains(
186 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\""));