3444eb7fdf63134a8b80b0bf6366a6106eb4b2c5
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.core.deployment;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.InputStream;
31 import java.io.PrintStream;
32 import org.junit.Test;
33
34 /**
35  * Test the periodic event manager utility.
36  */
37 public class PeriodicEventManagerTest {
38     @Test
39     public void testPeroidicEventManagerBad() {
40         try {
41             final String[] eventArgs = { "-h" };
42
43             PeriodicEventManager.main(eventArgs);
44             fail("test should throw an exception");
45         } catch (Exception exc) {
46             assertEquals("invalid arguments: [-h]", exc.getMessage().substring(0, 23));
47         }
48     }
49
50     @Test
51     public void testPeroidicEventManagerOk() {
52         try {
53             final String[] eventArgs = { "Host", "43443", "start", "1000" };
54
55             PeriodicEventManager.main(eventArgs);
56             fail("test should throw an exception");
57         } catch (Exception exc) {
58             assertEquals("periodic event setting failed on parameters Host 43443 true", exc.getMessage());
59         }
60     }
61
62     @Test
63     public void testPeroidicEventManagerNoOptions() {
64         final String[] eventArgs = new String[] {};
65
66         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
67
68         assertTrue(outputString
69             .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
70     }
71
72     @Test
73     public void testPeroidicEventManagerBadOptions() {
74         final String[] eventArgs = { "-zabbu" };
75
76         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
77
78         assertTrue(outputString
79             .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
80     }
81
82     @Test
83     public void testPeroidicEventManagerNonNumeric3() {
84         final String[] eventArgs = { "aaa", "bbb", "ccc", "ddd" };
85
86         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
87
88         assertTrue(outputString.contains("argument port is invalid"));
89     }
90
91     @Test
92     public void testPeroidicEventManagerNonNumeric2() {
93         final String[] eventArgs = { "aaa", "12345", "start", "stop" };
94
95         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
96
97         assertTrue(outputString.contains("argument period is invalid"));
98     }
99
100     @Test
101     public void testPeroidicEventManagerNotStartStop() {
102         final String[] eventArgs = { "aaa", "12345", "1000", "1000" };
103
104         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
105
106         assertTrue(outputString.contains("argument 1000 must be \"start\" or \"stop\""));
107     }
108
109     @Test
110     public void testPeroidicEventManagerStart() {
111         final String[] eventArgs = { "localhost", "12345", "start", "1000" };
112
113         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
114
115         PeriodicEventManager peManager = null;
116         final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
117         try {
118             peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
119             peManager.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
120         } catch (ApexDeploymentException ade) {
121             fail("test should not throw an exception");
122         }
123
124         dummyDeploymentClient.setInitSuccessful(false);
125         try {
126             peManager.init();
127             fail("test should throw an exception");
128         } catch (ApexDeploymentException ade) {
129             assertEquals("periodic event setting failed on parameters localhost 12345 true", ade.getMessage());
130         }
131
132         dummyDeploymentClient.setInitSuccessful(true);
133         try {
134             peManager.init();
135         } catch (ApexDeploymentException ade) {
136             ade.printStackTrace();
137             fail("test should not throw an exception");
138         }
139
140         try {
141             peManager.runCommand();
142             fail("test should throw an exception");
143         } catch (ApexDeploymentException ade) {
144             assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
145         }
146
147         try {
148             peManager.runCommand();
149         } catch (ApexDeploymentException ade) {
150             fail("test should not throw an exception");
151         }
152
153         peManager.close();
154     }
155
156     @Test
157     public void testPeroidicEventManagerStop() throws ApexDeploymentException {
158         final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
159
160         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
161
162         PeriodicEventManager peManager = null;
163         final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
164         peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
165         peManager.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
166
167         dummyDeploymentClient.setInitSuccessful(false);
168         try {
169             peManager.init();
170             fail("test should throw an exception");
171         } catch (ApexDeploymentException ade) {
172             assertEquals("periodic event setting failed on parameters localhost 12345 false", ade.getMessage());
173         }
174
175         dummyDeploymentClient.setInitSuccessful(true);
176         peManager.init();
177
178         try {
179             peManager.runCommand();
180             fail("test should throw an exception");
181         } catch (ApexDeploymentException ade) {
182             assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
183         }
184
185         peManager.runCommand();
186
187         peManager.close();
188     }
189
190     @Test
191     public void testPeroidicEventManagerStartUninitialized() throws ApexDeploymentException {
192         final String[] eventArgs = { "localhost", "12345", "start", "1000" };
193
194         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
195
196         PeriodicEventManager peManager = null;
197         final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
198         peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
199         peManager.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
200
201         dummyDeploymentClient.setInitSuccessful(false);
202         try {
203             peManager.runCommand();
204             fail("test should throw an exception");
205         } catch (ApexDeploymentException ade) {
206             assertEquals("connection to apex is not initialized", ade.getMessage());
207         }
208
209         dummyDeploymentClient.setInitSuccessful(true);
210         try {
211             peManager.runCommand();
212             fail("test should throw an exception");
213         } catch (ApexDeploymentException ade) {
214             assertEquals("connection to apex is not initialized", ade.getMessage());
215         }
216
217         peManager.close();
218     }
219
220     @Test
221     public void testPeroidicEventManagerStopUninitialized() throws ApexDeploymentException {
222         final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
223
224         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
225
226         PeriodicEventManager peManager = null;
227         peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
228         peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
229
230         try {
231             peManager.runCommand();
232             fail("test should throw an exception");
233         } catch (ApexDeploymentException ade) {
234             assertEquals("connection to apex is not initialized", ade.getMessage());
235         }
236
237         peManager.close();
238     }
239
240     /**
241      * Run the application.
242      *
243      * @param eventArgs the command arguments
244      * @return a string containing the command output
245      */
246     private String testPeriodicEventManagerConstructor(final String[] eventArgs) {
247         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
248         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
249
250         String exceptionString = "";
251         try {
252             PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
253             peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
254         } catch (ApexDeploymentException ade) {
255             exceptionString = ade.getCascadedMessage();
256         }
257
258         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
259         System.setIn(testInput);
260
261         String outString = baosOut.toString();
262         String errString = baosErr.toString();
263
264         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"
265             + exceptionString;
266     }
267 }