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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.client.editor.rest;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.util.concurrent.TimeUnit;
33 import org.junit.Test;
34 import org.onap.policy.apex.client.editor.rest.ApexEditorMain.EditorState;
37 * Test Apex Editor Startup.
39 public class ApexEditorStartupTest {
40 // CHECKSTYLE:OFF: MagicNumber
45 * @throws IOException Signals that an I/O exception has occurred.
46 * @throws InterruptedException if the test is interrupted
49 public void testNoArgs() throws IOException, InterruptedException {
50 final String[] args = new String[] {};
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]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
60 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec],"
61 + " State=STOPPED) shut down "));
67 * @throws IOException Signals that an I/O exception has occurred.
68 * @throws InterruptedException if the test is interrupted
71 public void testBadArg0() throws IOException, InterruptedException {
72 final String[] args = new String[] { "12321" };
76 fail("test should throw an exception here");
77 } catch (final Exception e) {
78 assertTrue(e.getLocalizedMessage().startsWith(
79 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
80 + " too many command line arguments specified : [12321]"));
87 * @throws IOException Signals that an I/O exception has occurred.
88 * @throws InterruptedException if the test is interrupted
91 public void testBadArg1() throws IOException, InterruptedException {
92 final String[] args = new String[] { "12321 12322 12323" };
96 fail("test should throw an exception here");
97 } catch (final Exception e) {
98 assertTrue(e.getLocalizedMessage().startsWith(
99 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
100 + " too many command line arguments specified : [12321 12322 12323]"));
107 * @throws IOException Signals that an I/O exception has occurred.
108 * @throws InterruptedException if the test is interrupted
111 public void testBadArg2() throws IOException, InterruptedException {
112 final String[] args = new String[] { "-z" };
116 fail("test should throw an exception here");
117 } catch (final Exception e) {
118 assertTrue(e.getLocalizedMessage().startsWith(
119 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
120 + " invalid command line arguments specified : Unrecognized option: -z"));
127 * @throws IOException Signals that an I/O exception has occurred.
128 * @throws InterruptedException if the test is interrupted
131 public void testBadArg3() throws IOException, InterruptedException {
132 final String[] args = new String[] { "--hello" };
136 fail("test should throw an exception here");
137 } catch (final Exception e) {
138 assertTrue(e.getLocalizedMessage().startsWith(
139 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
140 + " invalid command line arguments specified : Unrecognized option: --hello"));
148 * @throws IOException Signals that an I/O exception has occurred.
149 * @throws InterruptedException if the test is interrupted
152 public void testBadArg4() throws IOException, InterruptedException {
153 final String[] args = new String[] { "-l", "+++++" };
157 fail("test should throw an exception here");
158 } catch (final Exception e) {
159 assertTrue(e.getLocalizedMessage()
160 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
161 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
162 + "State=STOPPED) parameters invalid, listen address is not valid. "
163 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/"));
170 * @throws IOException Signals that an I/O exception has occurred.
171 * @throws InterruptedException if the test is interrupted
174 public void testHelp0() throws IOException, InterruptedException {
175 final String[] args = new String[] { "--help" };
179 fail("test should throw an exception here");
180 } catch (final Exception e) {
181 assertTrue(e.getMessage()
182 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
189 * @throws IOException Signals that an I/O exception has occurred.
190 * @throws InterruptedException if the test is interrupted
193 public void testHelp1() throws IOException, InterruptedException {
194 final String[] args = new String[] { "-h" };
198 fail("test should throw an exception here");
199 } catch (final Exception e) {
200 assertTrue(e.getMessage()
201 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
208 * @throws IOException Signals that an I/O exception has occurred.
209 * @throws InterruptedException if the test is interrupted
212 public void testPortArgShJo() throws IOException, InterruptedException {
213 final String[] args = new String[] { "-p12321" };
215 final String outString = runEditor(args);
217 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
218 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
219 + "State=READY) starting at http://localhost:12321/apexservices/"));
220 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
221 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
222 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
223 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
224 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
225 + " State=STOPPED) shut down "));
231 * @throws IOException Signals that an I/O exception has occurred.
232 * @throws InterruptedException if the test is interrupted
235 public void testPortArgShSe() throws IOException, InterruptedException {
236 final String[] args = new String[] { "-p", "12321" };
238 final String outString = runEditor(args);
240 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
241 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
242 + "State=READY) starting at http://localhost:12321/apexservices/"));
243 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
244 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
245 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
246 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("(ApexEditorMain: "
247 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
248 + " State=STOPPED) shut down "));
255 * @throws IOException Signals that an I/O exception has occurred.
256 * @throws InterruptedException if the test is interrupted
259 public void testPortArgSpace() throws IOException, InterruptedException {
260 final String[] args = new String[] { "-p 12321" };
264 fail("test should throw an exception here");
265 } catch (final Exception e) {
266 assertTrue(e.getMessage().startsWith(
267 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
268 + " error parsing argument \"port\" :For input string: \" 12321\""));
273 * Test bad port arg 0.
275 * @throws IOException Signals that an I/O exception has occurred.
276 * @throws InterruptedException if the test is interrupted
279 public void testBadPortArgs0() throws IOException, InterruptedException {
280 final String[] args = new String[] { "-p0" };
284 fail("test should throw an exception here");
285 } catch (final Exception e) {
286 assertTrue(e.getMessage()
287 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
288 + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
289 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
294 * Test bad port arg 1023.
296 * @throws IOException Signals that an I/O exception has occurred.
297 * @throws InterruptedException if the test is interrupted
300 public void testBadPortArgs1023() throws IOException, InterruptedException {
301 final String[] args = new String[] { "-p1023" };
305 fail("test should throw an exception here");
306 } catch (final Exception e) {
307 assertTrue(e.getMessage()
308 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
309 + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
310 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
315 * Test bad port arg 65536.
317 * @throws IOException Signals that an I/O exception has occurred.
318 * @throws InterruptedException if the test is interrupted
321 public void testBadPortArgs65536() throws IOException, InterruptedException {
322 final String[] args = new String[] { "-p65536" };
326 fail("test should throw an exception here");
327 } catch (final Exception e) {
328 assertTrue(e.getMessage()
329 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
330 + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
331 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
338 * @throws IOException Signals that an I/O exception has occurred.
339 * @throws InterruptedException if the test is interrupted
342 public void testTtlArg0() throws IOException, InterruptedException {
343 final String[] args = new String[] { "-t10" };
345 final String outString = runEditor(args);
347 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
348 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
349 + "State=READY) starting at http://localhost:18989/apexservices/"));
350 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
351 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
353 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
354 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
361 * @throws IOException Signals that an I/O exception has occurred.
362 * @throws InterruptedException if the test is interrupted
365 public void testTtlArg1() throws IOException, InterruptedException {
366 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
368 final String outString = runEditor(args);
370 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
371 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
372 + "State=READY) starting at http://localhost:18989/apexservices/"));
373 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
374 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
376 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
377 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
382 * Test port TTL arg 0.
384 * @throws IOException Signals that an I/O exception has occurred.
385 * @throws InterruptedException if the test is interrupted
388 public void testPortTtlArg0() throws IOException, InterruptedException {
389 final String[] args = new String[] { "-t", "10", "-p", "12321" };
391 final String outString = runEditor(args);
393 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
394 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
395 + "State=READY) starting at http://localhost:12321/apexservices/"));
396 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
397 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
399 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
400 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
406 * Test port TTL arg 10.
408 * @throws IOException Signals that an I/O exception has occurred.
409 * @throws InterruptedException if the test is interrupted
412 public void testPortTtlArg1() throws IOException, InterruptedException {
413 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
415 final String outString = runEditor(args);
417 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
418 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
419 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
420 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
421 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
423 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
424 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
429 * Run the editor for tests.
431 * @param args the args
432 * @return the output string
433 * @throws InterruptedException if the test is interrupted
435 private String runEditor(final String[] args) throws InterruptedException {
436 final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
437 final PrintStream outStream = new PrintStream(outBaStream);
439 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
441 // This test must be started in a thread because we want to intercept the output in cases where the editor is
442 // started infinitely
443 final Runnable testThread = new Runnable() {
449 new Thread(testThread).start();
450 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
451 || editorMain.getState().equals(EditorState.INITIALIZING)));
452 editorMain.shutdown();
453 final String outString = outBaStream.toString();
454 System.out.println(outString);