2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.client.monitoring.rest;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.InputStream;
33 import java.io.PrintStream;
34 import java.util.concurrent.TimeUnit;
35 import org.junit.Test;
38 * Test the periodic event manager utility.
40 public class MonitoringRestMainTest {
42 public void testMonitoringClientBad() {
43 final String[] eventArgs = {"-z"};
44 assertThatCode(() -> ApexMonitoringRestMain.main(eventArgs)).doesNotThrowAnyException();
48 public void testMonitoringClientOk() {
49 final String[] eventArgs = {"-t", "1"};
50 assertThatCode(() -> ApexMonitoringRestMain.main(eventArgs)).doesNotThrowAnyException();
54 public void testMonitoringClientNoOptions() {
55 final String[] eventArgs = new String[] {};
57 final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
59 System.err.println(outputString);
60 assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
64 public void testMonitoringClientBadOptions() {
65 final String[] eventArgs = {"-zabbu"};
67 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
68 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
69 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
73 public void testMonitoringClientHelp() {
74 final String[] eventArgs = {"-h"};
76 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
77 .hasMessageContaining("usage: org.onap.policy.apex.client.monitoring.rest."
78 + "ApexMonitoringRestMain [options...]");
82 public void testMonitoringClientPortBad() {
83 final String[] eventArgs = {"-p", "hello"};
85 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
86 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
87 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
91 public void testMonitoringClientPortNegative() {
92 final String[] eventArgs = {"-p", "-1"};
94 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
95 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRest"
96 + "Parameters: URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
97 + "port must be greater than 1023 and less than 65536");
101 public void testMonitoringClientTtlTooSmall() {
102 final String[] eventArgs = {"-t", "-2"};
104 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
105 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRest"
106 + "Parameters: URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
107 + "time to live must be greater than -1 (set to -1 to wait forever)");
111 public void testMonitoringClientTooManyPars() {
112 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
114 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
115 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
116 + "parameter error, too many command line arguments specified : [aaa, bbb]");
120 public void testMonitoringClientTtlNotNumber() {
121 final String[] eventArgs = {"-t", "timetolive"};
123 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
124 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
125 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \""
130 public void testMonitoringClientPortTooBig() {
131 final String[] eventArgs = {"-p", "65536"};
133 assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
134 .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoring"
135 + "RestParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
136 + "port must be greater than 1023 and less than 65536");
140 public void testMonitoringClientDefaultPars() {
141 ApexMonitoringRest monRest = new ApexMonitoringRest();
142 assertNotNull(monRest);
143 assertThatCode(() -> monRest.shutdown()).isNull();
147 public void testMonitoringOneSecStart() {
148 final String[] eventArgs = {"-t", "1"};
150 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
151 assertNotNull(monRestMain);
153 assertThatCode(() -> monRestMain.shutdown()).isNull();
157 public void testMonitoringForeverStart() {
158 final String[] eventArgs = {"-t", "-1"};
160 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
162 Thread monThread = new Thread() {
168 assertThatCode(() -> {
170 await().atMost(6, TimeUnit.SECONDS)
171 .until(() -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
172 monRestMain.shutdown();
173 }).doesNotThrowAnyException();
177 * Run the application.
179 * @param eventArgs the command arguments
180 * @return a string containing the command output
182 private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
183 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
184 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
186 new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
188 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
189 System.setIn(testInput);
191 String outString = baosOut.toString();
192 String errString = baosErr.toString();
194 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;