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;
33 import org.junit.After;
34 import org.junit.Test;
37 * Test the periodic event manager utility.
39 public class DeploymentRestMainTest {
40 private static InputStream systemInStream = System.in;
43 public void testDeploymentClientOk() {
44 final String[] eventArgs = {"-t", "1", "-p", "1256"};
45 assertThatCode(() -> ApexDeploymentRestMain.main(eventArgs)).doesNotThrowAnyException();
49 public void testDeploymentClientNoOptions() {
50 final String[] eventArgs = new String[] {};
51 assertThat(testApexDeploymentRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
55 public void testDeploymentClientBadOptions() {
56 final String[] eventArgs = {"-zabbu"};
57 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
59 assertThat(thrown).isInstanceOf(Exception.class)
60 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
61 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
65 public void testDeploymentClientHelp() {
66 final String[] eventArgs = {"-h"};
68 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
70 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
71 "usage: org.onap.policy.apex.client.deployment.rest.ApexDeploymentRestMain [options...]");
76 public void testDeploymentClientPortBad() {
77 final String[] eventArgs = {"-p", "hello"};
79 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
81 assertThat(thrown).isInstanceOf(Exception.class)
82 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
83 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
88 public void testDeploymentClientPortNegative() {
89 final String[] eventArgs = {"-p", "-1"};
91 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
93 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
94 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
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 testDeploymentClientTtlTooSmall() {
102 final String[] eventArgs = {"-t", "-2"};
104 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
106 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
107 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
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)");
114 public void testDeploymentClientTooManyPars() {
115 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
117 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
119 assertThat(thrown).isInstanceOf(Exception.class)
120 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
121 + "parameter error, too many command line arguments specified : [aaa, bbb]");
125 public void testDeploymentClientDefaultPars() {
126 assertThatCode(() -> {
127 ApexDeploymentRest monRest = new ApexDeploymentRest();
129 }).doesNotThrowAnyException();
134 public void testDeploymentClientTtlNotNumber() {
135 final String[] eventArgs = {"-t", "timetolive"};
137 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
139 assertThat(thrown).isInstanceOf(Exception.class)
140 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
141 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"");
146 public void testDeploymentClientPortTooBig() {
147 final String[] eventArgs = {"-p", "65536"};
149 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
151 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
152 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
153 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
154 + "port must be greater than 1023 and less than 65536");
158 public void testDeploymentOneSecStart() {
159 final String[] eventArgs = {"-t", "1"};
161 assertThatCode(() -> {
162 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
164 monRestMain.shutdown();
165 }).doesNotThrowAnyException();
170 public void testDeploymentForeverStart() {
171 final String[] eventArgs = {"-t", "-1"};
173 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
175 Thread monThread = new Thread() {
182 assertThatCode(() -> {
184 await().atMost(2, TimeUnit.SECONDS)
185 .until(() -> monRestMain.getState().equals(ApexDeploymentRestMain.ServicesState.RUNNING));
186 monRestMain.shutdown();
187 }).doesNotThrowAnyException();
191 public void cleanUpStreamSetting() {
192 System.setIn(systemInStream);
196 * Run the application.
198 * @param eventArgs the command arguments
199 * @return a string containing the command output
201 private String testApexDeploymentRestMainConstructor(final String[] eventArgs) {
202 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
203 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
205 new ApexDeploymentRestMain(eventArgs, new PrintStream(baosOut, true));
207 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
208 System.setIn(testInput);
210 String outString = baosOut.toString();
211 String errString = baosErr.toString();
213 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;