2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.InputStream;
29 import java.io.PrintStream;
30 import org.junit.After;
31 import org.junit.Test;
32 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35 * Test the periodic event manager utility.
37 public class DeploymentRestMainTest {
38 private static InputStream systemInStream = System.in;
41 public void testDeploymentClientOk() {
42 final String[] eventArgs = {"-t", "1", "-p", "1256"};
43 assertThatCode(() -> ApexDeploymentRestMain.main(eventArgs)).doesNotThrowAnyException();
47 public void testDeploymentClientNoOptions() {
48 final String[] eventArgs = new String[]
50 assertThat(testApexDeploymentRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
54 public void testDeploymentClientBadOptions() {
55 final String[] eventArgs =
57 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
59 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
60 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
61 + "parameter error, invalid command line arguments specified "
62 + ": Unrecognized option: -zabbu");
66 public void testDeploymentClientHelp() {
67 final String[] eventArgs =
70 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
72 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
73 "usage: org.onap.policy.apex.client.deployment.rest.ApexDeploymentRestMain [options...]");
78 public void testDeploymentClientPortBad() {
79 final String[] eventArgs =
82 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
84 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
85 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
86 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
91 public void testDeploymentClientPortNegative() {
92 final String[] eventArgs =
95 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
97 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
98 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
99 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
100 + "port must be greater than 1023 and less than 65536");
105 public void testDeploymentClientTtlTooSmall() {
106 final String[] eventArgs =
109 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
111 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
112 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
113 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
114 + "time to live must be greater than -1 (set to -1 to wait forever)");
119 public void testDeploymentClientTooManyPars() {
120 final String[] eventArgs =
121 { "-t", "10", "-p", "12344", "aaa", "bbb" };
123 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
125 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
126 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
127 + "parameter error, too many command line arguments specified : [aaa, bbb]");
131 public void testDeploymentClientDefaultPars() {
132 assertThatCode(() -> {
133 ApexDeploymentRest monRest = new ApexDeploymentRest();
135 }).doesNotThrowAnyException();
140 public void testDeploymentClientTtlNotNumber() {
141 final String[] eventArgs =
142 { "-t", "timetolive" };
144 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
146 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
147 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
148 + "parameter error, error parsing argument \"time-to-live\" :"
149 + "For input string: \"timetolive\"");
154 public void testDeploymentClientPortTooBig() {
155 final String[] eventArgs =
158 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
160 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
161 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
162 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
163 + "port must be greater than 1023 and less than 65536");
167 public void testDeploymentOneSecStart() {
168 final String[] eventArgs =
171 assertThatCode(() -> {
172 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
174 monRestMain.shutdown();
175 }).doesNotThrowAnyException();
180 public void testDeploymentForeverStart() {
181 final String[] eventArgs =
184 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
186 Thread monThread = new Thread() {
193 assertThatCode(() -> {
195 ThreadUtilities.sleep(2000);
196 monRestMain.shutdown();
197 }).doesNotThrowAnyException();
202 public void cleanUpStreamSetting() {
203 System.setIn(systemInStream);
207 * Run the application.
209 * @param eventArgs the command arguments
210 * @return a string containing the command output
212 private String testApexDeploymentRestMainConstructor(final String[] eventArgs) {
213 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
214 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
216 new ApexDeploymentRestMain(eventArgs, new PrintStream(baosOut, true));
218 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
219 System.setIn(testInput);
221 String outString = baosOut.toString();
222 String errString = baosErr.toString();
224 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;