2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertTrue;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain.EditorState;
38 * Test Apex Editor Startup.
40 public class ApexEditorStartupTest {
41 // CHECKSTYLE:OFF: MagicNumber
46 * @throws IOException Signals that an I/O exception has occurred.
47 * @throws InterruptedException if the test is interrupted
50 public void testNoArgs() throws IOException, InterruptedException {
51 final String[] args = new String[] {};
53 final String outString = runEditor(args);
55 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
56 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
57 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("help=false"));
58 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("restPort=18989"));
59 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=-1"));
60 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("listenAddress=localhost"));
61 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("uploadUrl=null"));
62 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("uploadUserid=null"));
63 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
69 * @throws IOException Signals that an I/O exception has occurred.
70 * @throws InterruptedException if the test is interrupted
73 public void testBadArg0() throws IOException, InterruptedException {
74 final String[] args = new String[] { "12321" };
76 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
77 .hasMessageContaining("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("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).hasMessageContaining(
105 "parameter error, invalid command line arguments specified : Unrecognized option: -z");
111 * @throws IOException Signals that an I/O exception has occurred.
112 * @throws InterruptedException if the test is interrupted
115 public void testBadArg3() throws IOException, InterruptedException {
116 final String[] args = new String[] { "--hello" };
118 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class).hasMessageContaining(
119 "parameter error, invalid command line arguments specified : Unrecognized option: --hello");
125 * @throws IOException Signals that an I/O exception has occurred.
126 * @throws InterruptedException if the test is interrupted
129 public void testBadArg4() throws IOException, InterruptedException {
130 final String[] args = new String[] { "-l", "+++++" };
132 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
133 .hasMessageContaining("parameters invalid, listen address is not valid. "
134 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/");
140 * @throws IOException Signals that an I/O exception has occurred.
141 * @throws InterruptedException if the test is interrupted
144 public void testHelp0() throws IOException, InterruptedException {
145 final String[] args = new String[] { "--help" };
147 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
148 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
154 * @throws IOException Signals that an I/O exception has occurred.
155 * @throws InterruptedException if the test is interrupted
158 public void testHelp1() throws IOException, InterruptedException {
159 final String[] args = new String[] { "-h" };
161 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
162 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
168 * @throws IOException Signals that an I/O exception has occurred.
169 * @throws InterruptedException if the test is interrupted
172 public void testPortArgShJo() throws IOException, InterruptedException {
173 final String[] args = new String[] { "-p12321" };
175 final String outString = runEditor(args);
177 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
178 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
179 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("localhost:12321"));
180 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
186 * @throws IOException Signals that an I/O exception has occurred.
187 * @throws InterruptedException if the test is interrupted
190 public void testPortArgShSe() throws IOException, InterruptedException {
191 final String[] args = new String[] { "-p", "12321" };
193 final String outString = runEditor(args);
195 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
196 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
197 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("localhost:12321"));
198 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
204 * @throws IOException Signals that an I/O exception has occurred.
205 * @throws InterruptedException if the test is interrupted
208 public void testPortArgSpace() throws IOException, InterruptedException {
209 final String[] args = new String[] { "-p 12321" };
211 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
212 .hasMessageContaining("parameter error, error parsing argument \"port\" :For input string: \" 12321\"");
216 * Test bad port arg 0.
218 * @throws IOException Signals that an I/O exception has occurred.
219 * @throws InterruptedException if the test is interrupted
222 public void testBadPortArgs0() throws IOException, InterruptedException {
223 final String[] args = new String[] { "-p0" };
225 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
226 .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
230 * Test bad port arg 1023.
232 * @throws IOException Signals that an I/O exception has occurred.
233 * @throws InterruptedException if the test is interrupted
236 public void testBadPortArgs1023() throws IOException, InterruptedException {
237 final String[] args = new String[] { "-p1023" };
239 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
240 .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
244 * Test bad port arg 65536.
246 * @throws IOException Signals that an I/O exception has occurred.
247 * @throws InterruptedException if the test is interrupted
250 public void testBadPortArgs65536() throws IOException, InterruptedException {
251 final String[] args = new String[] { "-p65536" };
253 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
254 .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
260 * @throws IOException Signals that an I/O exception has occurred.
261 * @throws InterruptedException if the test is interrupted
264 public void testTtlArg0() throws IOException, InterruptedException {
265 final String[] args = new String[] { "-t10" };
267 final String outString = runEditor(args);
269 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
270 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
271 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
272 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
278 * @throws IOException Signals that an I/O exception has occurred.
279 * @throws InterruptedException if the test is interrupted
282 public void testTtlArg1() throws IOException, InterruptedException {
283 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
285 final String outString = runEditor(args);
287 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
288 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
289 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
290 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
294 * Test port TTL arg 0.
296 * @throws IOException Signals that an I/O exception has occurred.
297 * @throws InterruptedException if the test is interrupted
300 public void testPortTtlArg0() throws IOException, InterruptedException {
301 final String[] args = new String[] { "-t", "10", "-p", "12321" };
303 final String outString = runEditor(args);
305 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
306 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
307 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
308 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
312 * Test port TTL arg 10.
314 * @throws IOException Signals that an I/O exception has occurred.
315 * @throws InterruptedException if the test is interrupted
318 public void testPortTtlArg1() throws IOException, InterruptedException {
319 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
321 final String outString = runEditor(args);
323 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
324 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
325 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
326 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
330 * Run the editor for tests.
332 * @param args the args
333 * @return the output string
334 * @throws InterruptedException if the test is interrupted
336 private String runEditor(final String[] args) throws InterruptedException {
337 ParameterService.clear();
338 final var outBaStream = new ByteArrayOutputStream();
339 final var outStream = new PrintStream(outBaStream);
341 final var editorMain = new ApexEditorMain(args, outStream);
343 // This test must be started in a thread because we want to intercept the output
344 // in cases where the editor is
345 // started infinitely
346 final var testThread = new Runnable() {
352 new Thread(testThread).start();
353 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
354 || editorMain.getState().equals(EditorState.INITIALIZING)));
355 editorMain.shutdown();
356 final String outString = outBaStream.toString();
357 System.out.println(outString);