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;
32 import org.junit.Test;
33 import org.onap.policy.apex.client.editor.rest.ApexEditorMain.EditorState;
36 * Test Apex Editor Startup.
38 public class ApexEditorStartupTest {
39 // CHECKSTYLE:OFF: MagicNumber
44 * @throws IOException Signals that an I/O exception has occurred.
45 * @throws InterruptedException if the test is interrupted
48 public void testNoArgs() throws IOException, InterruptedException {
49 final String[] args = new String[] {};
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]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
59 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec],"
60 + " State=STOPPED) shut down "));
66 * @throws IOException Signals that an I/O exception has occurred.
67 * @throws InterruptedException if the test is interrupted
70 public void testBadArg0() throws IOException, InterruptedException {
71 final String[] args = new String[] { "12321" };
75 fail("test should throw an exception here");
76 } catch (final Exception e) {
77 assertTrue(e.getLocalizedMessage().startsWith(
78 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
79 + " too many command line arguments specified : [12321]"));
86 * @throws IOException Signals that an I/O exception has occurred.
87 * @throws InterruptedException if the test is interrupted
90 public void testBadArg1() throws IOException, InterruptedException {
91 final String[] args = new String[] { "12321 12322 12323" };
95 fail("test should throw an exception here");
96 } catch (final Exception e) {
97 assertTrue(e.getLocalizedMessage().startsWith(
98 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
99 + " too many command line arguments specified : [12321 12322 12323]"));
106 * @throws IOException Signals that an I/O exception has occurred.
107 * @throws InterruptedException if the test is interrupted
110 public void testBadArg2() throws IOException, InterruptedException {
111 final String[] args = new String[] { "-z" };
115 fail("test should throw an exception here");
116 } catch (final Exception e) {
117 assertTrue(e.getLocalizedMessage().startsWith(
118 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
119 + " invalid command line arguments specified : Unrecognized option: -z"));
126 * @throws IOException Signals that an I/O exception has occurred.
127 * @throws InterruptedException if the test is interrupted
130 public void testBadArg3() throws IOException, InterruptedException {
131 final String[] args = new String[] { "--hello" };
135 fail("test should throw an exception here");
136 } catch (final Exception e) {
137 assertTrue(e.getLocalizedMessage().startsWith(
138 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
139 + " invalid command line arguments specified : Unrecognized option: --hello"));
147 * @throws IOException Signals that an I/O exception has occurred.
148 * @throws InterruptedException if the test is interrupted
151 public void testBadArg4() throws IOException, InterruptedException {
152 final String[] args = new String[] { "-l", "+++++" };
156 fail("test should throw an exception here");
157 } catch (final Exception e) {
158 assertTrue(e.getLocalizedMessage()
159 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
160 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
161 + "State=STOPPED) parameters invalid, listen address is not valid. "
162 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/"));
169 * @throws IOException Signals that an I/O exception has occurred.
170 * @throws InterruptedException if the test is interrupted
173 public void testHelp0() throws IOException, InterruptedException {
174 final String[] args = new String[] { "--help" };
178 fail("test should throw an exception here");
179 } catch (final Exception e) {
180 assertTrue(e.getMessage()
181 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
188 * @throws IOException Signals that an I/O exception has occurred.
189 * @throws InterruptedException if the test is interrupted
192 public void testHelp1() throws IOException, InterruptedException {
193 final String[] args = new String[] { "-h" };
197 fail("test should throw an exception here");
198 } catch (final Exception e) {
199 assertTrue(e.getMessage()
200 .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"));
207 * @throws IOException Signals that an I/O exception has occurred.
208 * @throws InterruptedException if the test is interrupted
211 public void testPortArgShJo() throws IOException, InterruptedException {
212 final String[] args = new String[] { "-p12321" };
214 final String outString = runEditor(args);
216 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
217 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
218 + "State=READY) starting at http://localhost:12321/apexservices/"));
219 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
220 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
221 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
222 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
223 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
224 + " State=STOPPED) shut down "));
230 * @throws IOException Signals that an I/O exception has occurred.
231 * @throws InterruptedException if the test is interrupted
234 public void testPortArgShSe() throws IOException, InterruptedException {
235 final String[] args = new String[] { "-p", "12321" };
237 final String outString = runEditor(args);
239 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
240 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
241 + "State=READY) starting at http://localhost:12321/apexservices/"));
242 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
243 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
244 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
245 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("(ApexEditorMain: "
246 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
247 + " State=STOPPED) shut down "));
254 * @throws IOException Signals that an I/O exception has occurred.
255 * @throws InterruptedException if the test is interrupted
258 public void testPortArgSpace() throws IOException, InterruptedException {
259 final String[] args = new String[] { "-p 12321" };
263 fail("test should throw an exception here");
264 } catch (final Exception e) {
265 assertTrue(e.getMessage().startsWith(
266 "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error,"
267 + " error parsing argument \"port\" :For input string: \" 12321\""));
272 * Test bad port arg 0.
274 * @throws IOException Signals that an I/O exception has occurred.
275 * @throws InterruptedException if the test is interrupted
278 public void testBadPortArgs0() throws IOException, InterruptedException {
279 final String[] args = new String[] { "-p0" };
283 fail("test should throw an exception here");
284 } catch (final Exception e) {
285 assertTrue(e.getMessage()
286 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
287 + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
288 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
293 * Test bad port arg 1023.
295 * @throws IOException Signals that an I/O exception has occurred.
296 * @throws InterruptedException if the test is interrupted
299 public void testBadPortArgs1023() throws IOException, InterruptedException {
300 final String[] args = new String[] { "-p1023" };
304 fail("test should throw an exception here");
305 } catch (final Exception e) {
306 assertTrue(e.getMessage()
307 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
308 + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
309 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
314 * Test bad port arg 65536.
316 * @throws IOException Signals that an I/O exception has occurred.
317 * @throws InterruptedException if the test is interrupted
320 public void testBadPortArgs65536() throws IOException, InterruptedException {
321 final String[] args = new String[] { "-p65536" };
325 fail("test should throw an exception here");
326 } catch (final Exception e) {
327 assertTrue(e.getMessage()
328 .startsWith("Apex Editor REST endpoint (ApexEditorMain: "
329 + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
330 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"));
337 * @throws IOException Signals that an I/O exception has occurred.
338 * @throws InterruptedException if the test is interrupted
341 public void testTtlArg0() throws IOException, InterruptedException {
342 final String[] args = new String[] { "-t10" };
344 final String outString = runEditor(args);
346 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
347 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
348 + "State=READY) starting at http://localhost:18989/apexservices/"));
349 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
350 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
352 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
353 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
360 * @throws IOException Signals that an I/O exception has occurred.
361 * @throws InterruptedException if the test is interrupted
364 public void testTtlArg1() throws IOException, InterruptedException {
365 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
367 final String outString = runEditor(args);
369 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
370 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
371 + "State=READY) starting at http://localhost:18989/apexservices/"));
372 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
373 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
375 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
376 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
381 * Test port TTL arg 0.
383 * @throws IOException Signals that an I/O exception has occurred.
384 * @throws InterruptedException if the test is interrupted
387 public void testPortTtlArg0() throws IOException, InterruptedException {
388 final String[] args = new String[] { "-t", "10", "-p", "12321" };
390 final String outString = runEditor(args);
392 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
393 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
394 + "State=READY) starting at http://localhost:12321/apexservices/"));
395 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
396 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
398 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
399 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
405 * Test port TTL arg 10.
407 * @throws IOException Signals that an I/O exception has occurred.
408 * @throws InterruptedException if the test is interrupted
411 public void testPortTtlArg1() throws IOException, InterruptedException {
412 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
414 final String outString = runEditor(args);
416 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
417 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
418 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
419 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
420 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
422 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
423 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
428 * Run the editor for tests.
430 * @param args the args
431 * @return the output string
432 * @throws InterruptedException if the test is interrupted
434 private String runEditor(final String[] args) throws InterruptedException {
435 final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
436 final PrintStream outStream = new PrintStream(outBaStream);
438 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
440 // This test must be started in a thread because we want to intercept the output in cases where the editor is
441 // started infinitely
442 final Runnable testThread = new Runnable() {
448 new Thread(testThread).start();
449 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
450 || editorMain.getState().equals(EditorState.INITIALIZING)));
451 editorMain.shutdown();
452 final String outString = outBaStream.toString();
453 System.out.println(outString);