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;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.InputStream;
30 import java.io.PrintStream;
31 import org.junit.After;
32 import org.junit.Test;
33 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 * Test the periodic event manager utility.
38 public class DeploymentRestMainTest {
39 private static InputStream systemInStream = System.in;
42 public void testDeploymentClientOk() {
43 final String[] eventArgs = {"-t", "1", "-p", "1256"};
44 assertThatCode(() -> ApexDeploymentRestMain.main(eventArgs)).doesNotThrowAnyException();
48 public void testDeploymentClientNoOptions() {
49 final String[] eventArgs = new String[]
51 assertThat(testApexDeploymentRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
55 public void testDeploymentClientBadOptions() {
56 final String[] eventArgs =
58 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
60 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
61 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
62 + "parameter error, invalid command line arguments specified "
63 + ": Unrecognized option: -zabbu");
67 public void testDeploymentClientHelp() {
68 final String[] eventArgs =
71 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
73 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
74 "usage: org.onap.policy.apex.client.deployment.rest.ApexDeploymentRestMain [options...]");
79 public void testDeploymentClientPortBad() {
80 final String[] eventArgs =
83 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
85 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
86 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
87 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
92 public void testDeploymentClientPortNegative() {
93 final String[] eventArgs =
96 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
98 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
99 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
100 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
101 + "port must be greater than 1023 and less than 65536");
106 public void testDeploymentClientTtlTooSmall() {
107 final String[] eventArgs =
110 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
112 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
113 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
114 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
115 + "time to live must be greater than -1 (set to -1 to wait forever)");
120 public void testDeploymentClientTooManyPars() {
121 final String[] eventArgs =
122 { "-t", "10", "-p", "12344", "aaa", "bbb" };
124 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
126 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
127 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
128 + "parameter error, too many command line arguments specified : [aaa, bbb]");
132 public void testDeploymentClientDefaultPars() {
133 assertThatCode(() -> {
134 ApexDeploymentRest monRest = new ApexDeploymentRest();
136 }).doesNotThrowAnyException();
141 public void testDeploymentClientTtlNotNumber() {
142 final String[] eventArgs =
143 { "-t", "timetolive" };
145 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
147 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
148 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
149 + "parameter error, error parsing argument \"time-to-live\" :"
150 + "For input string: \"timetolive\"");
155 public void testDeploymentClientPortTooBig() {
156 final String[] eventArgs =
159 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
161 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
162 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
163 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
164 + "port must be greater than 1023 and less than 65536");
168 public void testDeploymentOneSecStart() {
169 final String[] eventArgs =
172 assertThatCode(() -> {
173 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
175 monRestMain.shutdown();
176 }).doesNotThrowAnyException();
181 public void testDeploymentForeverStart() {
182 final String[] eventArgs =
185 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
187 Thread monThread = new Thread() {
194 assertThatCode(() -> {
196 ThreadUtilities.sleep(2000);
197 monRestMain.shutdown();
198 }).doesNotThrowAnyException();
203 public void cleanUpStreamSetting() {
204 System.setIn(systemInStream);
208 * Run the application.
210 * @param eventArgs the command arguments
211 * @return a string containing the command output
213 private String testApexDeploymentRestMainConstructor(final String[] eventArgs) {
214 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
215 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
217 new ApexDeploymentRestMain(eventArgs, new PrintStream(baosOut, true));
219 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
220 System.setIn(testInput);
222 String outString = baosOut.toString();
223 String errString = baosErr.toString();
225 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;