2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
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.monitoring.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.InputStream;
29 import java.io.PrintStream;
31 import org.junit.Test;
32 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35 * Test the periodic event manager utility.
37 public class MonitoringRestMainTest {
39 public void testMonitoringClientBad() {
41 final String[] eventArgs =
44 ApexMonitoringRestMain.main(eventArgs);
45 } catch (Exception exc) {
46 fail("test should not throw an exception");
51 public void testMonitoringClientOk() {
53 final String[] eventArgs =
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[]
67 final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
69 System.err.println(outputString);
70 assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
74 public void testMonitoringClientBadOptions() {
75 final String[] eventArgs =
79 new ApexMonitoringRestMain(eventArgs, System.out);
80 fail("test should throw an exception");
81 } catch (Exception ex) {
82 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
83 + "parameter error, invalid command line arguments specified "
84 + ": Unrecognized option: -zabbu", ex.getMessage().substring(0, 170));
89 public void testMonitoringClientHelp() {
90 final String[] eventArgs =
94 new ApexMonitoringRestMain(eventArgs, System.out);
95 fail("test should throw an exception");
96 } catch (Exception ex) {
97 assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]",
98 ex.getMessage().substring(0, 86));
103 public void testMonitoringClientPortBad() {
104 final String[] eventArgs =
108 new ApexMonitoringRestMain(eventArgs, System.out);
109 fail("test should throw an exception");
110 } catch (Exception ex) {
111 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
112 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"",
113 ex.getMessage().substring(0, 156));
118 public void testMonitoringClientPortNegative() {
119 final String[] eventArgs =
123 new ApexMonitoringRestMain(eventArgs, System.out);
124 fail("test should throw an exception");
125 } catch (Exception ex) {
126 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
127 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
128 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227));
133 public void testMonitoringClientTtlTooSmall() {
134 final String[] eventArgs =
138 new ApexMonitoringRestMain(eventArgs, System.out);
139 fail("test should throw an exception");
140 } catch (Exception ex) {
141 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
142 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
143 + "time to live must be greater than -1 (set to -1 to wait forever)",
144 ex.getMessage().substring(0, 244));
149 public void testMonitoringClientTooManyPars() {
150 final String[] eventArgs =
151 { "-t", "10", "-p", "12344", "aaa", "bbb" };
154 new ApexMonitoringRestMain(eventArgs, System.out);
155 fail("test should throw an exception");
156 } catch (Exception ex) {
157 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
158 + "parameter error, too many command line arguments specified : [aaa, bbb]",
159 ex.getMessage().substring(0, 154));
164 public void testMonitoringClientTtlNotNumber() {
165 final String[] eventArgs =
166 { "-t", "timetolive" };
169 new ApexMonitoringRestMain(eventArgs, System.out);
170 fail("test should throw an exception");
171 } catch (Exception ex) {
172 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
173 + "parameter error, error parsing argument \"time-to-live\" :"
174 + "For input string: \"timetolive\"", ex.getMessage().substring(0, 169));
179 public void testMonitoringClientPortTooBig() {
180 final String[] eventArgs =
184 new ApexMonitoringRestMain(eventArgs, System.out);
185 fail("test should throw an exception");
186 } catch (Exception ex) {
187 assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
188 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
189 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230));
194 public void testMonitoringClientDefaultPars() {
196 ApexMonitoringRest monRest = new ApexMonitoringRest();
199 } catch (Exception ex) {
200 fail("test should not throw an exception");
205 public void testMonitoringOneSecStart() {
206 final String[] eventArgs =
210 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
212 monRestMain.shutdown();
214 } catch (Exception ex) {
215 fail("test should not throw an exception");
220 public void testMonitoringForeverStart() {
221 final String[] eventArgs =
224 ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
226 Thread monThread = new Thread() {
234 ThreadUtilities.sleep(2000);
235 monRestMain.shutdown();
236 } catch (Exception ex) {
237 fail("test should not throw an exception");
242 * Run the application.
244 * @param eventArgs the command arguments
245 * @return a string containing the command output
247 private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
248 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
249 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
251 new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
253 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
254 System.setIn(testInput);
256 String outString = baosOut.toString();
257 String errString = baosErr.toString();
259 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;