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