245900d7d2f3421dd1878e8dbd16c4ffe19e802d
[policy/apex-pdp.git] /
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.deployment.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
34 import org.junit.After;
35 import org.junit.Test;
36
37 /**
38  * Test the periodic event manager utility.
39  */
40 public class DeploymentRestMainTest {
41     private static InputStream systemInStream = System.in;
42
43     @Test
44     public void testDeploymentClientOk() {
45         final String[] eventArgs = {"-t", "1", "-p", "1256"};
46         assertThatCode(() -> ApexDeploymentRestMain.main(eventArgs)).doesNotThrowAnyException();
47     }
48
49     @Test
50     public void testDeploymentClientNoOptions() {
51         final String[] eventArgs = new String[]
52             {};
53         assertThat(testApexDeploymentRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n");
54     }
55
56     @Test
57     public void testDeploymentClientBadOptions() {
58         final String[] eventArgs =
59             { "-zabbu" };
60         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
61
62         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
63                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
64                         + "parameter error, invalid command line arguments specified "
65                         + ": Unrecognized option: -zabbu");
66     }
67
68     @Test
69     public void testDeploymentClientHelp() {
70         final String[] eventArgs =
71             { "-h" };
72
73         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
74
75         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
76                 "usage: org.onap.policy.apex.client.deployment.rest.ApexDeploymentRestMain [options...]");
77
78     }
79
80     @Test
81     public void testDeploymentClientPortBad() {
82         final String[] eventArgs =
83             { "-p", "hello" };
84
85         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
86
87         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
88                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
89                         + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
90
91     }
92
93     @Test
94     public void testDeploymentClientPortNegative() {
95         final String[] eventArgs =
96             { "-p", "-1" };
97
98         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
99
100         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
101                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
102                         + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
103                         + "port must be greater than 1023 and less than 65536");
104
105     }
106
107     @Test
108     public void testDeploymentClientTtlTooSmall() {
109         final String[] eventArgs =
110             { "-t", "-2" };
111
112         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
113
114         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
115                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
116                         + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
117                         + "time to live must be greater than -1 (set to -1 to wait forever)");
118
119     }
120
121     @Test
122     public void testDeploymentClientTooManyPars() {
123         final String[] eventArgs =
124             { "-t", "10", "-p", "12344", "aaa", "bbb" };
125
126         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
127
128         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
129                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
130                         + "parameter error, too many command line arguments specified : [aaa, bbb]");
131     }
132
133     @Test
134     public void testDeploymentClientDefaultPars() {
135         assertThatCode(() -> {
136             ApexDeploymentRest monRest = new ApexDeploymentRest();
137             monRest.shutdown();
138         }).doesNotThrowAnyException();
139
140     }
141
142     @Test
143     public void testDeploymentClientTtlNotNumber() {
144         final String[] eventArgs =
145             { "-t", "timetolive" };
146
147         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
148
149         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
150                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[null], State=STOPPED) "
151                         + "parameter error, error parsing argument \"time-to-live\" :"
152                         + "For input string: \"timetolive\"");
153
154     }
155
156     @Test
157     public void testDeploymentClientPortTooBig() {
158         final String[] eventArgs =
159             { "-p", "65536" };
160
161         Throwable thrown = catchThrowable(() -> new ApexDeploymentRestMain(eventArgs, System.out));
162
163         assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining(
164                 "Apex Services REST endpoint (ApexDeploymentRestMain: Config=[ApexDeploymentRestParameters: "
165                         + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
166                         + "port must be greater than 1023 and less than 65536");
167     }
168
169     @Test
170     public void testDeploymentOneSecStart() {
171         final String[] eventArgs =
172             { "-t", "1" };
173
174         assertThatCode(() -> {
175             ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
176             monRestMain.init();
177             monRestMain.shutdown();
178         }).doesNotThrowAnyException();
179
180     }
181
182     @Test
183     public void testDeploymentForeverStart() {
184         final String[] eventArgs =
185             { "-t", "-1" };
186
187         ApexDeploymentRestMain monRestMain = new ApexDeploymentRestMain(eventArgs, System.out);
188
189         Thread monThread = new Thread() {
190             @Override
191             public void run() {
192                 monRestMain.init();
193             }
194         };
195
196         assertThatCode(() -> {
197             monThread.start();
198             await().atMost(2, TimeUnit.SECONDS).until(
199                     () -> monRestMain.getState().equals(ApexDeploymentRestMain.ServicesState.RUNNING));
200             monRestMain.shutdown();
201         }).doesNotThrowAnyException();
202     }
203
204     @After
205     public void cleanUpStreamSetting() {
206         System.setIn(systemInStream);
207     }
208
209     /**
210      * Run the application.
211      *
212      * @param eventArgs the command arguments
213      * @return a string containing the command output
214      */
215     private String testApexDeploymentRestMainConstructor(final String[] eventArgs) {
216         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
217         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
218
219         new ApexDeploymentRestMain(eventArgs, new PrintStream(baosOut, true));
220
221         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
222         System.setIn(testInput);
223
224         String outString = baosOut.toString();
225         String errString = baosErr.toString();
226
227         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
228     }
229 }