Changes for checkstyle 8.32
[policy/apex-pdp.git] / client / client-monitoring / src / test / java / org / onap / policy / apex / client / monitoring / rest / MonitoringRestMainTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.client.monitoring.rest;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
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;
34
35 /**
36  * Test the periodic event manager utility.
37  */
38 public class MonitoringRestMainTest {
39     @Test
40     public void testMonitoringClientBad() {
41         try {
42             final String[] eventArgs = {"-z"};
43
44             ApexMonitoringRestMain.main(eventArgs);
45         } catch (Exception exc) {
46             fail("test should not throw an exception");
47         }
48     }
49
50     @Test
51     public void testMonitoringClientOk() {
52         try {
53             final String[] eventArgs = {"-t", "1"};
54
55             ApexMonitoringRestMain.main(eventArgs);
56         } catch (Exception exc) {
57             fail("test should not throw an exception");
58         }
59     }
60
61     @Test
62     public void testMonitoringClientNoOptions() {
63         final String[] eventArgs = new String[] {};
64
65         final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
66
67         System.err.println(outputString);
68         assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
69     }
70
71     @Test
72     public void testMonitoringClientBadOptions() {
73         final String[] eventArgs = {"-zabbu"};
74
75         try {
76             new ApexMonitoringRestMain(eventArgs, System.out);
77             fail("test should throw an exception");
78         } catch (Exception ex) {
79             assertEquals(
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));
83         }
84     }
85
86     @Test
87     public void testMonitoringClientHelp() {
88         final String[] eventArgs = {"-h"};
89
90         try {
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));
96         }
97     }
98
99     @Test
100     public void testMonitoringClientPortBad() {
101         final String[] eventArgs = {"-p", "hello"};
102
103         try {
104             new ApexMonitoringRestMain(eventArgs, System.out);
105             fail("test should throw an exception");
106         } catch (Exception ex) {
107             assertEquals(
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));
111         }
112     }
113
114     @Test
115     public void testMonitoringClientPortNegative() {
116         final String[] eventArgs = {"-p", "-1"};
117
118         try {
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));
125         }
126     }
127
128     @Test
129     public void testMonitoringClientTtlTooSmall() {
130         final String[] eventArgs = {"-t", "-2"};
131
132         try {
133             new ApexMonitoringRestMain(eventArgs, System.out);
134             fail("test should throw an exception");
135         } catch (Exception ex) {
136             assertEquals(
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));
141         }
142     }
143
144     @Test
145     public void testMonitoringClientTooManyPars() {
146         final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
147
148         try {
149             new ApexMonitoringRestMain(eventArgs, System.out);
150             fail("test should throw an exception");
151         } catch (Exception ex) {
152             assertEquals(
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));
156         }
157     }
158
159     @Test
160     public void testMonitoringClientTtlNotNumber() {
161         final String[] eventArgs = {"-t", "timetolive"};
162
163         try {
164             new ApexMonitoringRestMain(eventArgs, System.out);
165             fail("test should throw an exception");
166         } catch (Exception ex) {
167             assertEquals(
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));
171         }
172     }
173
174     @Test
175     public void testMonitoringClientPortTooBig() {
176         final String[] eventArgs = {"-p", "65536"};
177
178         try {
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));
185         }
186     }
187
188     @Test
189     public void testMonitoringClientDefaultPars() {
190         try {
191             ApexMonitoringRest monRest = new ApexMonitoringRest();
192             monRest.shutdown();
193
194         } catch (Exception ex) {
195             fail("test should not throw an exception");
196         }
197     }
198
199     @Test
200     public void testMonitoringOneSecStart() {
201         final String[] eventArgs = {"-t", "1"};
202
203         try {
204             ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
205             monRestMain.init();
206             monRestMain.shutdown();
207
208         } catch (Exception ex) {
209             fail("test should not throw an exception");
210         }
211     }
212
213     @Test
214     public void testMonitoringForeverStart() {
215         final String[] eventArgs = {"-t", "-1"};
216
217         ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
218
219         Thread monThread = new Thread() {
220             @Override
221             public void run() {
222                 monRestMain.init();
223             }
224         };
225
226         try {
227             monThread.start();
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");
233         }
234     }
235
236     /**
237      * Run the application.
238      *
239      * @param eventArgs the command arguments
240      * @return a string containing the command output
241      */
242     private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
243         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
244         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
245
246         new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
247
248         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
249         System.setIn(testInput);
250
251         String outString = baosOut.toString();
252         String errString = baosErr.toString();
253
254         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
255     }
256 }