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