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