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