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