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.common.parameters.ParameterService;
34 import org.onap.policy.gui.editors.apex.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]+", " ")
60 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
61 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=-1sec],"
62 + " State=STOPPED) shut down "));
68 * @throws IOException Signals that an I/O exception has occurred.
69 * @throws InterruptedException if the test is interrupted
72 public void testBadArg0() throws IOException, InterruptedException {
73 final String[] args = new String[] { "12321" };
75 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
76 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
77 + " parameter error, too many command line arguments specified : [12321]");
83 * @throws IOException Signals that an I/O exception has occurred.
84 * @throws InterruptedException if the test is interrupted
87 public void testBadArg1() throws IOException, InterruptedException {
88 final String[] args = new String[] { "12321 12322 12323" };
90 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
91 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
92 + " parameter error, too many command line arguments specified : [12321 12322 12323]");
98 * @throws IOException Signals that an I/O exception has occurred.
99 * @throws InterruptedException if the test is interrupted
102 public void testBadArg2() throws IOException, InterruptedException {
103 final String[] args = new String[] { "-z" };
105 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
106 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
107 + " parameter error, invalid command line arguments specified : Unrecognized option: -z");
113 * @throws IOException Signals that an I/O exception has occurred.
114 * @throws InterruptedException if the test is interrupted
117 public void testBadArg3() throws IOException, InterruptedException {
118 final String[] args = new String[] { "--hello" };
120 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
121 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
122 + " parameter error, invalid command line arguments specified : Unrecognized option: --hello");
128 * @throws IOException Signals that an I/O exception has occurred.
129 * @throws InterruptedException if the test is interrupted
132 public void testBadArg4() throws IOException, InterruptedException {
133 final String[] args = new String[] { "-l", "+++++" };
135 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
136 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
137 + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], "
138 + "State=STOPPED) parameters invalid, listen address is not valid. "
139 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/");
145 * @throws IOException Signals that an I/O exception has occurred.
146 * @throws InterruptedException if the test is interrupted
149 public void testHelp0() throws IOException, InterruptedException {
150 final String[] args = new String[] { "--help" };
152 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
153 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
159 * @throws IOException Signals that an I/O exception has occurred.
160 * @throws InterruptedException if the test is interrupted
163 public void testHelp1() throws IOException, InterruptedException {
164 final String[] args = new String[] { "-h" };
166 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
167 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
173 * @throws IOException Signals that an I/O exception has occurred.
174 * @throws InterruptedException if the test is interrupted
177 public void testPortArgShJo() throws IOException, InterruptedException {
178 final String[] args = new String[] { "-p12321" };
180 final String outString = runEditor(args);
182 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
183 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
184 + "State=READY) starting at http://localhost:12321/apexservices/"));
185 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
186 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
187 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
188 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
189 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
190 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
191 + " State=STOPPED) shut down "));
197 * @throws IOException Signals that an I/O exception has occurred.
198 * @throws InterruptedException if the test is interrupted
201 public void testPortArgShSe() throws IOException, InterruptedException {
202 final String[] args = new String[] { "-p", "12321" };
204 final String outString = runEditor(args);
206 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
207 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
208 + "State=READY) starting at http://localhost:12321/apexservices/"));
209 assertTrue(outString.contains("Apex Editor REST endpoint (ApexEditorMain: "
210 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec], "
211 + "State=RUNNING) started at http://localhost:12321/apexservices/"));
212 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
213 .endsWith("(ApexEditorMain: "
214 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
215 + " State=STOPPED) shut down "));
221 * @throws IOException Signals that an I/O exception has occurred.
222 * @throws InterruptedException if the test is interrupted
225 public void testPortArgSpace() throws IOException, InterruptedException {
226 final String[] args = new String[] { "-p 12321" };
228 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
229 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)"
230 + " parameter error, error parsing argument \"port\" :For input string: \" 12321\"");
234 * Test bad port arg 0.
236 * @throws IOException Signals that an I/O exception has occurred.
237 * @throws InterruptedException if the test is interrupted
240 public void testBadPortArgs0() throws IOException, InterruptedException {
241 final String[] args = new String[] { "-p0" };
243 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
244 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
245 + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], "
246 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
250 * Test bad port arg 1023.
252 * @throws IOException Signals that an I/O exception has occurred.
253 * @throws InterruptedException if the test is interrupted
256 public void testBadPortArgs1023() throws IOException, InterruptedException {
257 final String[] args = new String[] { "-p1023" };
259 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
260 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
261 + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], "
262 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
266 * Test bad port arg 65536.
268 * @throws IOException Signals that an I/O exception has occurred.
269 * @throws InterruptedException if the test is interrupted
272 public void testBadPortArgs65536() throws IOException, InterruptedException {
273 final String[] args = new String[] { "-p65536" };
275 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
276 .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: "
277 + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], "
278 + "State=STOPPED) parameters invalid, port must be between 1024 and 65535");
284 * @throws IOException Signals that an I/O exception has occurred.
285 * @throws InterruptedException if the test is interrupted
288 public void testTtlArg0() throws IOException, InterruptedException {
289 final String[] args = new String[] { "-t10" };
291 final String outString = runEditor(args);
293 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
294 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
295 + "State=READY) starting at http://localhost:18989/apexservices/"));
296 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
297 .contains("Apex Editor REST endpoint (ApexEditorMain: "
298 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
300 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
301 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
302 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
309 * @throws IOException Signals that an I/O exception has occurred.
310 * @throws InterruptedException if the test is interrupted
313 public void testTtlArg1() throws IOException, InterruptedException {
314 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
316 final String outString = runEditor(args);
318 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
319 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
320 + "State=READY) starting at http://localhost:18989/apexservices/"));
321 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
322 .contains("Apex Editor REST endpoint (ApexEditorMain: "
323 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
325 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
326 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
327 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
332 * Test port TTL arg 0.
334 * @throws IOException Signals that an I/O exception has occurred.
335 * @throws InterruptedException if the test is interrupted
338 public void testPortTtlArg0() throws IOException, InterruptedException {
339 final String[] args = new String[] { "-t", "10", "-p", "12321" };
341 final String outString = runEditor(args);
343 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
344 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
345 + "State=READY) starting at http://localhost:12321/apexservices/"));
346 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
347 .contains("Apex Editor REST endpoint (ApexEditorMain: "
348 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
350 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
351 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
352 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
357 * Test port TTL arg 10.
359 * @throws IOException Signals that an I/O exception has occurred.
360 * @throws InterruptedException if the test is interrupted
363 public void testPortTtlArg1() throws IOException, InterruptedException {
364 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
366 final String outString = runEditor(args);
368 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
369 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
370 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
371 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
372 .contains("Apex Editor REST endpoint (ApexEditorMain: "
373 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
375 assertTrue(outString.replaceAll("[\\r?\\n]+", " ")
376 .endsWith("Apex Editor REST endpoint (ApexEditorMain: "
377 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
382 * Run the editor for tests.
384 * @param args the args
385 * @return the output string
386 * @throws InterruptedException if the test is interrupted
388 private String runEditor(final String[] args) throws InterruptedException {
389 ParameterService.clear();
390 final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
391 final PrintStream outStream = new PrintStream(outBaStream);
393 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
395 // This test must be started in a thread because we want to intercept the output
396 // in cases where the editor is
397 // started infinitely
398 final Runnable testThread = new Runnable() {
404 new Thread(testThread).start();
405 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
406 || editorMain.getState().equals(EditorState.INITIALIZING)));
407 editorMain.shutdown();
408 final String outString = outBaStream.toString();
409 System.out.println(outString);