4d6c93222ac5d0142c8c75890804f13d2e024012
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.client.editor.rest;
23
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;
28
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;
35
36 /**
37  * Test Apex Editor Startup.
38  */
39 public class ApexEditorStartupTest {
40     // CHECKSTYLE:OFF: MagicNumber
41
42     /**
43      * Test no args.
44      *
45      * @throws IOException Signals that an I/O exception has occurred.
46      * @throws InterruptedException if the test is interrupted
47      */
48     @Test
49     public void testNoArgs() throws IOException, InterruptedException {
50         final String[] args = new String[] {};
51
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 "));
62     }
63
64     /**
65      * Test bad arg 0.
66      *
67      * @throws IOException Signals that an I/O exception has occurred.
68      * @throws InterruptedException if the test is interrupted
69      */
70     @Test
71     public void testBadArg0() throws IOException, InterruptedException {
72         final String[] args = new String[] { "12321" };
73
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]");
77     }
78
79     /**
80      * Test bad arg 1.
81      *
82      * @throws IOException Signals that an I/O exception has occurred.
83      * @throws InterruptedException if the test is interrupted
84      */
85     @Test
86     public void testBadArg1() throws IOException, InterruptedException {
87         final String[] args = new String[] { "12321 12322 12323" };
88
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]");
92     }
93
94     /**
95      * Test bad arg 2.
96      *
97      * @throws IOException Signals that an I/O exception has occurred.
98      * @throws InterruptedException if the test is interrupted
99      */
100     @Test
101     public void testBadArg2() throws IOException, InterruptedException {
102         final String[] args = new String[] { "-z" };
103
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");
107     }
108
109     /**
110      * Test bad arg 3.
111      *
112      * @throws IOException Signals that an I/O exception has occurred.
113      * @throws InterruptedException if the test is interrupted
114      */
115     @Test
116     public void testBadArg3() throws IOException, InterruptedException {
117         final String[] args = new String[] { "--hello" };
118
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");
122     }
123
124
125     /**
126      * Test bad arg 4.
127      *
128      * @throws IOException Signals that an I/O exception has occurred.
129      * @throws InterruptedException if the test is interrupted
130      */
131     @Test
132     public void testBadArg4() throws IOException, InterruptedException {
133         final String[] args = new String[] { "-l", "+++++" };
134
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/");
140     }
141
142     /**
143      * Test help 0.
144      *
145      * @throws IOException Signals that an I/O exception has occurred.
146      * @throws InterruptedException if the test is interrupted
147      */
148     @Test
149     public void testHelp0() throws IOException, InterruptedException {
150         final String[] args = new String[] { "--help" };
151
152         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
153             .hasMessageContaining("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]");
154     }
155
156     /**
157      * Test help 1.
158      *
159      * @throws IOException Signals that an I/O exception has occurred.
160      * @throws InterruptedException if the test is interrupted
161      */
162     @Test
163     public void testHelp1() throws IOException, InterruptedException {
164         final String[] args = new String[] { "-h" };
165
166         assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class)
167             .hasMessageContaining("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]");
168     }
169
170     /**
171      * Test port arg.
172      *
173      * @throws IOException Signals that an I/O exception has occurred.
174      * @throws InterruptedException if the test is interrupted
175      */
176     @Test
177     public void testPortArgShJo() throws IOException, InterruptedException {
178         final String[] args = new String[] { "-p12321" };
179
180         final String outString = runEditor(args);
181
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 "));
191     }
192
193     /**
194      * Test port arg.
195      *
196      * @throws IOException Signals that an I/O exception has occurred.
197      * @throws InterruptedException if the test is interrupted
198      */
199     @Test
200     public void testPortArgShSe() throws IOException, InterruptedException {
201         final String[] args = new String[] { "-p", "12321" };
202
203         final String outString = runEditor(args);
204
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 "));
214     }
215
216
217     /**
218      * Test port arg.
219      *
220      * @throws IOException Signals that an I/O exception has occurred.
221      * @throws InterruptedException if the test is interrupted
222      */
223     @Test
224     public void testPortArgSpace() throws IOException, InterruptedException {
225         final String[] args = new String[] { "-p 12321" };
226
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\"");
230     }
231
232     /**
233      * Test bad port arg 0.
234      *
235      * @throws IOException Signals that an I/O exception has occurred.
236      * @throws InterruptedException if the test is interrupted
237      */
238     @Test
239     public void testBadPortArgs0() throws IOException, InterruptedException {
240         final String[] args = new String[] { "-p0" };
241
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");
246     }
247
248     /**
249      * Test bad port arg 1023.
250      *
251      * @throws IOException Signals that an I/O exception has occurred.
252      * @throws InterruptedException if the test is interrupted
253      */
254     @Test
255     public void testBadPortArgs1023() throws IOException, InterruptedException {
256         final String[] args = new String[] { "-p1023" };
257
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");
262     }
263
264     /**
265      * Test bad port arg 65536.
266      *
267      * @throws IOException Signals that an I/O exception has occurred.
268      * @throws InterruptedException if the test is interrupted
269      */
270     @Test
271     public void testBadPortArgs65536() throws IOException, InterruptedException {
272         final String[] args = new String[] { "-p65536" };
273
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");
278     }
279
280     /**
281      * Test TTL arg 0.
282      *
283      * @throws IOException Signals that an I/O exception has occurred.
284      * @throws InterruptedException if the test is interrupted
285      */
286     @Test
287     public void testTtlArg0() throws IOException, InterruptedException {
288         final String[] args = new String[] { "-t10" };
289
290         final String outString = runEditor(args);
291
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)"
297                 + " started"));
298         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
299                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
300                 + " shut down "));
301     }
302
303     /**
304      * Test TTL arg 10.
305      *
306      * @throws IOException Signals that an I/O exception has occurred.
307      * @throws InterruptedException if the test is interrupted
308      */
309     @Test
310     public void testTtlArg1() throws IOException, InterruptedException {
311         final String[] args = new String[] { "-t", "10", "-l", "localhost" };
312
313         final String outString = runEditor(args);
314
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)"
320                 + " started"));
321         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
322                 + "Config=[ApexEditorParameters: URI=http://localhost:18989/apexservices/, TTL=10sec], State=STOPPED)"
323                 + " shut down "));
324     }
325
326     /**
327      * Test port TTL arg 0.
328      *
329      * @throws IOException Signals that an I/O exception has occurred.
330      * @throws InterruptedException if the test is interrupted
331      */
332     @Test
333     public void testPortTtlArg0() throws IOException, InterruptedException {
334         final String[] args = new String[] { "-t", "10", "-p", "12321" };
335
336         final String outString = runEditor(args);
337
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)"
343                 + " started"));
344         assertTrue(outString.replaceAll("[\\r?\\n]+", " ").endsWith("Apex Editor REST endpoint (ApexEditorMain: "
345                 + "Config=[ApexEditorParameters: URI=http://localhost:12321/apexservices/, TTL=10sec], State=STOPPED)"
346                 + " shut down "));
347     }
348
349
350     /**
351      * Test port TTL arg 10.
352      *
353      * @throws IOException Signals that an I/O exception has occurred.
354      * @throws InterruptedException if the test is interrupted
355      */
356     @Test
357     public void testPortTtlArg1() throws IOException, InterruptedException {
358         final String[] args = new String[] { "--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1" };
359
360         final String outString = runEditor(args);
361
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)"
367                 + " started"));
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)"
370                 + " shut down "));
371     }
372
373     /**
374      * Run the editor for tests.
375      *
376      * @param args the args
377      * @return the output string
378      * @throws InterruptedException if the test is interrupted
379      */
380     private String runEditor(final String[] args) throws InterruptedException {
381         final ByteArrayOutputStream outBaStream = new ByteArrayOutputStream();
382         final PrintStream outStream = new PrintStream(outBaStream);
383
384         final ApexEditorMain editorMain = new ApexEditorMain(args, outStream);
385
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() {
389             @Override
390             public void run() {
391                 editorMain.init();
392             }
393         };
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);
400         return outString;
401     }
402 }