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