c80b8163b318af51e0e6fc8d749fc90fa0423e6f
[policy/gui.git] /
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.gui.editors.apex.rest;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertTrue;
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.common.parameters.ParameterService;
34 import org.onap.policy.gui.editors.apex.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]+", " ")
60             .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
61                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec],"
62                 + " State=STOPPED) shut down "));
63     }
64
65     /**
66      * Test bad arg 0.
67      *
68      * @throws IOException          Signals that an I/O exception has occurred.
69      * @throws InterruptedException if the test is interrupted
70      */
71     @Test
72     public void testBadArg0() throws IOException, InterruptedException {
73         final String[] args = new String[] { "12321" };
74
75         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
76             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
77                 + " 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("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
92                 + " parameter error, too many command line arguments specified : [12321 12322 12323]");
93     }
94
95     /**
96      * Test bad arg 2.
97      *
98      * @throws IOException          Signals that an I/O exception has occurred.
99      * @throws InterruptedException if the test is interrupted
100      */
101     @Test
102     public void testBadArg2() throws IOException, InterruptedException {
103         final String[] args = new String[] { "-z" };
104
105         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
106             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
107                 + " parameter error, invalid command line arguments specified : Unrecognized option: -z");
108     }
109
110     /**
111      * Test bad arg 3.
112      *
113      * @throws IOException          Signals that an I/O exception has occurred.
114      * @throws InterruptedException if the test is interrupted
115      */
116     @Test
117     public void testBadArg3() throws IOException, InterruptedException {
118         final String[] args = new String[] { "--hello" };
119
120         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
121             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
122                 + " parameter error, invalid command line arguments specified : Unrecognized option: --hello");
123     }
124
125     /**
126      * Test bad arg 4.
127      *
128      * @throws IOException          Signals that an I/O exception has occurred.
129      * @throws InterruptedException if the test is interrupted
130      */
131     @Test
132     public void testBadArg4() throws IOException, InterruptedException {
133         final String[] args = new String[] { "-l", "+++++" };
134
135         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
136             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
137                 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
138                 + "State=STOPPED) parameters invalid, listen address is not valid. "
139                 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/");
140     }
141
142     /**
143      * Test help 0.
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 testHelp0() throws IOException, InterruptedException {
150         final String[] args = new String[] { "--help" };
151
152         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
153             .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
154     }
155
156     /**
157      * Test help 1.
158      *
159      * @throws IOException          Signals that an I/O exception has occurred.
160      * @throws InterruptedException if the test is interrupted
161      */
162     @Test
163     public void testHelp1() throws IOException, InterruptedException {
164         final String[] args = new String[] { "-h" };
165
166         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
167             .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
168     }
169
170     /**
171      * Test port arg.
172      *
173      * @throws IOException          Signals that an I/O exception has occurred.
174      * @throws InterruptedException if the test is interrupted
175      */
176     @Test
177     public void testPortArgShJo() throws IOException, InterruptedException {
178         final String[] args = new String[] { "-p12321" };
179
180         final String outString = runEditor(args);
181
182         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
183             + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
184             + "State=READY) starting at http://localhost:12321/apexservices/"));
185         assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
186             + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
187             + "State=RUNNING) started at http://localhost:12321/apexservices/"));
188         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
189             .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
190                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
191                 + " State=STOPPED) shut down "));
192     }
193
194     /**
195      * Test port arg.
196      *
197      * @throws IOException          Signals that an I/O exception has occurred.
198      * @throws InterruptedException if the test is interrupted
199      */
200     @Test
201     public void testPortArgShSe() throws IOException, InterruptedException {
202         final String[] args = new String[] { "-p", "12321" };
203
204         final String outString = runEditor(args);
205
206         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
207             + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
208             + "State=READY) starting at http://localhost:12321/apexservices/"));
209         assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
210             + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
211             + "State=RUNNING) started at http://localhost:12321/apexservices/"));
212         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
213             .endsWith("(ApexEditorMain: "
214                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
215                 + " State=STOPPED) shut down "));
216     }
217
218     /**
219      * Test port arg.
220      *
221      * @throws IOException          Signals that an I/O exception has occurred.
222      * @throws InterruptedException if the test is interrupted
223      */
224     @Test
225     public void testPortArgSpace() throws IOException, InterruptedException {
226         final String[] args = new String[] { "-p 12321" };
227
228         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
229             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
230                 + " parameter error, error parsing argument \"port\" :For input string: \" 12321\"");
231     }
232
233     /**
234      * Test bad port arg 0.
235      *
236      * @throws IOException          Signals that an I/O exception has occurred.
237      * @throws InterruptedException if the test is interrupted
238      */
239     @Test
240     public void testBadPortArgs0() throws IOException, InterruptedException {
241         final String[] args = new String[] { "-p0" };
242
243         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
244             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
245                 + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
246                 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
247     }
248
249     /**
250      * Test bad port arg 1023.
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 testBadPortArgs1023() throws IOException, InterruptedException {
257         final String[] args = new String[] { "-p1023" };
258
259         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
260             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
261                 + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
262                 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
263     }
264
265     /**
266      * Test bad port arg 65536.
267      *
268      * @throws IOException          Signals that an I/O exception has occurred.
269      * @throws InterruptedException if the test is interrupted
270      */
271     @Test
272     public void testBadPortArgs65536() throws IOException, InterruptedException {
273         final String[] args = new String[] { "-p65536" };
274
275         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
276             .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
277                 + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
278                 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
279     }
280
281     /**
282      * Test TTL arg 0.
283      *
284      * @throws IOException          Signals that an I/O exception has occurred.
285      * @throws InterruptedException if the test is interrupted
286      */
287     @Test
288     public void testTtlArg0() throws IOException, InterruptedException {
289         final String[] args = new String[] { "-t10" };
290
291         final String outString = runEditor(args);
292
293         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
294             + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
295             + "State=READY) starting at http://localhost:18989/apexservices/"));
296         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
297             .contains("Apex Editor REST endpoint (ApexEditorMain: "
298                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
299                 + " started"));
300         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
301             .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
302                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
303                 + " shut down "));
304     }
305
306     /**
307      * Test TTL arg 10.
308      *
309      * @throws IOException          Signals that an I/O exception has occurred.
310      * @throws InterruptedException if the test is interrupted
311      */
312     @Test
313     public void testTtlArg1() throws IOException, InterruptedException {
314         final String[] args = new String[] { "-t", "10", "-l", "localhost" };
315
316         final String outString = runEditor(args);
317
318         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
319             + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
320             + "State=READY) starting at http://localhost:18989/apexservices/"));
321         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
322             .contains("Apex Editor REST endpoint (ApexEditorMain: "
323                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
324                 + " started"));
325         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
326             .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
327                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
328                 + " shut down "));
329     }
330
331     /**
332      * Test port TTL arg 0.
333      *
334      * @throws IOException          Signals that an I/O exception has occurred.
335      * @throws InterruptedException if the test is interrupted
336      */
337     @Test
338     public void testPortTtlArg0() throws IOException, InterruptedException {
339         final String[] args = new String[] { "-t", "10", "-p", "12321" };
340
341         final String outString = runEditor(args);
342
343         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
344             + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
345             + "State=READY) starting at http://localhost:12321/apexservices/"));
346         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
347             .contains("Apex Editor REST endpoint (ApexEditorMain: "
348                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
349                 + " started"));
350         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
351             .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
352                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
353                 + " shut down "));
354     }
355
356     /**
357      * Test port TTL arg 10.
358      *
359      * @throws IOException          Signals that an I/O exception has occurred.
360      * @throws InterruptedException if the test is interrupted
361      */
362     @Test
363     public void testPortTtlArg1() throws IOException, InterruptedException {
364         final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
365
366         final String outString = runEditor(args);
367
368         assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
369             + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
370             + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
371         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
372             .contains("Apex Editor REST endpoint (ApexEditorMain: "
373                 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
374                 + " started"));
375         assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
376             .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
377                 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
378                 + " shut down "));
379     }
380
381     /**
382      * Run the editor for tests.
383      *
384      * @param args the args
385      * @return the output string
386      * @throws InterruptedException if the test is interrupted
387      */
388     private String runEditor(final String[] args) throws InterruptedException {
389         ParameterService.clear();
390         final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
391         final PrintStream outStream = new PrintStream(outBaStream);
392
393         final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
394
395         // This test must be started in a thread because we want to intercept the output
396         // in cases where the editor is
397         // started infinitely
398         final Runnable testThread = new Runnable() {
399             @Override
400             public void run() {
401                 editorMain.init();
402             }
403         };
404         new Thread(testThread).start();
405         await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
406             || editorMain.getState().equals(EditorState.INITIALIZING)));
407         editorMain.shutdown();
408         final String outString = outBaStream.toString();
409         System.out.println(outString);
410         return outString;
411     }
412 }