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