f27a53adc3094f9d07bcecaca9ea7547f44978d5
[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
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 = {"-z"};
44
45             ApexMonitoringRestMain.main(eventArgs);
46         } catch (Exception exc) {
47             fail("test should not throw an exception");
48         }
49     }
50
51     @Test
52     public void testMonitoringClientOk() {
53         try {
54             final String[] eventArgs = {"-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         final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
67
68         System.err.println(outputString);
69         assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
70     }
71
72     @Test
73     public void testMonitoringClientBadOptions() {
74         final String[] eventArgs = {"-zabbu"};
75
76         try {
77             new ApexMonitoringRestMain(eventArgs, System.out);
78             fail("test should throw an exception");
79         } catch (Exception ex) {
80             assertEquals(
81                 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
82                     + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu",
83                 ex.getMessage().substring(0, 170));
84         }
85     }
86
87     @Test
88     public void testMonitoringClientHelp() {
89         final String[] eventArgs = {"-h"};
90
91         try {
92             new ApexMonitoringRestMain(eventArgs, System.out);
93             fail("test should throw an exception");
94         } catch (Exception ex) {
95             assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]",
96                 ex.getMessage().substring(0, 86));
97         }
98     }
99
100     @Test
101     public void testMonitoringClientPortBad() {
102         final String[] eventArgs = {"-p", "hello"};
103
104         try {
105             new ApexMonitoringRestMain(eventArgs, System.out);
106             fail("test should throw an exception");
107         } catch (Exception ex) {
108             assertEquals(
109                 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
110                     + "parameter error, error parsing argument \"port\" :For input string: \"hello\"",
111                 ex.getMessage().substring(0, 156));
112         }
113     }
114
115     @Test
116     public void testMonitoringClientPortNegative() {
117         final String[] eventArgs = {"-p", "-1"};
118
119         try {
120             new ApexMonitoringRestMain(eventArgs, System.out);
121             fail("test should throw an exception");
122         } catch (Exception ex) {
123             assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
124                 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
125                 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227));
126         }
127     }
128
129     @Test
130     public void testMonitoringClientTtlTooSmall() {
131         final String[] eventArgs = {"-t", "-2"};
132
133         try {
134             new ApexMonitoringRestMain(eventArgs, System.out);
135             fail("test should throw an exception");
136         } catch (Exception ex) {
137             assertEquals(
138                 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
139                     + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
140                     + "time to live must be greater than -1 (set to -1 to wait forever)",
141                 ex.getMessage().substring(0, 244));
142         }
143     }
144
145     @Test
146     public void testMonitoringClientTooManyPars() {
147         final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
148
149         try {
150             new ApexMonitoringRestMain(eventArgs, System.out);
151             fail("test should throw an exception");
152         } catch (Exception ex) {
153             assertEquals(
154                 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
155                     + "parameter error, too many command line arguments specified : [aaa, bbb]",
156                 ex.getMessage().substring(0, 154));
157         }
158     }
159
160     @Test
161     public void testMonitoringClientTtlNotNumber() {
162         final String[] eventArgs = {"-t", "timetolive"};
163
164         try {
165             new ApexMonitoringRestMain(eventArgs, System.out);
166             fail("test should throw an exception");
167         } catch (Exception ex) {
168             assertEquals(
169                 "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
170                     + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"",
171                 ex.getMessage().substring(0, 169));
172         }
173     }
174
175     @Test
176     public void testMonitoringClientPortTooBig() {
177         final String[] eventArgs = {"-p", "65536"};
178
179         try {
180             new ApexMonitoringRestMain(eventArgs, System.out);
181             fail("test should throw an exception");
182         } catch (Exception ex) {
183             assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: "
184                 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
185                 + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230));
186         }
187     }
188
189     @Test
190     public void testMonitoringClientDefaultPars() {
191         try {
192             ApexMonitoringRest monRest = new ApexMonitoringRest();
193             monRest.shutdown();
194
195         } catch (Exception ex) {
196             fail("test should not throw an exception");
197         }
198     }
199
200     @Test
201     public void testMonitoringOneSecStart() {
202         final String[] eventArgs = {"-t", "1"};
203
204         try {
205             ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
206             monRestMain.init();
207             monRestMain.shutdown();
208
209         } catch (Exception ex) {
210             fail("test should not throw an exception");
211         }
212     }
213
214     @Test
215     public void testMonitoringForeverStart() {
216         final String[] eventArgs = {"-t", "-1"};
217
218         ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
219
220         Thread monThread = new Thread() {
221             @Override
222             public void run() {
223                 monRestMain.init();
224             }
225         };
226
227         try {
228             monThread.start();
229             await().atMost(6, TimeUnit.SECONDS)
230                 .until(() -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
231             monRestMain.shutdown();
232         } catch (Exception ex) {
233             fail("test should not throw an exception");
234         }
235     }
236
237     /**
238      * Run the application.
239      *
240      * @param eventArgs the command arguments
241      * @return a string containing the command output
242      */
243     private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
244         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
245         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
246
247         new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
248
249         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
250         System.setIn(testInput);
251
252         String outString = baosOut.toString();
253         String errString = baosErr.toString();
254
255         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
256     }
257 }