Changes for checkstyle 8.32
[policy/apex-pdp.git] / client / client-editor / src / test / java / org / onap / policy / apex / client / editor / rest / ApexEditorStartupTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.editor.rest;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.Test;
33 import org.onap.policy.apex.client.editor.rest.ApexEditorMain.EditorState;
34
35 /**
36  * Test Apex Editor Startup.
37  */
38 public class ApexEditorStartupTest {
39     // CHECKSTYLE:OFF: MagicNumber
40
41     /**
42      * Test no args.
43      *
44      * @throws IOException Signals that an I/O exception has occurred.
45      * @throws InterruptedException if the test is interrupted
46      */
47     @Test
48     public void testNoArgs() throws IOException, InterruptedException {
49         final String[] args = new String[] {};
50
51         final String outString = runEditor(args);
52         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
53                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec], "
54                 + "State=READY) starting at http://localhost:18989/apexservices/"));
55         assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
56                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec], "
57                 + "State=RUNNING) started at http://localhost:18989/apexservices/"));
58         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
59                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec],"
60                 + " State=STOPPED) shut down "));
61     }
62
63     /**
64      * Test bad arg 0.
65      *
66      * @throws IOException Signals that an I/O exception has occurred.
67      * @throws InterruptedException if the test is interrupted
68      */
69     @Test
70     public void testBadArg0() throws IOException, InterruptedException {
71         final String[] args = new String[] { "12321" };
72
73         try {
74             runEditor(args);
75             fail("test should throw an exception here");
76         } catch (final Exception e) {
77             assertTrue(e.getLocalizedMessage().startsWith(
78                     "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
79                     + " too many command line arguments specified : [12321]"));
80         }
81     }
82
83     /**
84      * Test bad arg 1.
85      *
86      * @throws IOException Signals that an I/O exception has occurred.
87      * @throws InterruptedException if the test is interrupted
88      */
89     @Test
90     public void testBadArg1() throws IOException, InterruptedException {
91         final String[] args = new String[] { "12321 12322 12323" };
92
93         try {
94             runEditor(args);
95             fail("test should throw an exception here");
96         } catch (final Exception e) {
97             assertTrue(e.getLocalizedMessage().startsWith(
98                     "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
99                     + " too many command line arguments specified : [12321 12322 12323]"));
100         }
101     }
102
103     /**
104      * Test bad arg 2.
105      *
106      * @throws IOException Signals that an I/O exception has occurred.
107      * @throws InterruptedException if the test is interrupted
108      */
109     @Test
110     public void testBadArg2() throws IOException, InterruptedException {
111         final String[] args = new String[] { "-z" };
112
113         try {
114             runEditor(args);
115             fail("test should throw an exception here");
116         } catch (final Exception e) {
117             assertTrue(e.getLocalizedMessage().startsWith(
118                     "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
119                     + " invalid command line arguments specified : Unrecognized option: -z"));
120         }
121     }
122
123     /**
124      * Test bad arg 3.
125      *
126      * @throws IOException Signals that an I/O exception has occurred.
127      * @throws InterruptedException if the test is interrupted
128      */
129     @Test
130     public void testBadArg3() throws IOException, InterruptedException {
131         final String[] args = new String[] { "--hello" };
132
133         try {
134             runEditor(args);
135             fail("test should throw an exception here");
136         } catch (final Exception e) {
137             assertTrue(e.getLocalizedMessage().startsWith(
138                     "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
139                     + " invalid command line arguments specified : Unrecognized option: --hello"));
140         }
141     }
142
143
144     /**
145      * Test bad arg 4.
146      *
147      * @throws IOException Signals that an I/O exception has occurred.
148      * @throws InterruptedException if the test is interrupted
149      */
150     @Test
151     public void testBadArg4() throws IOException, InterruptedException {
152         final String[] args = new String[] { "-l", "+++++" };
153
154         try {
155             runEditor(args);
156             fail("test should throw an exception here");
157         } catch (final Exception e) {
158             assertTrue(e.getLocalizedMessage()
159                     .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
160                             + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
161                             + "State=STOPPED) parameters invalid, listen address is not valid. "
162                             + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/"));
163         }
164     }
165
166     /**
167      * Test help 0.
168      *
169      * @throws IOException Signals that an I/O exception has occurred.
170      * @throws InterruptedException if the test is interrupted
171      */
172     @Test
173     public void testHelp0() throws IOException, InterruptedException {
174         final String[] args = new String[] { "--help" };
175
176         try {
177             runEditor(args);
178             fail("test should throw an exception here");
179         } catch (final Exception e) {
180             assertTrue(e.getMessage()
181                     .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
182         }
183     }
184
185     /**
186      * Test help 1.
187      *
188      * @throws IOException Signals that an I/O exception has occurred.
189      * @throws InterruptedException if the test is interrupted
190      */
191     @Test
192     public void testHelp1() throws IOException, InterruptedException {
193         final String[] args = new String[] { "-h" };
194
195         try {
196             runEditor(args);
197             fail("test should throw an exception here");
198         } catch (final Exception e) {
199             assertTrue(e.getMessage()
200                     .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
201         }
202     }
203
204     /**
205      * Test port arg.
206      *
207      * @throws IOException Signals that an I/O exception has occurred.
208      * @throws InterruptedException if the test is interrupted
209      */
210     @Test
211     public void testPortArgShJo() throws IOException, InterruptedException {
212         final String[] args = new String[] { "-p12321" };
213
214         final String outString = runEditor(args);
215
216         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
217                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
218                 + "State=READY) starting at http://localhost:12321/apexservices/"));
219         assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
220                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
221                 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
222         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
223                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
224                 + " State=STOPPED) shut down "));
225     }
226
227     /**
228      * Test port arg.
229      *
230      * @throws IOException Signals that an I/O exception has occurred.
231      * @throws InterruptedException if the test is interrupted
232      */
233     @Test
234     public void testPortArgShSe() throws IOException, InterruptedException {
235         final String[] args = new String[] { "-p", "12321" };
236
237         final String outString = runEditor(args);
238
239         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
240                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
241                 + "State=READY) starting at http://localhost:12321/apexservices/"));
242         assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
243                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
244                 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
245         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("(ApexEditorMain: "
246                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
247                 + " State=STOPPED) shut down "));
248     }
249
250
251     /**
252      * Test port arg.
253      *
254      * @throws IOException Signals that an I/O exception has occurred.
255      * @throws InterruptedException if the test is interrupted
256      */
257     @Test
258     public void testPortArgSpace() throws IOException, InterruptedException {
259         final String[] args = new String[] { "-p 12321" };
260
261         try {
262             runEditor(args);
263             fail("test should throw an exception here");
264         } catch (final Exception e) {
265             assertTrue(e.getMessage().startsWith(
266                     "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
267                     + " error parsing argument \"port\" :For input string: \" 12321\""));
268         }
269     }
270
271     /**
272      * Test bad port arg 0.
273      *
274      * @throws IOException Signals that an I/O exception has occurred.
275      * @throws InterruptedException if the test is interrupted
276      */
277     @Test
278     public void testBadPortArgs0() throws IOException, InterruptedException {
279         final String[] args = new String[] { "-p0" };
280
281         try {
282             runEditor(args);
283             fail("test should throw an exception here");
284         } catch (final Exception e) {
285             assertTrue(e.getMessage()
286                     .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
287                             + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
288                             + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
289         }
290     }
291
292     /**
293      * Test bad port arg 1023.
294      *
295      * @throws IOException Signals that an I/O exception has occurred.
296      * @throws InterruptedException if the test is interrupted
297      */
298     @Test
299     public void testBadPortArgs1023() throws IOException, InterruptedException {
300         final String[] args = new String[] { "-p1023" };
301
302         try {
303             runEditor(args);
304             fail("test should throw an exception here");
305         } catch (final Exception e) {
306             assertTrue(e.getMessage()
307                     .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
308                             + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
309                             + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
310         }
311     }
312
313     /**
314      * Test bad port arg 65536.
315      *
316      * @throws IOException Signals that an I/O exception has occurred.
317      * @throws InterruptedException if the test is interrupted
318      */
319     @Test
320     public void testBadPortArgs65536() throws IOException, InterruptedException {
321         final String[] args = new String[] { "-p65536" };
322
323         try {
324             runEditor(args);
325             fail("test should throw an exception here");
326         } catch (final Exception e) {
327             assertTrue(e.getMessage()
328                     .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
329                             + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
330                             + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
331         }
332     }
333
334     /**
335      * Test TTL arg 0.
336      *
337      * @throws IOException Signals that an I/O exception has occurred.
338      * @throws InterruptedException if the test is interrupted
339      */
340     @Test
341     public void testTtlArg0() throws IOException, InterruptedException {
342         final String[] args = new String[] { "-t10" };
343
344         final String outString = runEditor(args);
345
346         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
347                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
348                 + "State=READY) starting at http://localhost:18989/apexservices/"));
349         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
350                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
351                 + " started"));
352         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
353                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
354                 + " shut down "));
355     }
356
357     /**
358      * Test TTL arg 10.
359      *
360      * @throws IOException Signals that an I/O exception has occurred.
361      * @throws InterruptedException if the test is interrupted
362      */
363     @Test
364     public void testTtlArg1() throws IOException, InterruptedException {
365         final String[] args = new String[] { "-t", "10", "-l", "localhost" };
366
367         final String outString = runEditor(args);
368
369         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
370                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
371                 + "State=READY) starting at http://localhost:18989/apexservices/"));
372         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
373                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
374                 + " started"));
375         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
376                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
377                 + " shut down "));
378     }
379
380     /**
381      * Test port TTL arg 0.
382      *
383      * @throws IOException Signals that an I/O exception has occurred.
384      * @throws InterruptedException if the test is interrupted
385      */
386     @Test
387     public void testPortTtlArg0() throws IOException, InterruptedException {
388         final String[] args = new String[] { "-t", "10", "-p", "12321" };
389
390         final String outString = runEditor(args);
391
392         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
393                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
394                 + "State=READY) starting at http://localhost:12321/apexservices/"));
395         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
396                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
397                 + " started"));
398         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
399                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
400                 + " shut down "));
401     }
402
403
404     /**
405      * Test port TTL arg 10.
406      *
407      * @throws IOException Signals that an I/O exception has occurred.
408      * @throws InterruptedException if the test is interrupted
409      */
410     @Test
411     public void testPortTtlArg1() throws IOException, InterruptedException {
412         final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
413
414         final String outString = runEditor(args);
415
416         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
417                 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
418                 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
419         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
420                 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
421                 + " started"));
422         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
423                 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
424                 + " shut down "));
425     }
426
427     /**
428      * Run the editor for tests.
429      *
430      * @param args the args
431      * @return the output string
432      * @throws InterruptedException if the test is interrupted
433      */
434     private String runEditor(final String[] args) throws InterruptedException {
435         final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
436         final PrintStream outStream = new PrintStream(outBaStream);
437
438         final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
439
440         // This test must be started in a thread because we want to intercept the output in cases where the editor is
441         // started infinitely
442         final Runnable testThread = new Runnable() {
443             @Override
444             public void run() {
445                 editorMain.init();
446             }
447         };
448         new Thread(testThread).start();
449         await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
450                 || editorMain.getState().equals(EditorState.INITIALIZING)));
451         editorMain.shutdown();
452         final String outString = outBaStream.toString();
453         System.out.println(outString);
454         return outString;
455     }
456 }