2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
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.client.full.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.catchThrowable;
26 import static org.awaitility.Awaitility.await;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.InputStream;
31 import java.io.PrintStream;
32 import java.util.concurrent.TimeUnit;
34 import org.junit.After;
35 import org.junit.Test;
38 * Test the periodic event manager utility.
40 public class ServicesRestMainTest {
41 private static InputStream systemInStream = System.in;
44 public void testServicesMainClientOk() {
45 final String[] eventArgs = {"-t", "1", "-p", "1256"};
46 assertThatCode(() -> ApexServicesRestMain.main(eventArgs)).doesNotThrowAnyException();
50 public void testServicesClientNoOptions() {
51 final String[] eventArgs = new String[] {};
53 assertThat(testApexServicesRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
58 public void testServicesClientBadOptions() {
59 final String[] eventArgs = {"-zabbu"};
60 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
62 assertThat(thrown).isInstanceOf(Exception.class)
63 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
64 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
68 public void testServicesClientHelp() {
69 final String[] eventArgs = {"-h"};
70 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
72 assertThat(thrown).isInstanceOf(Exception.class)
73 .hasMessageContaining("usage: org.onap.policy.apex.client.full.rest.ApexServicesRestMain [options...]");
77 public void testServicesClientPortBad() {
78 final String[] eventArgs = {"-p", "hello"};
80 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
82 assertThat(thrown).isInstanceOf(Exception.class)
83 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
84 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
89 public void testServicesClientPortNegative() {
90 final String[] eventArgs = {"-p", "-1"};
92 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
94 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
95 "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
96 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
97 + "port must be greater than 1023 and less than 65536");
102 public void testServicesClientTtlTooSmall() {
103 final String[] eventArgs = {"-t", "-2"};
105 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
107 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
108 "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
109 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
110 + "time to live must be greater than -1 (set to -1 to wait forever)");
114 public void testServicesClientTooManyPars() {
115 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
116 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
118 assertThat(thrown).isInstanceOf(Exception.class)
119 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
120 + "parameter error, too many command line arguments specified : [aaa, bbb]");
124 public void testServicesClientTtlNotNumber() {
125 final String[] eventArgs = {"-t", "timetolive"};
126 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
128 assertThat(thrown).isInstanceOf(Exception.class)
129 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
130 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"");
134 public void testServicesClientTtlSetValue() {
135 final String[] eventArgs = {"-t", "3", "-p", "1257"};
136 assertThatCode(() -> {
137 ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
139 monRestMain.shutdown();
140 }).doesNotThrowAnyException();
145 public void testServicesClientPortTooBig() {
146 final String[] eventArgs = {"-p", "65536"};
147 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
149 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
150 "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
151 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
152 + "port must be greater than 1023 and less than 65536");
156 public void testServicesOneSecStart() {
157 final String[] eventArgs = {"-t", "1", "-p", "1258"};
159 assertThatCode(() -> {
160 ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
162 monRestMain.shutdown();
163 }).doesNotThrowAnyException();
167 public void testServicesForeverStart() {
168 final String[] eventArgs = {"-t", "-1", "-p", "1259"};
170 ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
172 Thread monThread = new Thread() {
179 assertThatCode(() -> {
181 await().atMost(6, TimeUnit.SECONDS)
182 .until(() -> monRestMain.getState().equals(ApexServicesRestMain.EditorState.RUNNING));
183 monRestMain.shutdown();
184 }).doesNotThrowAnyException();
188 public void cleanUpStreamSetting() {
189 System.setIn(systemInStream);
193 * Run the application.
195 * @param eventArgs the command arguments
196 * @return a string containing the command output
198 private String testApexServicesRestMainConstructor(final String[] eventArgs) {
199 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
200 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
202 new ApexServicesRestMain(eventArgs, new PrintStream(baosOut, true));
204 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
205 System.setIn(testInput);
207 String outString = baosOut.toString();
208 String errString = baosErr.toString();
210 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;