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);
54 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
55 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
56 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("help=false"));
57 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("restPort=18989"));
58 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=-1"));
59 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("listenAddress=localhost"));
60 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("uploadUrl=null"));
61 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("uploadUserid=null"));
62 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" 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("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("parameter error, too many command line arguments specified : [12321 12322 12323]");
96 * @throws IOException Signals that an I/O exception has occurred.
97 * @throws InterruptedException if the test is interrupted
100 public void testBadArg2() throws IOException, InterruptedException {
101 final String[] args = new String[] { "-z" };
103 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class).hasMessageContaining(
104 "parameter error, invalid command line arguments specified : Unrecognized option: -z");
110 * @throws IOException Signals that an I/O exception has occurred.
111 * @throws InterruptedException if the test is interrupted
114 public void testBadArg3() throws IOException, InterruptedException {
115 final String[] args = new String[] { "--hello" };
117 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class).hasMessageContaining(
118 "parameter error, invalid command line arguments specified : Unrecognized option: --hello");
124 * @throws IOException Signals that an I/O exception has occurred.
125 * @throws InterruptedException if the test is interrupted
128 public void testBadArg4() throws IOException, InterruptedException {
129 final String[] args = new String[] { "-l", "+++++" };
131 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
132 .hasMessageContaining("parameters invalid, listen address is not valid. "
133 + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/");
139 * @throws IOException Signals that an I/O exception has occurred.
140 * @throws InterruptedException if the test is interrupted
143 public void testHelp0() throws IOException, InterruptedException {
144 final String[] args = new String[] { "--help" };
146 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
147 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
153 * @throws IOException Signals that an I/O exception has occurred.
154 * @throws InterruptedException if the test is interrupted
157 public void testHelp1() throws IOException, InterruptedException {
158 final String[] args = new String[] { "-h" };
160 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
161 .hasMessageContaining("usage: org.onap.policy.gui.editors.apex.rest.ApexEditorMain [options...]");
167 * @throws IOException Signals that an I/O exception has occurred.
168 * @throws InterruptedException if the test is interrupted
171 public void testPortArgShJo() throws IOException, InterruptedException {
172 final String[] args = new String[] { "-p12321" };
174 final String outString = runEditor(args);
176 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
177 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
178 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("localhost:12321"));
179 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
185 * @throws IOException Signals that an I/O exception has occurred.
186 * @throws InterruptedException if the test is interrupted
189 public void testPortArgShSe() throws IOException, InterruptedException {
190 final String[] args = new String[] { "-p", "12321" };
192 final String outString = runEditor(args);
194 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
195 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
196 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("localhost:12321"));
197 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
203 * @throws IOException Signals that an I/O exception has occurred.
204 * @throws InterruptedException if the test is interrupted
207 public void testPortArgSpace() throws IOException, InterruptedException {
208 final String[] args = new String[] { "-p 12321" };
210 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
211 .hasMessageContaining("parameter error, error parsing argument \"port\" :For input string: \" 12321\"");
215 * Test bad port arg 0.
217 * @throws IOException Signals that an I/O exception has occurred.
218 * @throws InterruptedException if the test is interrupted
221 public void testBadPortArgs0() throws IOException, InterruptedException {
222 final String[] args = new String[] { "-p0" };
224 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
225 .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
229 * Test bad port arg 1023.
231 * @throws IOException Signals that an I/O exception has occurred.
232 * @throws InterruptedException if the test is interrupted
235 public void testBadPortArgs1023() throws IOException, InterruptedException {
236 final String[] args = new String[] { "-p1023" };
238 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
239 .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
243 * Test bad port arg 65536.
245 * @throws IOException Signals that an I/O exception has occurred.
246 * @throws InterruptedException if the test is interrupted
249 public void testBadPortArgs65536() throws IOException, InterruptedException {
250 final String[] args = new String[] { "-p65536" };
252 assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
253 .hasMessageContaining("parameters invalid, port must be between 1024 and 65535");
259 * @throws IOException Signals that an I/O exception has occurred.
260 * @throws InterruptedException if the test is interrupted
263 public void testTtlArg0() throws IOException, InterruptedException {
264 final String[] args = new String[] { "-t10" };
266 final String outString = runEditor(args);
268 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
269 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
270 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
271 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
277 * @throws IOException Signals that an I/O exception has occurred.
278 * @throws InterruptedException if the test is interrupted
281 public void testTtlArg1() throws IOException, InterruptedException {
282 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
284 final String outString = runEditor(args);
286 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
287 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
288 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
289 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
293 * Test port TTL arg 0.
295 * @throws IOException Signals that an I/O exception has occurred.
296 * @throws InterruptedException if the test is interrupted
299 public void testPortTtlArg0() throws IOException, InterruptedException {
300 final String[] args = new String[] { "-t", "10", "-p", "12321" };
302 final String outString = runEditor(args);
304 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
305 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
306 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
307 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
311 * Test port TTL arg 10.
313 * @throws IOException Signals that an I/O exception has occurred.
314 * @throws InterruptedException if the test is interrupted
317 public void testPortTtlArg1() throws IOException, InterruptedException {
318 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
320 final String outString = runEditor(args);
322 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain:"));
323 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("State=RUNNING) started"));
324 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("timeToLive=10"));
325 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith(" State=STOPPED) shut down "));
329 * Run the editor for tests.
331 * @param args the args
332 * @return the output string
333 * @throws InterruptedException if the test is interrupted
335 private String runEditor(final String[] args) throws InterruptedException {
336 ParameterService.clear();
337 final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
338 final PrintStream outStream = new PrintStream(outBaStream);
340 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
342 // This test must be started in a thread because we want to intercept the output
343 // in cases where the editor is
344 // started infinitely
345 final Runnable testThread = new Runnable() {
351 new Thread(testThread).start();
352 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
353 || editorMain.getState().equals(EditorState.INITIALIZING)));
354 editorMain.shutdown();
355 final String outString = outBaStream.toString();
356 System.out.println(outString);