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