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[] {};
52 assertThat(testApexDeploymentRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
56 public void testDeploymentClientBadOptions() {
57 final String[] eventArgs = {"-zabbu"};
58 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
60 assertThat(thrown).isInstanceOf(Exception.class)
61 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
62 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
66 public void testDeploymentClientHelp() {
67 final String[] eventArgs = {"-h"};
69 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
71 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
72 "usage: org.onap.policy.apex.client.deployment.rest.ApexDeploymentRestMain [options...]");
77 public void testDeploymentClientPortBad() {
78 final String[] eventArgs = {"-p", "hello"};
80 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
82 assertThat(thrown).isInstanceOf(Exception.class)
83 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
84 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
89 public void testDeploymentClientPortNegative() {
90 final String[] eventArgs = {"-p", "-1"};
92 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
94 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
95 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
96 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
97 + "port must be greater than 1023 and less than 65536");
102 public void testDeploymentClientTtlTooSmall() {
103 final String[] eventArgs = {"-t", "-2"};
105 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
107 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
108 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
109 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
110 + "time to live must be greater than -1 (set to -1 to wait forever)");
115 public void testDeploymentClientTooManyPars() {
116 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
118 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
120 assertThat(thrown).isInstanceOf(Exception.class)
121 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
122 + "parameter error, too many command line arguments specified : [aaa, bbb]");
126 public void testDeploymentClientDefaultPars() {
127 assertThatCode(() -> {
128 ApexDeploymentRest monRest = new ApexDeploymentRest();
130 }).doesNotThrowAnyException();
135 public void testDeploymentClientTtlNotNumber() {
136 final String[] eventArgs = {"-t", "timetolive"};
138 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
140 assertThat(thrown).isInstanceOf(Exception.class)
141 .hasMessageContaining("Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
142 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"");
147 public void testDeploymentClientPortTooBig() {
148 final String[] eventArgs = {"-p", "65536"};
150 Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
152 assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
153 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
154 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
155 + "port must be greater than 1023 and less than 65536");
159 public void testDeploymentOneSecStart() {
160 final String[] eventArgs = {"-t", "1"};
162 assertThatCode(() -> {
163 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
165 monRestMain.shutdown();
166 }).doesNotThrowAnyException();
171 public void testDeploymentForeverStart() {
172 final String[] eventArgs = {"-t", "-1"};
174 ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
176 Thread monThread = new Thread() {
183 assertThatCode(() -> {
185 await().atMost(2, TimeUnit.SECONDS)
186 .until(() -> monRestMain.getState().equals(ApexDeploymentRestMain.ServicesState.RUNNING));
187 monRestMain.shutdown();
188 }).doesNotThrowAnyException();
192 public void cleanUpStreamSetting() {
193 System.setIn(systemInStream);
197 * Run the application.
199 * @param eventArgs the command arguments
200 * @return a string containing the command output
202 private String testApexDeploymentRestMainConstructor(final String[] eventArgs) {
203 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
204 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
206 new ApexDeploymentRestMain(eventArgs, new PrintStream(baosOut, true));
208 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
209 System.setIn(testInput);
211 String outString = baosOut.toString();
212 String errString = baosErr.toString();
214 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;