0c929f53495699337aee6b15182556a7497c3216
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.monitoring.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.InputStream;
29 import java.io.PrintStream;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
33
34 /**
35  * Test the periodic event manager utility.
36  */
37 public class MonitoringRestMainTest {
38     @Test
39     public void testMonitoringClientBad() {
40         try {
41             final String[] eventArgs =
42                 { "-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 =
54                 { "-t", "1" };
55
56             ApexMonitoringRestMain.main(eventArgs);
57         } catch (Exception exc) {
58             fail("test should not throw an exception");
59         }
60     }
61
62     @Test
63     public void testMonitoringClientNoOptions() {
64         final String[] eventArgs = new String[]
65             {};
66
67         final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
68
69         System.err.println(outputString);
70         assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
71     }
72
73     @Test
74     public void testMonitoringClientBadOptions() {
75         final String[] eventArgs =
76             { "-zabbu" };
77
78         try {
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));
85         }
86     }
87
88     @Test
89     public void testMonitoringClientHelp() {
90         final String[] eventArgs =
91             { "-h" };
92
93         try {
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));
99         }
100     }
101
102     @Test
103     public void testMonitoringClientPortBad() {
104         final String[] eventArgs =
105             { "-p", "hello" };
106
107         try {
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));
114         }
115     }
116
117     @Test
118     public void testMonitoringClientPortNegative() {
119         final String[] eventArgs =
120             { "-p", "-1" };
121
122         try {
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));
129         }
130     }
131
132     @Test
133     public void testMonitoringClientTtlTooSmall() {
134         final String[] eventArgs =
135             { "-t", "-2" };
136
137         try {
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));
145         }
146     }
147
148     @Test
149     public void testMonitoringClientTooManyPars() {
150         final String[] eventArgs =
151             { "-t", "10", "-p", "12344", "aaa", "bbb" };
152
153         try {
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));
160         }
161     }
162
163     @Test
164     public void testMonitoringClientTtlNotNumber() {
165         final String[] eventArgs =
166             { "-t", "timetolive" };
167
168         try {
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));
175         }
176     }
177
178     @Test
179     public void testMonitoringClientPortTooBig() {
180         final String[] eventArgs =
181             { "-p", "65536" };
182
183         try {
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));
190         }
191     }
192
193     @Test
194     public void testMonitoringClientDefaultPars() {
195         try {
196             ApexMonitoringRest monRest = new ApexMonitoringRest();
197             monRest.shutdown();
198
199         } catch (Exception ex) {
200             fail("test should not throw an exception");
201         }
202     }
203
204     @Test
205     public void testMonitoringOneSecStart() {
206         final String[] eventArgs =
207             { "-t", "1" };
208
209         try {
210             ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
211             monRestMain.init();
212             monRestMain.shutdown();
213
214         } catch (Exception ex) {
215             fail("test should not throw an exception");
216         }
217     }
218
219     @Test
220     public void testMonitoringForeverStart() {
221         final String[] eventArgs =
222             { "-t", "-1" };
223
224         ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
225
226         Thread monThread = new Thread() {
227             public void run() {
228                 monRestMain.init();
229             }
230         };
231
232         try {
233             monThread.start();
234             ThreadUtilities.sleep(2000);
235             monRestMain.shutdown();
236         } catch (Exception ex) {
237             fail("test should not throw an exception");
238         }
239     }
240
241     /**
242      * Run the application.
243      * 
244      * @param eventArgs the command arguments
245      * @return a string containing the command output
246      */
247     private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
248         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
249         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
250
251         new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
252
253         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
254         System.setIn(testInput);
255
256         String outString = baosOut.toString();
257         String errString = baosErr.toString();
258
259         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
260     }
261 }