Changes for checkstyle 8.32
[policy/apex-pdp.git] / client / client-full / src / test / java / org / onap / policy / apex / client / full / rest / ServicesRestMainTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.full.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.catchThrowable;
26 import static org.awaitility.Awaitility.await;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.InputStream;
31 import java.io.PrintStream;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.After;
34 import org.junit.Test;
35
36 /**
37  * Test the periodic event manager utility.
38  */
39 public class ServicesRestMainTest {
40     private static InputStream systemInStream = System.in;
41
42     @Test
43     public void testServicesMainClientOk() {
44         final String[] eventArgs = {"-t", "1", "-p", "1256"};
45         assertThatCode(() -> ApexServicesRestMain.main(eventArgs)).doesNotThrowAnyException();
46     }
47
48     @Test
49     public void testServicesClientNoOptions() {
50         final String[] eventArgs = new String[] {};
51
52         assertThat(testApexServicesRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
53
54     }
55
56     @Test
57     public void testServicesClientBadOptions() {
58         final String[] eventArgs = {"-zabbu"};
59         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
60
61         assertThat(thrown).isInstanceOf(Exception.class)
62             .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
63                 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
64     }
65
66     @Test
67     public void testServicesClientHelp() {
68         final String[] eventArgs = {"-h"};
69         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
70
71         assertThat(thrown).isInstanceOf(Exception.class)
72             .hasMessageContaining("usage: org.onap.policy.apex.client.full.rest.ApexServicesRestMain [options...]");
73     }
74
75     @Test
76     public void testServicesClientPortBad() {
77         final String[] eventArgs = {"-p", "hello"};
78
79         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
80
81         assertThat(thrown).isInstanceOf(Exception.class)
82             .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
83                 + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
84
85     }
86
87     @Test
88     public void testServicesClientPortNegative() {
89         final String[] eventArgs = {"-p", "-1"};
90
91         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
92
93         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
94             "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
95                 + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
96                 + "port must be greater than 1023 and less than 65536");
97
98     }
99
100     @Test
101     public void testServicesClientTtlTooSmall() {
102         final String[] eventArgs = {"-t", "-2"};
103
104         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
105
106         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
107             "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
108                 + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
109                 + "time to live must be greater than -1 (set to -1 to wait forever)");
110     }
111
112     @Test
113     public void testServicesClientTooManyPars() {
114         final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
115         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
116
117         assertThat(thrown).isInstanceOf(Exception.class)
118             .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
119                 + "parameter error, too many command line arguments specified : [aaa, bbb]");
120     }
121
122     @Test
123     public void testServicesClientTtlNotNumber() {
124         final String[] eventArgs = {"-t", "timetolive"};
125         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
126
127         assertThat(thrown).isInstanceOf(Exception.class)
128             .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) "
129                 + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"");
130     }
131
132     @Test
133     public void testServicesClientTtlSetValue() {
134         final String[] eventArgs = {"-t", "3", "-p", "1257"};
135         assertThatCode(() -> {
136             ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
137             monRestMain.init();
138             monRestMain.shutdown();
139         }).doesNotThrowAnyException();
140
141     }
142
143     @Test
144     public void testServicesClientPortTooBig() {
145         final String[] eventArgs = {"-p", "65536"};
146         Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out));
147
148         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
149             "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: "
150                 + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
151                 + "port must be greater than 1023 and less than 65536");
152     }
153
154     @Test
155     public void testServicesOneSecStart() {
156         final String[] eventArgs = {"-t", "1", "-p", "1258"};
157
158         assertThatCode(() -> {
159             ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
160             monRestMain.init();
161             monRestMain.shutdown();
162         }).doesNotThrowAnyException();
163     }
164
165     @Test
166     public void testServicesForeverStart() {
167         final String[] eventArgs = {"-t", "-1", "-p", "1259"};
168
169         ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out);
170
171         Thread monThread = new Thread() {
172             @Override
173             public void run() {
174                 monRestMain.init();
175             }
176         };
177
178         assertThatCode(() -> {
179             monThread.start();
180             await().atMost(6, TimeUnit.SECONDS)
181                 .until(() -> monRestMain.getState().equals(ApexServicesRestMain.EditorState.RUNNING));
182             monRestMain.shutdown();
183         }).doesNotThrowAnyException();
184     }
185
186     @After
187     public void cleanUpStreamSetting() {
188         System.setIn(systemInStream);
189     }
190
191     /**
192      * Run the application.
193      *
194      * @param eventArgs the command arguments
195      * @return a string containing the command output
196      */
197     private String testApexServicesRestMainConstructor(final String[] eventArgs) {
198         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
199         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
200
201         new ApexServicesRestMain(eventArgs, new PrintStream(baosOut, true));
202
203         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
204         System.setIn(testInput);
205
206         String outString = baosOut.toString();
207         String errString = baosErr.toString();
208
209         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
210     }
211 }