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.deployment.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 DeploymentRestMainTest {
41 private static InputStream systemInStream = System.in;
44 public void testDeploymentClientOk() {
45 final String[] eventArgs = {"-t", "1", "-p", "1256"};
46 assertThatCode(() -> ApexDeploymentRestMain.main(eventArgs)).doesNotThrowAnyException();
50 public void testDeploymentClientNoOptions() {
51 final String[] eventArgs = new String[]
53 assertThat(testApexDeploymentRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
57 public void testDeploymentClientBadOptions() {
58 final String[] eventArgs =
60 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
62 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
63 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
64 + "parameter error, invalid command line arguments specified "
65 + ": Unrecognized option: -zabbu");
69 public void testDeploymentClientHelp() {
70 final String[] eventArgs =
73 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
75 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
76 "usage: org.onap.policy.apex.client.deployment.rest.ApexDeploymentRestMain [options...]");
81 public void testDeploymentClientPortBad() {
82 final String[] eventArgs =
85 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
87 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
88 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
89 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
94 public void testDeploymentClientPortNegative() {
95 final String[] eventArgs =
98 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
100 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
101 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
102 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
103 + "port must be greater than 1023 and less than 65536");
108 public void testDeploymentClientTtlTooSmall() {
109 final String[] eventArgs =
112 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
114 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
115 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
116 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
117 + "time to live must be greater than -1 (set to -1 to wait forever)");
122 public void testDeploymentClientTooManyPars() {
123 final String[] eventArgs =
124 { "-t", "10", "-p", "12344", "aaa", "bbb" };
126 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
128 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
129 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
130 + "parameter error, too many command line arguments specified : [aaa, bbb]");
134 public void testDeploymentClientDefaultPars() {
135 assertThatCode(() -> {
136 ApexDeploymentRest monRest = new ApexDeploymentRest();
138 }).doesNotThrowAnyException();
143 public void testDeploymentClientTtlNotNumber() {
144 final String[] eventArgs =
145 { "-t", "timetolive" };
147 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
149 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
150 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
151 + "parameter error, error parsing argument \"time-to-live\" :"
152 + "For input string: \"timetolive\"");
157 public void testDeploymentClientPortTooBig() {
158 final String[] eventArgs =
161 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
163 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
164 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
165 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
166 + "port must be greater than 1023 and less than 65536");
170 public void testDeploymentOneSecStart() {
171 final String[] eventArgs =
174 assertThatCode(() -> {
175 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
177 monRestMain.shutdown();
178 }).doesNotThrowAnyException();
183 public void testDeploymentForeverStart() {
184 final String[] eventArgs =
187 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
189 Thread monThread = new Thread() {
196 assertThatCode(() -> {
198 await().atMost(2, TimeUnit.SECONDS).until(
199 () -> monRestMain.getState().equals(ApexDeploymentRestMain.ServicesState.RUNNING));
200 monRestMain.shutdown();
201 }).doesNotThrowAnyException();
205 public void cleanUpStreamSetting() {
206 System.setIn(systemInStream);
210 * Run the application.
212 * @param eventArgs the command arguments
213 * @return a string containing the command output
215 private String testApexDeploymentRestMainConstructor(final String[] eventArgs) {
216 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
217 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
219 new ApexDeploymentRestMain(eventArgs, new PrintStream(baosOut, true));
221 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
222 System.setIn(testInput);
224 String outString = baosOut.toString();
225 String errString = baosErr.toString();
227 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;