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.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
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.Test;
37 * Test the periodic event manager utility.
39 public class MonitoringRestMainTest {
41 public void testMonitoringClientBad() {
43 final String[] eventArgs =
46 ApexMonitoringRestMain.main(eventArgs);
47 } catch (Exception exc) {
48 fail("test should not throw an exception");
53 public void testMonitoringClientOk() {
55 final String[] eventArgs =
58 ApexMonitoringRestMain.main(eventArgs);
59 } catch (Exception exc) {
60 fail("test should not throw an exception");
65 public void testMonitoringClientNoOptions() {
66 final String[] eventArgs = new String[]
69 final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
71 System.err.println(outputString);
72 assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
76 public void testMonitoringClientBadOptions() {
77 final String[] eventArgs =
81 new ApexMonitoringRestMain(eventArgs, System.out);
82 fail("test should throw an exception");
83 } catch (Exception ex) {
84 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
85 + "parameter error, invalid command line arguments specified "
86 + ": Unrecognized option: -zabbu", ex.getMessage().substring(0, 170));
91 public void testMonitoringClientHelp() {
92 final String[] eventArgs =
96 new ApexMonitoringRestMain(eventArgs, System.out);
97 fail("test should throw an exception");
98 } catch (Exception ex) {
99 assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]",
100 ex.getMessage().substring(0, 86));
105 public void testMonitoringClientPortBad() {
106 final String[] eventArgs =
110 new ApexMonitoringRestMain(eventArgs, System.out);
111 fail("test should throw an exception");
112 } catch (Exception ex) {
113 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
114 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"",
115 ex.getMessage().substring(0, 156));
120 public void testMonitoringClientPortNegative() {
121 final String[] eventArgs =
125 new ApexMonitoringRestMain(eventArgs, System.out);
126 fail("test should throw an exception");
127 } catch (Exception ex) {
128 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
129 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
130 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227));
135 public void testMonitoringClientTtlTooSmall() {
136 final String[] eventArgs =
140 new ApexMonitoringRestMain(eventArgs, System.out);
141 fail("test should throw an exception");
142 } catch (Exception ex) {
143 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
144 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
145 + "time to live must be greater than -1 (set to -1 to wait forever)",
146 ex.getMessage().substring(0, 244));
151 public void testMonitoringClientTooManyPars() {
152 final String[] eventArgs =
153 { "-t", "10", "-p", "12344", "aaa", "bbb" };
156 new ApexMonitoringRestMain(eventArgs, System.out);
157 fail("test should throw an exception");
158 } catch (Exception ex) {
159 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
160 + "parameter error, too many command line arguments specified : [aaa, bbb]",
161 ex.getMessage().substring(0, 154));
166 public void testMonitoringClientTtlNotNumber() {
167 final String[] eventArgs =
168 { "-t", "timetolive" };
171 new ApexMonitoringRestMain(eventArgs, System.out);
172 fail("test should throw an exception");
173 } catch (Exception ex) {
174 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
175 + "parameter error, error parsing argument \"time-to-live\" :"
176 + "For input string: \"timetolive\"", ex.getMessage().substring(0, 169));
181 public void testMonitoringClientPortTooBig() {
182 final String[] eventArgs =
186 new ApexMonitoringRestMain(eventArgs, System.out);
187 fail("test should throw an exception");
188 } catch (Exception ex) {
189 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
190 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
191 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230));
196 public void testMonitoringClientDefaultPars() {
198 ApexMonitoringRest monRest = new ApexMonitoringRest();
201 } catch (Exception ex) {
202 fail("test should not throw an exception");
207 public void testMonitoringOneSecStart() {
208 final String[] eventArgs =
212 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
214 monRestMain.shutdown();
216 } catch (Exception ex) {
217 fail("test should not throw an exception");
222 public void testMonitoringForeverStart() {
223 final String[] eventArgs =
226 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
228 Thread monThread = new Thread() {
236 await().atMost(6, TimeUnit.SECONDS).until(
237 () -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
238 monRestMain.shutdown();
239 } catch (Exception ex) {
240 fail("test should not throw an exception");
245 * Run the application.
247 * @param eventArgs the command arguments
248 * @return a string containing the command output
250 private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
251 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
252 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
254 new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
256 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
257 System.setIn(testInput);
259 String outString = baosOut.toString();
260 String errString = baosErr.toString();
262 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;