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;
33 import org.junit.Test;
36 * Test the periodic event manager utility.
38 public class MonitoringRestMainTest {
40 public void testMonitoringClientBad() {
42 final String[] eventArgs = {"-z"};
44 ApexMonitoringRestMain.main(eventArgs);
45 } catch (Exception exc) {
46 fail("test should not throw an exception");
51 public void testMonitoringClientOk() {
53 final String[] eventArgs = {"-t", "1"};
55 ApexMonitoringRestMain.main(eventArgs);
56 } catch (Exception exc) {
57 fail("test should not throw an exception");
62 public void testMonitoringClientNoOptions() {
63 final String[] eventArgs = new String[] {};
65 final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
67 System.err.println(outputString);
68 assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
72 public void testMonitoringClientBadOptions() {
73 final String[] eventArgs = {"-zabbu"};
76 new ApexMonitoringRestMain(eventArgs, System.out);
77 fail("test should throw an exception");
78 } catch (Exception ex) {
80 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
81 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu",
82 ex.getMessage().substring(0, 170));
87 public void testMonitoringClientHelp() {
88 final String[] eventArgs = {"-h"};
91 new ApexMonitoringRestMain(eventArgs, System.out);
92 fail("test should throw an exception");
93 } catch (Exception ex) {
94 assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]",
95 ex.getMessage().substring(0, 86));
100 public void testMonitoringClientPortBad() {
101 final String[] eventArgs = {"-p", "hello"};
104 new ApexMonitoringRestMain(eventArgs, System.out);
105 fail("test should throw an exception");
106 } catch (Exception ex) {
108 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
109 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"",
110 ex.getMessage().substring(0, 156));
115 public void testMonitoringClientPortNegative() {
116 final String[] eventArgs = {"-p", "-1"};
119 new ApexMonitoringRestMain(eventArgs, System.out);
120 fail("test should throw an exception");
121 } catch (Exception ex) {
122 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
123 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
124 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227));
129 public void testMonitoringClientTtlTooSmall() {
130 final String[] eventArgs = {"-t", "-2"};
133 new ApexMonitoringRestMain(eventArgs, System.out);
134 fail("test should throw an exception");
135 } catch (Exception ex) {
137 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
138 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
139 + "time to live must be greater than -1 (set to -1 to wait forever)",
140 ex.getMessage().substring(0, 244));
145 public void testMonitoringClientTooManyPars() {
146 final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
149 new ApexMonitoringRestMain(eventArgs, System.out);
150 fail("test should throw an exception");
151 } catch (Exception ex) {
153 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
154 + "parameter error, too many command line arguments specified : [aaa, bbb]",
155 ex.getMessage().substring(0, 154));
160 public void testMonitoringClientTtlNotNumber() {
161 final String[] eventArgs = {"-t", "timetolive"};
164 new ApexMonitoringRestMain(eventArgs, System.out);
165 fail("test should throw an exception");
166 } catch (Exception ex) {
168 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
169 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"",
170 ex.getMessage().substring(0, 169));
175 public void testMonitoringClientPortTooBig() {
176 final String[] eventArgs = {"-p", "65536"};
179 new ApexMonitoringRestMain(eventArgs, System.out);
180 fail("test should throw an exception");
181 } catch (Exception ex) {
182 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
183 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
184 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230));
189 public void testMonitoringClientDefaultPars() {
191 ApexMonitoringRest monRest = new ApexMonitoringRest();
194 } catch (Exception ex) {
195 fail("test should not throw an exception");
200 public void testMonitoringOneSecStart() {
201 final String[] eventArgs = {"-t", "1"};
204 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
206 monRestMain.shutdown();
208 } catch (Exception ex) {
209 fail("test should not throw an exception");
214 public void testMonitoringForeverStart() {
215 final String[] eventArgs = {"-t", "-1"};
217 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
219 Thread monThread = new Thread() {
228 await().atMost(6, TimeUnit.SECONDS)
229 .until(() -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
230 monRestMain.shutdown();
231 } catch (Exception ex) {
232 fail("test should not throw an exception");
237 * Run the application.
239 * @param eventArgs the command arguments
240 * @return a string containing the command output
242 private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
243 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
244 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
246 new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
248 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
249 System.setIn(testInput);
251 String outString = baosOut.toString();
252 String errString = baosErr.toString();
254 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;