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