b376c9bfa71d809be98435070fc20f9c223e45f8
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.client.monitoring.rest;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.InputStream;
33 import java.io.PrintStream;
34 import java.util.concurrent.TimeUnit;
35 import org.junit.Test;
36
37 /**
38  * Test the periodic event manager utility.
39  */
40 public class MonitoringRestMainTest {
41     @Test
42     public void testMonitoringClientBad() {
43         final String[] eventArgs = {"-z"};
44         assertThatCode(() -> ApexMonitoringRestMain.main(eventArgs)).doesNotThrowAnyException();
45     }
46
47     @Test
48     public void testMonitoringClientOk() {
49         final String[] eventArgs = {"-t", "1"};
50         assertThatCode(() -> ApexMonitoringRestMain.main(eventArgs)).doesNotThrowAnyException();
51     }
52
53     @Test
54     public void testMonitoringClientNoOptions() {
55         final String[] eventArgs = new String[] {};
56
57         final String outputString = testApexMonitoringRestMainConstructor(eventArgs);
58
59         System.err.println(outputString);
60         assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString);
61     }
62
63     @Test
64     public void testMonitoringClientBadOptions() {
65         final String[] eventArgs = {"-zabbu"};
66
67         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
68             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
69                 + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu");
70     }
71
72     @Test
73     public void testMonitoringClientHelp() {
74         final String[] eventArgs = {"-h"};
75
76         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
77             .hasMessageContaining("usage: org.onap.policy.apex.client.monitoring.rest."
78                     + "ApexMonitoringRestMain [options...]");
79     }
80
81     @Test
82     public void testMonitoringClientPortBad() {
83         final String[] eventArgs = {"-p", "hello"};
84
85         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
86             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
87                     + "parameter error, error parsing argument \"port\" :For input string: \"hello\"");
88     }
89
90     @Test
91     public void testMonitoringClientPortNegative() {
92         final String[] eventArgs = {"-p", "-1"};
93
94         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
95             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRest"
96                 + "Parameters: 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     @Test
101     public void testMonitoringClientTtlTooSmall() {
102         final String[] eventArgs = {"-t", "-2"};
103
104         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
105             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRest"
106                     + "Parameters: URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, "
107                     + "time to live must be greater than -1 (set to -1 to wait forever)");
108     }
109
110     @Test
111     public void testMonitoringClientTooManyPars() {
112         final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"};
113
114         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
115             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
116                     + "parameter error, too many command line arguments specified : [aaa, bbb]");
117     }
118
119     @Test
120     public void testMonitoringClientTtlNotNumber() {
121         final String[] eventArgs = {"-t", "timetolive"};
122
123         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
124             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) "
125                     + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \""
126                             + "timetolive\"");
127     }
128
129     @Test
130     public void testMonitoringClientPortTooBig() {
131         final String[] eventArgs = {"-p", "65536"};
132
133         assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out))
134             .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoring"
135                 + "RestParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, "
136                 + "port must be greater than 1023 and less than 65536");
137     }
138
139     @Test
140     public void testMonitoringClientDefaultPars() {
141         ApexMonitoringRest monRest = new ApexMonitoringRest();
142         assertNotNull(monRest);
143         assertThatCode(() -> monRest.shutdown()).isNull();
144     }
145
146     @Test
147     public void testMonitoringOneSecStart() {
148         final String[] eventArgs = {"-t", "1"};
149
150         ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
151         assertNotNull(monRestMain);
152         monRestMain.init();
153         assertThatCode(() -> monRestMain.shutdown()).isNull();
154     }
155
156     @Test
157     public void testMonitoringForeverStart() {
158         final String[] eventArgs = {"-t", "-1"};
159
160         ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out);
161
162         Thread monThread = new Thread() {
163             @Override
164             public void run() {
165                 monRestMain.init();
166             }
167         };
168         assertThatCode(() -> {
169             monThread.start();
170             await().atMost(6, TimeUnit.SECONDS)
171                 .until(() -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
172             monRestMain.shutdown();
173         }).doesNotThrowAnyException();
174     }
175
176     /**
177      * Run the application.
178      *
179      * @param eventArgs the command arguments
180      * @return a string containing the command output
181      */
182     private String testApexMonitoringRestMainConstructor(final String[] eventArgs) {
183         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
184         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
185
186         new ApexMonitoringRestMain(eventArgs, new PrintStream(baosOut, true));
187
188         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
189         System.setIn(testInput);
190
191         String outString = baosOut.toString();
192         String errString = baosErr.toString();
193
194         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
195     }
196 }