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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
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.apex.client.editor.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]+", " ").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");
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.apex.client.editor.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.apex.client.editor.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]+", " ").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]+", " ").endsWith("(ApexEditorMain: "
212 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=-1sec],"
213 + " 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]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
296 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
298 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
299 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
306 * @throws IOException Signals that an I/O exception has occurred.
307 * @throws InterruptedException if the test is interrupted
310 public void testTtlArg1() throws IOException, InterruptedException {
311 final String[] args = new String[] { "-t", "10", "-l", "localhost" };
313 final String outString = runEditor(args);
315 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
316 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], "
317 + "State=READY) starting at http://localhost:18989/apexservices/"));
318 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
319 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=RUNNING)"
321 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
322 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
327 * Test port TTL arg 0.
329 * @throws IOException Signals that an I/O exception has occurred.
330 * @throws InterruptedException if the test is interrupted
333 public void testPortTtlArg0() throws IOException, InterruptedException {
334 final String[] args = new String[] { "-t", "10", "-p", "12321" };
336 final String outString = runEditor(args);
338 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
339 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], "
340 + "State=READY) starting at http://localhost:12321/apexservices/"));
341 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
342 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=RUNNING)"
344 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
345 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
351 * Test port TTL arg 10.
353 * @throws IOException Signals that an I/O exception has occurred.
354 * @throws InterruptedException if the test is interrupted
357 public void testPortTtlArg1() throws IOException, InterruptedException {
358 final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
360 final String outString = runEditor(args);
362 assertTrue(outString.startsWith("Apex Editor REST endpoint (ApexEditorMain: "
363 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], "
364 + "State=READY) starting at http://127.0.0.1:12321/apexservices/"));
365 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").contains("Apex Editor REST endpoint (ApexEditorMain: "
366 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=RUNNING)"
368 assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
369 + "Config=[ApexEditorParameters: URI=http://127.0.0.1:12321/apexservices/, TTL=10sec], State=STOPPED)"
374 * Run the editor for tests.
376 * @param args the args
377 * @return the output string
378 * @throws InterruptedException if the test is interrupted
380 private String runEditor(final String[] args) throws InterruptedException {
381 final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
382 final PrintStream outStream = new PrintStream(outBaStream);
384 final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
386 // This test must be started in a thread because we want to intercept the output in cases where the editor is
387 // started infinitely
388 final Runnable testThread = new Runnable() {
394 new Thread(testThread).start();
395 await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
396 || editorMain.getState().equals(EditorState.INITIALIZING)));
397 editorMain.shutdown();
398 final String outString = outBaStream.toString();
399 System.out.println(outString);