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;
33 import org.junit.After;
34 import org.junit.Test;
37 * Test the periodic event manager utility.
39 public class ServicesRestMainTest {
40 private static InputStream systemInStream = System.in;
43 public void testServicesMainClientOk() {
44 final String[] eventArgs = {"-t", "1", "-p", "1256"};
45 assertThatCode(() -> ApexServicesRestMain.main(eventArgs)).doesNotThrowAnyException();
49 public void testServicesClientNoOptions() {
50 final String[] eventArgs = new String[] {};
52 assertThat(testApexServicesRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
57 public void testServicesClientBadOptions() {
58 final String[] eventArgs = {"-zabbu"};
59 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
61 assertThat(thrown).isInstanceOf(Exception.class)
62 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
63 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
67 public void testServicesClientHelp() {
68 final String[] eventArgs = {"-h"};
69 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
71 assertThat(thrown).isInstanceOf(Exception.class)
72 .hasMessageContaining("usage: org.onap.policy.apex.client.full.rest.ApexServicesRestMain [options...]");
76 public void testServicesClientPortBad() {
77 final String[] eventArgs = {"-p", "hello"};
79 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
81 assertThat(thrown).isInstanceOf(Exception.class)
82 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
83 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
88 public void testServicesClientPortNegative() {
89 final String[] eventArgs = {"-p", "-1"};
91 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
93 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
94 "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
95 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
96 + "port must be greater than 1023 and less than 65536");
101 public void testServicesClientTtlTooSmall() {
102 final String[] eventArgs = {"-t", "-2"};
104 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
106 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
107 "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
108 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
109 + "time to live must be greater than -1 (set to -1 to wait forever)");
113 public void testServicesClientTooManyPars() {
114 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
115 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
117 assertThat(thrown).isInstanceOf(Exception.class)
118 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
119 + "parameter error, too many command line arguments specified : [aaa, bbb]");
123 public void testServicesClientTtlNotNumber() {
124 final String[] eventArgs = {"-t", "timetolive"};
125 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
127 assertThat(thrown).isInstanceOf(Exception.class)
128 .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
129 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"");
133 public void testServicesClientTtlSetValue() {
134 final String[] eventArgs = {"-t", "3", "-p", "1257"};
135 assertThatCode(() -> {
136 ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
138 monRestMain.shutdown();
139 }).doesNotThrowAnyException();
144 public void testServicesClientPortTooBig() {
145 final String[] eventArgs = {"-p", "65536"};
146 Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
148 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
149 "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
150 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
151 + "port must be greater than 1023 and less than 65536");
155 public void testServicesOneSecStart() {
156 final String[] eventArgs = {"-t", "1", "-p", "1258"};
158 assertThatCode(() -> {
159 ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
161 monRestMain.shutdown();
162 }).doesNotThrowAnyException();
166 public void testServicesForeverStart() {
167 final String[] eventArgs = {"-t", "-1", "-p", "1259"};
169 ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
171 Thread monThread = new Thread() {
178 assertThatCode(() -> {
180 await().atMost(6, TimeUnit.SECONDS)
181 .until(() -> monRestMain.getState().equals(ApexServicesRestMain.EditorState.RUNNING));
182 monRestMain.shutdown();
183 }).doesNotThrowAnyException();
187 public void cleanUpStreamSetting() {
188 System.setIn(systemInStream);
192 * Run the application.
194 * @param eventArgs the command arguments
195 * @return a string containing the command output
197 private String testApexServicesRestMainConstructor(final String[] eventArgs) {
198 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
199 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
201 new ApexServicesRestMain(eventArgs, new PrintStream(baosOut, true));
203 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
204 System.setIn(testInput);
206 String outString = baosOut.toString();
207 String errString = baosErr.toString();
209 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;