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 * Test Apex Editor Startup.
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://localhost:18989/apexservices/, TTL=-1sec], "
52 + "State=READY) starting at http://localhost:18989/apexservices/"));
53 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
54 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec], "
55 + "State=RUNNING) started at http://localhost:18989/apexservices/"));
56 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
57 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec],"
58 + " State=STOPPED) shut down "));
64 * @throws IOException Signals that an I/O exception has occurred.
65 * @throws InterruptedException if the test is interrupted
68 public void testBadArg0() throws IOException, InterruptedException {
69 final String[] args = new String[] { "12321" };
73 fail("test should throw an exception here");
74 } catch (final Exception e) {
75 assertTrue(e.getLocalizedMessage().startsWith(
76 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
77 + " too many command line arguments specified : [12321]"));
84 * @throws IOException Signals that an I/O exception has occurred.
85 * @throws InterruptedException if the test is interrupted
88 public void testBadArg1() throws IOException, InterruptedException {
89 final String[] args = new String[] { "12321 12322 12323" };
93 fail("test should throw an exception here");
94 } catch (final Exception e) {
95 assertTrue(e.getLocalizedMessage().startsWith(
96 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
97 + " too many command line arguments specified : [12321 12322 12323]"));
104 * @throws IOException Signals that an I/O exception has occurred.
105 * @throws InterruptedException if the test is interrupted
108 public void testBadArg2() throws IOException, InterruptedException {
109 final String[] args = new String[] { "-z" };
113 fail("test should throw an exception here");
114 } catch (final Exception e) {
115 assertTrue(e.getLocalizedMessage().startsWith(
116 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
117 + " invalid command line arguments specified : Unrecognized option: -z"));
124 * @throws IOException Signals that an I/O exception has occurred.
125 * @throws InterruptedException if the test is interrupted
128 public void testBadArg3() throws IOException, InterruptedException {
129 final String[] args = new String[] { "--hello" };
133 fail("test should throw an exception here");
134 } catch (final Exception e) {
135 assertTrue(e.getLocalizedMessage().startsWith(
136 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
137 + " invalid command line arguments specified : Unrecognized option: --hello"));
145 * @throws IOException Signals that an I/O exception has occurred.
146 * @throws InterruptedException if the test is interrupted
149 public void testBadArg4() throws IOException, InterruptedException {
150 final String[] args = new String[] { "-l", "+++++" };
154 fail("test should throw an exception here");
155 } catch (final Exception e) {
156 assertTrue(e.getLocalizedMessage()
157 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
158 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
159 + "State=STOPPED) parameters invalid, listen address is not valid. "
160 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/"));
167 * @throws IOException Signals that an I/O exception has occurred.
168 * @throws InterruptedException if the test is interrupted
171 public void testHelp0() throws IOException, InterruptedException {
172 final String[] args = new String[] { "--help" };
176 fail("test should throw an exception here");
177 } catch (final Exception e) {
178 assertTrue(e.getMessage()
179 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
186 * @throws IOException Signals that an I/O exception has occurred.
187 * @throws InterruptedException if the test is interrupted
190 public void testHelp1() throws IOException, InterruptedException {
191 final String[] args = new String[] { "-h" };
195 fail("test should throw an exception here");
196 } catch (final Exception e) {
197 assertTrue(e.getMessage()
198 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
205 * @throws IOException Signals that an I/O exception has occurred.
206 * @throws InterruptedException if the test is interrupted
209 public void testPortArgShJo() throws IOException, InterruptedException {
210 final String[] args = new String[] { "-p12321" };
212 final String outString = runEditor(args);
214 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
215 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
216 + "State=READY) starting at http://localhost:12321/apexservices/"));
217 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
218 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
219 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
220 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
221 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
222 + " State=STOPPED) shut down "));
228 * @throws IOException Signals that an I/O exception has occurred.
229 * @throws InterruptedException if the test is interrupted
232 public void testPortArgShSe() throws IOException, InterruptedException {
233 final String[] args = new String[] { "-p", "12321" };
235 final String outString = runEditor(args);
237 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
238 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
239 + "State=READY) starting at http://localhost:12321/apexservices/"));
240 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
241 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
242 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
243 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("(ApexEditorMain: "
244 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
245 + " State=STOPPED) shut down "));
252 * @throws IOException Signals that an I/O exception has occurred.
253 * @throws InterruptedException if the test is interrupted
256 public void testPortArgSpace() throws IOException, InterruptedException {
257 final String[] args = new String[] { "-p 12321" };
261 fail("test should throw an exception here");
262 } catch (final Exception e) {
263 assertTrue(e.getMessage().startsWith(
264 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
265 + " error parsing argument \"port\" :For input string: \" 12321\""));
270 * Test bad port arg 0.
272 * @throws IOException Signals that an I/O exception has occurred.
273 * @throws InterruptedException if the test is interrupted
276 public void testBadPortArgs0() throws IOException, InterruptedException {
277 final String[] args = new String[] { "-p0" };
281 fail("test should throw an exception here");
282 } catch (final Exception e) {
283 assertTrue(e.getMessage()
284 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
285 + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
286 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
291 * Test bad port arg 1023.
293 * @throws IOException Signals that an I/O exception has occurred.
294 * @throws InterruptedException if the test is interrupted
297 public void testBadPortArgs1023() throws IOException, InterruptedException {
298 final String[] args = new String[] { "-p1023" };
302 fail("test should throw an exception here");
303 } catch (final Exception e) {
304 assertTrue(e.getMessage()
305 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
306 + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
307 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
312 * Test bad port arg 65536.
314 * @throws IOException Signals that an I/O exception has occurred.
315 * @throws InterruptedException if the test is interrupted
318 public void testBadPortArgs65536() throws IOException, InterruptedException {
319 final String[] args = new String[] { "-p65536" };
323 fail("test should throw an exception here");
324 } catch (final Exception e) {
325 assertTrue(e.getMessage()
326 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
327 + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
328 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
335 * @throws IOException Signals that an I/O exception has occurred.
336 * @throws InterruptedException if the test is interrupted
339 public void testTTLArg0() throws IOException, InterruptedException {
340 final String[] args = new String[] { "-t10" };
342 final String outString = runEditor(args);
344 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
345 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
346 + "State=READY) starting at http://localhost:18989/apexservices/"));
347 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
348 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
350 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
351 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
358 * @throws IOException Signals that an I/O exception has occurred.
359 * @throws InterruptedException if the test is interrupted
362 public void testTTLArg1() throws IOException, InterruptedException {
363 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
365 final String outString = runEditor(args);
367 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
368 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
369 + "State=READY) starting at http://localhost:18989/apexservices/"));
370 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
371 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
373 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
374 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
379 * Test port TTL arg 0.
381 * @throws IOException Signals that an I/O exception has occurred.
382 * @throws InterruptedException if the test is interrupted
385 public void testPortTTLArg0() throws IOException, InterruptedException {
386 final String[] args = new String[] { "-t", "10", "-p", "12321" };
388 final String outString = runEditor(args);
390 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
391 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
392 + "State=READY) starting at http://localhost:12321/apexservices/"));
393 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
394 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
396 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
397 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
403 * Test port TTL arg 10.
405 * @throws IOException Signals that an I/O exception has occurred.
406 * @throws InterruptedException if the test is interrupted
409 public void testPortTTLArg1() throws IOException, InterruptedException {
410 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
412 final String outString = runEditor(args);
414 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
415 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
416 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
417 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
418 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
420 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
421 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
426 * Run the editor for tests.
428 * @param args the args
429 * @return the output string
430 * @throws InterruptedException if the test is interrupted
432 private String runEditor(final String[] args) throws InterruptedException {
433 final ByteArrayOutputStream outBAStream = new ByteArrayOutputStream();
434 final PrintStream outStream = new PrintStream(outBAStream);
436 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
438 // This test must be started in a thread because we want to intercept the output in cases where the editor is
439 // started infinitely
440 final Runnable testThread = new Runnable() {
446 new Thread(testThread).start();
447 while (editorMain.getState().equals(EditorState.READY)
448 || editorMain.getState().equals(EditorState.INITIALIZING)) {
452 editorMain.shutdown();
453 final String outString = outBAStream.toString();
454 System.out.println(outString);