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 = {"-z"};
45 ApexMonitoringRestMain.main(eventArgs);
46 } catch (Exception exc) {
47 fail("test should not throw an exception");
52 public void testMonitoringClientOk() {
54 final String[] eventArgs = {"-t", "1"};
56 ApexMonitoringRestMain.main(eventArgs);
57 } catch (Exception exc) {
58 fail("test should not throw an exception");
63 public void testMonitoringClientNoOptions() {
64 final String[] eventArgs = new String[] {};
66 final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
68 System.err.println(outputString);
69 assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
73 public void testMonitoringClientBadOptions() {
74 final String[] eventArgs = {"-zabbu"};
77 new ApexMonitoringRestMain(eventArgs, System.out);
78 fail("test should throw an exception");
79 } catch (Exception ex) {
81 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
82 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu",
83 ex.getMessage().substring(0, 170));
88 public void testMonitoringClientHelp() {
89 final String[] eventArgs = {"-h"};
92 new ApexMonitoringRestMain(eventArgs, System.out);
93 fail("test should throw an exception");
94 } catch (Exception ex) {
95 assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]",
96 ex.getMessage().substring(0, 86));
101 public void testMonitoringClientPortBad() {
102 final String[] eventArgs = {"-p", "hello"};
105 new ApexMonitoringRestMain(eventArgs, System.out);
106 fail("test should throw an exception");
107 } catch (Exception ex) {
109 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
110 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"",
111 ex.getMessage().substring(0, 156));
116 public void testMonitoringClientPortNegative() {
117 final String[] eventArgs = {"-p", "-1"};
120 new ApexMonitoringRestMain(eventArgs, System.out);
121 fail("test should throw an exception");
122 } catch (Exception ex) {
123 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
124 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
125 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227));
130 public void testMonitoringClientTtlTooSmall() {
131 final String[] eventArgs = {"-t", "-2"};
134 new ApexMonitoringRestMain(eventArgs, System.out);
135 fail("test should throw an exception");
136 } catch (Exception ex) {
138 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
139 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
140 + "time to live must be greater than -1 (set to -1 to wait forever)",
141 ex.getMessage().substring(0, 244));
146 public void testMonitoringClientTooManyPars() {
147 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
150 new ApexMonitoringRestMain(eventArgs, System.out);
151 fail("test should throw an exception");
152 } catch (Exception ex) {
154 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
155 + "parameter error, too many command line arguments specified : [aaa, bbb]",
156 ex.getMessage().substring(0, 154));
161 public void testMonitoringClientTtlNotNumber() {
162 final String[] eventArgs = {"-t", "timetolive"};
165 new ApexMonitoringRestMain(eventArgs, System.out);
166 fail("test should throw an exception");
167 } catch (Exception ex) {
169 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
170 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"",
171 ex.getMessage().substring(0, 169));
176 public void testMonitoringClientPortTooBig() {
177 final String[] eventArgs = {"-p", "65536"};
180 new ApexMonitoringRestMain(eventArgs, System.out);
181 fail("test should throw an exception");
182 } catch (Exception ex) {
183 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
184 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
185 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230));
190 public void testMonitoringClientDefaultPars() {
192 ApexMonitoringRest monRest = new ApexMonitoringRest();
195 } catch (Exception ex) {
196 fail("test should not throw an exception");
201 public void testMonitoringOneSecStart() {
202 final String[] eventArgs = {"-t", "1"};
205 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
207 monRestMain.shutdown();
209 } catch (Exception ex) {
210 fail("test should not throw an exception");
215 public void testMonitoringForeverStart() {
216 final String[] eventArgs = {"-t", "-1"};
218 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
220 Thread monThread = new Thread() {
229 await().atMost(6, TimeUnit.SECONDS)
230 .until(() -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
231 monRestMain.shutdown();
232 } catch (Exception ex) {
233 fail("test should not throw an exception");
238 * Run the application.
240 * @param eventArgs the command arguments
241 * @return a string containing the command output
243 private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
244 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
245 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
247 new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
249 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
250 System.setIn(testInput);
252 String outString = baosOut.toString();
253 String errString = baosErr.toString();
255 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;