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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
30 import org.junit.Test;
31 import org.onap.policy.apex.client.editor.rest.ApexEditorMain.EditorState;
34 * The Class TestApexEditorStartup.
36 public class TestApexEditorStartup {
37 // CHECKSTYLE:OFF: MagicNumber
42 * @throws IOException Signals that an I/O exception has occurred.
43 * @throws InterruptedException if the test is interrupted
46 public void testNoArgs() throws IOException, InterruptedException {
47 final String[] args = new String[] {};
49 final String outString = runEditor(args);
50 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
51 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:18989/apexservices/, TTL=-1sec], "
52 + "State=READY) starting at http://0.0.0.0:18989/apexservices/"));
53 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
54 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:18989/apexservices/, TTL=-1sec], "
55 + "State=RUNNING) started at http://0.0.0.0:18989/apexservices/"));
56 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
57 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:18989/apexservices/, TTL=-1sec], State=STOPPED) shut down "));
63 * @throws IOException Signals that an I/O exception has occurred.
64 * @throws InterruptedException if the test is interrupted
67 public void testBadArg0() throws IOException, InterruptedException {
68 final String[] args = new String[] { "12321" };
72 fail("test should throw an exception here");
73 } catch (final Exception e) {
74 assertTrue(e.getLocalizedMessage().startsWith("Apex Editor REST endpoint (ApexEditorMain: "
75 + "Config=[null], State=STOPPED) parameter error, too many command line arguments specified : [12321]"));
82 * @throws IOException Signals that an I/O exception has occurred.
83 * @throws InterruptedException if the test is interrupted
86 public void testBadArg1() throws IOException, InterruptedException {
87 final String[] args = new String[] { "12321 12322 12323" };
91 fail("test should throw an exception here");
92 } catch (final Exception e) {
93 assertTrue(e.getLocalizedMessage().startsWith("Apex Editor REST endpoint (ApexEditorMain: "
94 + "Config=[null], State=STOPPED) parameter error, too many command line arguments specified : [12321 12322 12323]"));
101 * @throws IOException Signals that an I/O exception has occurred.
102 * @throws InterruptedException if the test is interrupted
105 public void testBadArg2() throws IOException, InterruptedException {
106 final String[] args = new String[] { "-z" };
110 fail("test should throw an exception here");
111 } catch (final Exception e) {
112 assertTrue(e.getLocalizedMessage().startsWith("Apex Editor REST endpoint (ApexEditorMain: "
113 + "Config=[null], State=STOPPED) parameter error, invalid command line arguments specified : Unrecognized option: -z"));
120 * @throws IOException Signals that an I/O exception has occurred.
121 * @throws InterruptedException if the test is interrupted
124 public void testBadArg3() throws IOException, InterruptedException {
125 final String[] args = new String[] { "--hello" };
129 fail("test should throw an exception here");
130 } catch (final Exception e) {
131 assertTrue(e.getLocalizedMessage().startsWith("Apex Editor REST endpoint (ApexEditorMain: "
132 + "Config=[null], State=STOPPED) parameter error, invalid command line arguments specified : Unrecognized option: --hello"));
140 * @throws IOException Signals that an I/O exception has occurred.
141 * @throws InterruptedException if the test is interrupted
144 public void testBadArg4() throws IOException, InterruptedException {
145 final String[] args = new String[] { "-l", "+++++" };
149 fail("test should throw an exception here");
150 } catch (final Exception e) {
151 assertTrue(e.getLocalizedMessage()
152 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
153 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
154 + "State=STOPPED) parameters invalid, listen address is not valid. "
155 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/"));
162 * @throws IOException Signals that an I/O exception has occurred.
163 * @throws InterruptedException if the test is interrupted
166 public void testHelp0() throws IOException, InterruptedException {
167 final String[] args = new String[] { "--help" };
171 fail("test should throw an exception here");
172 } catch (final Exception e) {
173 assertTrue(e.getMessage()
174 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
181 * @throws IOException Signals that an I/O exception has occurred.
182 * @throws InterruptedException if the test is interrupted
185 public void testHelp1() throws IOException, InterruptedException {
186 final String[] args = new String[] { "-h" };
190 fail("test should throw an exception here");
191 } catch (final Exception e) {
192 assertTrue(e.getMessage()
193 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
200 * @throws IOException Signals that an I/O exception has occurred.
201 * @throws InterruptedException if the test is interrupted
204 public void testPortArgShJo() throws IOException, InterruptedException {
205 final String[] args = new String[] { "-p12321" };
207 final String outString = runEditor(args);
209 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
210 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=-1sec], "
211 + "State=READY) starting at http://0.0.0.0:12321/apexservices/"));
212 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
213 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=-1sec], "
214 + "State=RUNNING) started at http://0.0.0.0:12321/apexservices/"));
215 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
216 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=-1sec], State=STOPPED) shut down "));
222 * @throws IOException Signals that an I/O exception has occurred.
223 * @throws InterruptedException if the test is interrupted
226 public void testPortArgShSe() throws IOException, InterruptedException {
227 final String[] args = new String[] { "-p", "12321" };
229 final String outString = runEditor(args);
231 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
232 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=-1sec], "
233 + "State=READY) starting at http://0.0.0.0:12321/apexservices/"));
234 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
235 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=-1sec], "
236 + "State=RUNNING) started at http://0.0.0.0:12321/apexservices/"));
237 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("(ApexEditorMain: "
238 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=-1sec], State=STOPPED) shut down "));
245 * @throws IOException Signals that an I/O exception has occurred.
246 * @throws InterruptedException if the test is interrupted
249 public void testPortArgSpace() throws IOException, InterruptedException {
250 final String[] args = new String[] { "-p 12321" };
254 fail("test should throw an exception here");
255 } catch (final Exception e) {
256 assertTrue(e.getMessage().startsWith("Apex Editor REST endpoint (ApexEditorMain: "
257 + "Config=[null], State=STOPPED) parameter error, error parsing argument \"port\" :For input string: \" 12321\""));
262 * Test bad port arg 0.
264 * @throws IOException Signals that an I/O exception has occurred.
265 * @throws InterruptedException if the test is interrupted
268 public void testBadPortArgs0() throws IOException, InterruptedException {
269 final String[] args = new String[] { "-p0" };
273 fail("test should throw an exception here");
274 } catch (final Exception e) {
275 assertTrue(e.getMessage()
276 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
277 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:0/apexservices/, TTL=-1sec], "
278 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
283 * Test bad port arg 1023.
285 * @throws IOException Signals that an I/O exception has occurred.
286 * @throws InterruptedException if the test is interrupted
289 public void testBadPortArgs1023() throws IOException, InterruptedException {
290 final String[] args = new String[] { "-p1023" };
294 fail("test should throw an exception here");
295 } catch (final Exception e) {
296 assertTrue(e.getMessage()
297 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
298 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:1023/apexservices/, TTL=-1sec], "
299 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
304 * Test bad port arg 65536.
306 * @throws IOException Signals that an I/O exception has occurred.
307 * @throws InterruptedException if the test is interrupted
310 public void testBadPortArgs65536() throws IOException, InterruptedException {
311 final String[] args = new String[] { "-p65536" };
315 fail("test should throw an exception here");
316 } catch (final Exception e) {
317 assertTrue(e.getMessage()
318 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
319 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:65536/apexservices/, TTL=-1sec], "
320 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
327 * @throws IOException Signals that an I/O exception has occurred.
328 * @throws InterruptedException if the test is interrupted
331 public void testTTLArg0() throws IOException, InterruptedException {
332 final String[] args = new String[] { "-t10" };
334 final String outString = runEditor(args);
336 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
337 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:18989/apexservices/, TTL=10sec], "
338 + "State=READY) starting at http://0.0.0.0:18989/apexservices/"));
339 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
340 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:18989/apexservices/, TTL=10sec], State=RUNNING) started"));
341 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
342 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:18989/apexservices/, TTL=10sec], State=STOPPED) shut down "));
348 * @throws IOException Signals that an I/O exception has occurred.
349 * @throws InterruptedException if the test is interrupted
352 public void testTTLArg1() throws IOException, InterruptedException {
353 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
355 final String outString = runEditor(args);
357 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
358 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
359 + "State=READY) starting at http://localhost:18989/apexservices/"));
360 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
361 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING) started"));
362 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
363 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED) shut down "));
367 * Test port TTL arg 0.
369 * @throws IOException Signals that an I/O exception has occurred.
370 * @throws InterruptedException if the test is interrupted
373 public void testPortTTLArg0() throws IOException, InterruptedException {
374 final String[] args = new String[] { "-t", "10", "-p", "12321" };
376 final String outString = runEditor(args);
378 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
379 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=10sec], "
380 + "State=READY) starting at http://0.0.0.0:12321/apexservices/"));
381 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
382 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=10sec], State=RUNNING) started"));
383 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
384 + "Config=[ApexEditorParameters: URI=http://0.0.0.0:12321/apexservices/, TTL=10sec], State=STOPPED) shut down "));
389 * Test port TTL arg 10.
391 * @throws IOException Signals that an I/O exception has occurred.
392 * @throws InterruptedException if the test is interrupted
395 public void testPortTTLArg1() throws IOException, InterruptedException {
396 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
398 final String outString = runEditor(args);
400 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
401 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
402 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
403 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
404 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING) started"));
405 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
406 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED) shut down "));
410 * Run the editor for tests.
412 * @param args the args
413 * @return the output string
414 * @throws InterruptedException if the test is interrupted
416 private String runEditor(final String[] args) throws InterruptedException {
417 final ByteArrayOutputStream outBAStream = new ByteArrayOutputStream();
418 final PrintStream outStream = new PrintStream(outBAStream);
420 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
422 // This test must be started in a thread because we want to intercept the output in cases where the editor is
423 // started infinitely
424 final Runnable testThread = new Runnable() {
430 new Thread(testThread).start();
431 while (editorMain.getState().equals(EditorState.READY)
432 || editorMain.getState().equals(EditorState.INITIALIZING)) {
436 editorMain.shutdown();
437 final String outString = outBAStream.toString();
438 System.out.println(outString);