717da1184be599605fbe9ba8dc07552960395b0a
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.gui.editors.apex.rest;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain.EditorState;
36
37 /**
38  * Test Apex Editor Startup.
39  */
40 public class ApexEditorStartupTest {
41     // CHECKSTYLE:OFF: MagicNumber
42
43     /**
44      * Test no args.
45      *
46      * @throws IOException          Signals that an I/O exception has occurred.
47      * @throws InterruptedException if the test is interrupted
48      */
49     @Test
50     public void testNoArgs() throws IOException, InterruptedException {
51         final String[] args = new String[] {};
52
53         final String outString = runEditor(args);
54
55         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
56         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
57         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("help=false"));
58         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("restPort=18989"));
59         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=-1"));
60         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("listenAddress=localhost"));
61         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("uploadUrl=null"));
62         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("uploadUserid=null"));
63         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
64     }
65
66     /**
67      * Test bad arg 0.
68      *
69      * @throws IOException          Signals that an I/O exception has occurred.
70      * @throws InterruptedException if the test is interrupted
71      */
72     @Test
73     public void testBadArg0() throws IOException, InterruptedException {
74         final String[] args = new String[] { "12321" };
75
76         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
77             .hasMessageContaining("parameter error, too many command line arguments specified : [12321]");
78     }
79
80     /**
81      * Test bad arg 1.
82      *
83      * @throws IOException          Signals that an I/O exception has occurred.
84      * @throws InterruptedException if the test is interrupted
85      */
86     @Test
87     public void testBadArg1() throws IOException, InterruptedException {
88         final String[] args = new String[] { "12321 12322 12323" };
89
90         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
91             .hasMessageContaining("parameter error, too many command line arguments specified : [12321 12322 12323]");
92     }
93
94     /**
95      * Test bad arg 2.
96      *
97      * @throws IOException          Signals that an I/O exception has occurred.
98      * @throws InterruptedException if the test is interrupted
99      */
100     @Test
101     public void testBadArg2() throws IOException, InterruptedException {
102         final String[] args = new String[] { "-z" };
103
104         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class).hasMessageContaining(
105             "parameter error, invalid command line arguments specified : Unrecognized option: -z");
106     }
107
108     /**
109      * Test bad arg 3.
110      *
111      * @throws IOException          Signals that an I/O exception has occurred.
112      * @throws InterruptedException if the test is interrupted
113      */
114     @Test
115     public void testBadArg3() throws IOException, InterruptedException {
116         final String[] args = new String[] { "--hello" };
117
118         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class).hasMessageContaining(
119             "parameter error, invalid command line arguments specified : Unrecognized option: --hello");
120     }
121
122     /**
123      * Test bad arg 4.
124      *
125      * @throws IOException          Signals that an I/O exception has occurred.
126      * @throws InterruptedException if the test is interrupted
127      */
128     @Test
129     public void testBadArg4() throws IOException, InterruptedException {
130         final String[] args = new String[] { "-l", "+++++" };
131
132         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
133             .hasMessageContaining("parameters invalid, listen address is not valid. "
134                 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/");
135     }
136
137     /**
138      * Test help 0.
139      *
140      * @throws IOException          Signals that an I/O exception has occurred.
141      * @throws InterruptedException if the test is interrupted
142      */
143     @Test
144     public void testHelp0() throws IOException, InterruptedException {
145         final String[] args = new String[] { "--help" };
146
147         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
148             .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
149     }
150
151     /**
152      * Test help 1.
153      *
154      * @throws IOException          Signals that an I/O exception has occurred.
155      * @throws InterruptedException if the test is interrupted
156      */
157     @Test
158     public void testHelp1() throws IOException, InterruptedException {
159         final String[] args = new String[] { "-h" };
160
161         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
162             .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
163     }
164
165     /**
166      * Test port arg.
167      *
168      * @throws IOException          Signals that an I/O exception has occurred.
169      * @throws InterruptedException if the test is interrupted
170      */
171     @Test
172     public void testPortArgShJo() throws IOException, InterruptedException {
173         final String[] args = new String[] { "-p12321" };
174
175         final String outString = runEditor(args);
176
177         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
178         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
179         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("localhost:12321"));
180         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
181     }
182
183     /**
184      * Test port arg.
185      *
186      * @throws IOException          Signals that an I/O exception has occurred.
187      * @throws InterruptedException if the test is interrupted
188      */
189     @Test
190     public void testPortArgShSe() throws IOException, InterruptedException {
191         final String[] args = new String[] { "-p", "12321" };
192
193         final String outString = runEditor(args);
194
195         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
196         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
197         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("localhost:12321"));
198         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
199     }
200
201     /**
202      * Test port arg.
203      *
204      * @throws IOException          Signals that an I/O exception has occurred.
205      * @throws InterruptedException if the test is interrupted
206      */
207     @Test
208     public void testPortArgSpace() throws IOException, InterruptedException {
209         final String[] args = new String[] { "-p 12321" };
210
211         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
212             .hasMessageContaining("parameter error, error parsing argument \"port\" :For input string: \" 12321\"");
213     }
214
215     /**
216      * Test bad port arg 0.
217      *
218      * @throws IOException          Signals that an I/O exception has occurred.
219      * @throws InterruptedException if the test is interrupted
220      */
221     @Test
222     public void testBadPortArgs0() throws IOException, InterruptedException {
223         final String[] args = new String[] { "-p0" };
224
225         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
226             .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
227     }
228
229     /**
230      * Test bad port arg 1023.
231      *
232      * @throws IOException          Signals that an I/O exception has occurred.
233      * @throws InterruptedException if the test is interrupted
234      */
235     @Test
236     public void testBadPortArgs1023() throws IOException, InterruptedException {
237         final String[] args = new String[] { "-p1023" };
238
239         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
240             .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
241     }
242
243     /**
244      * Test bad port arg 65536.
245      *
246      * @throws IOException          Signals that an I/O exception has occurred.
247      * @throws InterruptedException if the test is interrupted
248      */
249     @Test
250     public void testBadPortArgs65536() throws IOException, InterruptedException {
251         final String[] args = new String[] { "-p65536" };
252
253         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
254             .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
255     }
256
257     /**
258      * Test TTL arg 0.
259      *
260      * @throws IOException          Signals that an I/O exception has occurred.
261      * @throws InterruptedException if the test is interrupted
262      */
263     @Test
264     public void testTtlArg0() throws IOException, InterruptedException {
265         final String[] args = new String[] { "-t10" };
266
267         final String outString = runEditor(args);
268
269         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
270         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
271         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
272         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
273     }
274
275     /**
276      * Test TTL arg 10.
277      *
278      * @throws IOException          Signals that an I/O exception has occurred.
279      * @throws InterruptedException if the test is interrupted
280      */
281     @Test
282     public void testTtlArg1() throws IOException, InterruptedException {
283         final String[] args = new String[] { "-t", "10", "-l", "localhost" };
284
285         final String outString = runEditor(args);
286
287         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
288         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
289         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
290         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
291     }
292
293     /**
294      * Test port TTL arg 0.
295      *
296      * @throws IOException          Signals that an I/O exception has occurred.
297      * @throws InterruptedException if the test is interrupted
298      */
299     @Test
300     public void testPortTtlArg0() throws IOException, InterruptedException {
301         final String[] args = new String[] { "-t", "10", "-p", "12321" };
302
303         final String outString = runEditor(args);
304
305         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
306         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
307         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
308         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
309     }
310
311     /**
312      * Test port TTL arg 10.
313      *
314      * @throws IOException          Signals that an I/O exception has occurred.
315      * @throws InterruptedException if the test is interrupted
316      */
317     @Test
318     public void testPortTtlArg1() throws IOException, InterruptedException {
319         final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
320
321         final String outString = runEditor(args);
322
323         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
324         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
325         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
326         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
327     }
328
329     /**
330      * Run the editor for tests.
331      *
332      * @param args the args
333      * @return the output string
334      * @throws InterruptedException if the test is interrupted
335      */
336     private String runEditor(final String[] args) throws InterruptedException {
337         ParameterService.clear();
338         final var outBaStream = new ByteArrayOutputStream();
339         final var outStream = new PrintStream(outBaStream);
340
341         final var editorMain = new ApexEditorMain(args, outStream);
342
343         // This test must be started in a thread because we want to intercept the output
344         // in cases where the editor is
345         // started infinitely
346         final var testThread = new Runnable() {
347             @Override
348             public void run() {
349                 editorMain.init();
350             }
351         };
352         new Thread(testThread).start();
353         await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
354             || editorMain.getState().equals(EditorState.INITIALIZING)));
355         editorMain.shutdown();
356         final String outString = outBaStream.toString();
357         System.out.println(outString);
358         return outString;
359     }
360 }