71ca21d2ef6196d4e1d34dbfb38bceca8920d98a
[policy/apex-pdp.git] /
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
34 import org.junit.Test;
35
36 /**
37  * Test the periodic event manager utility.
38  */
39 public class MonitoringRestMainTest {
40     @Test
41     public void testMonitoringClientBad() {
42         try {
43             final String[] eventArgs =
44                 { "-z" };
45
46             ApexMonitoringRestMain.main(eventArgs);
47         } catch (Exception exc) {
48             fail("test should not throw an exception");
49         }
50     }
51
52     @Test
53     public void testMonitoringClientOk() {
54         try {
55             final String[] eventArgs =
56                 { "-t", "1" };
57
58             ApexMonitoringRestMain.main(eventArgs);
59         } catch (Exception exc) {
60             fail("test should not throw an exception");
61         }
62     }
63
64     @Test
65     public void testMonitoringClientNoOptions() {
66         final String[] eventArgs = new String[]
67             {};
68
69         final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
70
71         System.err.println(outputString);
72         assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
73     }
74
75     @Test
76     public void testMonitoringClientBadOptions() {
77         final String[] eventArgs =
78             { "-zabbu" };
79
80         try {
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));
87         }
88     }
89
90     @Test
91     public void testMonitoringClientHelp() {
92         final String[] eventArgs =
93             { "-h" };
94
95         try {
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));
101         }
102     }
103
104     @Test
105     public void testMonitoringClientPortBad() {
106         final String[] eventArgs =
107             { "-p", "hello" };
108
109         try {
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));
116         }
117     }
118
119     @Test
120     public void testMonitoringClientPortNegative() {
121         final String[] eventArgs =
122             { "-p", "-1" };
123
124         try {
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));
131         }
132     }
133
134     @Test
135     public void testMonitoringClientTtlTooSmall() {
136         final String[] eventArgs =
137             { "-t", "-2" };
138
139         try {
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));
147         }
148     }
149
150     @Test
151     public void testMonitoringClientTooManyPars() {
152         final String[] eventArgs =
153             { "-t", "10", "-p", "12344", "aaa", "bbb" };
154
155         try {
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));
162         }
163     }
164
165     @Test
166     public void testMonitoringClientTtlNotNumber() {
167         final String[] eventArgs =
168             { "-t", "timetolive" };
169
170         try {
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));
177         }
178     }
179
180     @Test
181     public void testMonitoringClientPortTooBig() {
182         final String[] eventArgs =
183             { "-p", "65536" };
184
185         try {
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));
192         }
193     }
194
195     @Test
196     public void testMonitoringClientDefaultPars() {
197         try {
198             ApexMonitoringRest monRest = new ApexMonitoringRest();
199             monRest.shutdown();
200
201         } catch (Exception ex) {
202             fail("test should not throw an exception");
203         }
204     }
205
206     @Test
207     public void testMonitoringOneSecStart() {
208         final String[] eventArgs =
209             { "-t", "1" };
210
211         try {
212             ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
213             monRestMain.init();
214             monRestMain.shutdown();
215
216         } catch (Exception ex) {
217             fail("test should not throw an exception");
218         }
219     }
220
221     @Test
222     public void testMonitoringForeverStart() {
223         final String[] eventArgs =
224             { "-t", "-1" };
225
226         ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
227
228         Thread monThread = new Thread() {
229             public void run() {
230                 monRestMain.init();
231             }
232         };
233
234         try {
235             monThread.start();
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");
241         }
242     }
243
244     /**
245      * Run the application.
246      * 
247      * @param eventArgs the command arguments
248      * @return a string containing the command output
249      */
250     private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
251         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
252         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
253
254         new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
255
256         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
257         System.setIn(testInput);
258
259         String outString = baosOut.toString();
260         String errString = baosErr.toString();
261
262         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
263     }
264 }