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.gui.editors.apex.rest;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertTrue;
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.gui.editors.apex.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]+", " ")
59 .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" };
74 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
75 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
76 + " 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" };
89 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
90 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
91 + " parameter error, too many command line arguments specified : [12321 12322 12323]");
97 * @throws IOException Signals that an I/O exception has occurred.
98 * @throws InterruptedException if the test is interrupted
101 public void testBadArg2() throws IOException, InterruptedException {
102 final String[] args = new String[] { "-z" };
104 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
105 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
106 + " parameter error, invalid command line arguments specified : Unrecognized option: -z");
112 * @throws IOException Signals that an I/O exception has occurred.
113 * @throws InterruptedException if the test is interrupted
116 public void testBadArg3() throws IOException, InterruptedException {
117 final String[] args = new String[] { "--hello" };
119 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
120 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
121 + " parameter error, invalid command line arguments specified : Unrecognized option: --hello");
127 * @throws IOException Signals that an I/O exception has occurred.
128 * @throws InterruptedException if the test is interrupted
131 public void testBadArg4() throws IOException, InterruptedException {
132 final String[] args = new String[] { "-l", "+++++" };
134 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
135 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
136 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
137 + "State=STOPPED) parameters invalid, listen address is not valid. "
138 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/");
144 * @throws IOException Signals that an I/O exception has occurred.
145 * @throws InterruptedException if the test is interrupted
148 public void testHelp0() throws IOException, InterruptedException {
149 final String[] args = new String[] { "--help" };
151 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
152 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
158 * @throws IOException Signals that an I/O exception has occurred.
159 * @throws InterruptedException if the test is interrupted
162 public void testHelp1() throws IOException, InterruptedException {
163 final String[] args = new String[] { "-h" };
165 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
166 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
172 * @throws IOException Signals that an I/O exception has occurred.
173 * @throws InterruptedException if the test is interrupted
176 public void testPortArgShJo() throws IOException, InterruptedException {
177 final String[] args = new String[] { "-p12321" };
179 final String outString = runEditor(args);
181 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
182 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
183 + "State=READY) starting at http://localhost:12321/apexservices/"));
184 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
185 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
186 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
187 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
188 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
189 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
190 + " State=STOPPED) shut down "));
196 * @throws IOException Signals that an I/O exception has occurred.
197 * @throws InterruptedException if the test is interrupted
200 public void testPortArgShSe() throws IOException, InterruptedException {
201 final String[] args = new String[] { "-p", "12321" };
203 final String outString = runEditor(args);
205 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
206 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
207 + "State=READY) starting at http://localhost:12321/apexservices/"));
208 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
209 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
210 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
211 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
212 .endsWith("(ApexEditorMain: "
213 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
214 + " State=STOPPED) shut down "));
220 * @throws IOException Signals that an I/O exception has occurred.
221 * @throws InterruptedException if the test is interrupted
224 public void testPortArgSpace() throws IOException, InterruptedException {
225 final String[] args = new String[] { "-p 12321" };
227 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
228 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
229 + " parameter error, error parsing argument \"port\" :For input string: \" 12321\"");
233 * Test bad port arg 0.
235 * @throws IOException Signals that an I/O exception has occurred.
236 * @throws InterruptedException if the test is interrupted
239 public void testBadPortArgs0() throws IOException, InterruptedException {
240 final String[] args = new String[] { "-p0" };
242 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
243 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
244 + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
245 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
249 * Test bad port arg 1023.
251 * @throws IOException Signals that an I/O exception has occurred.
252 * @throws InterruptedException if the test is interrupted
255 public void testBadPortArgs1023() throws IOException, InterruptedException {
256 final String[] args = new String[] { "-p1023" };
258 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
259 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
260 + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
261 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
265 * Test bad port arg 65536.
267 * @throws IOException Signals that an I/O exception has occurred.
268 * @throws InterruptedException if the test is interrupted
271 public void testBadPortArgs65536() throws IOException, InterruptedException {
272 final String[] args = new String[] { "-p65536" };
274 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
275 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
276 + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
277 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
283 * @throws IOException Signals that an I/O exception has occurred.
284 * @throws InterruptedException if the test is interrupted
287 public void testTtlArg0() throws IOException, InterruptedException {
288 final String[] args = new String[] { "-t10" };
290 final String outString = runEditor(args);
292 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
293 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
294 + "State=READY) starting at http://localhost:18989/apexservices/"));
295 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
296 .contains("Apex Editor REST endpoint (ApexEditorMain: "
297 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
299 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
300 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
301 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
308 * @throws IOException Signals that an I/O exception has occurred.
309 * @throws InterruptedException if the test is interrupted
312 public void testTtlArg1() throws IOException, InterruptedException {
313 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
315 final String outString = runEditor(args);
317 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
318 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
319 + "State=READY) starting at http://localhost:18989/apexservices/"));
320 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
321 .contains("Apex Editor REST endpoint (ApexEditorMain: "
322 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
324 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
325 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
326 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
331 * Test port TTL arg 0.
333 * @throws IOException Signals that an I/O exception has occurred.
334 * @throws InterruptedException if the test is interrupted
337 public void testPortTtlArg0() throws IOException, InterruptedException {
338 final String[] args = new String[] { "-t", "10", "-p", "12321" };
340 final String outString = runEditor(args);
342 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
343 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
344 + "State=READY) starting at http://localhost:12321/apexservices/"));
345 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
346 .contains("Apex Editor REST endpoint (ApexEditorMain: "
347 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
349 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
350 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
351 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
356 * Test port TTL arg 10.
358 * @throws IOException Signals that an I/O exception has occurred.
359 * @throws InterruptedException if the test is interrupted
362 public void testPortTtlArg1() throws IOException, InterruptedException {
363 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
365 final String outString = runEditor(args);
367 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
368 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
369 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
370 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
371 .contains("Apex Editor REST endpoint (ApexEditorMain: "
372 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
374 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
375 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
376 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
381 * Run the editor for tests.
383 * @param args the args
384 * @return the output string
385 * @throws InterruptedException if the test is interrupted
387 private String runEditor(final String[] args) throws InterruptedException {
388 final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
389 final PrintStream outStream = new PrintStream(outBaStream);
391 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
393 // This test must be started in a thread because we want to intercept the output
394 // in cases where the editor is
395 // started infinitely
396 final Runnable testThread = new Runnable() {
402 new Thread(testThread).start();
403 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
404 || editorMain.getState().equals(EditorState.INITIALIZING)));
405 editorMain.shutdown();
406 final String outString = outBaStream.toString();
407 System.out.println(outString);