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