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