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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.deployment;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.InputStream;
30 import java.io.PrintStream;
32 import org.junit.Test;
35 * Test the periodic event manager utility.
37 public class PeriodicEventManagerTest {
39 public void testPeroidicEventManagerBad() {
41 final String[] eventArgs =
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));
52 public void testPeroidicEventManagerOk() {
54 final String[] eventArgs =
55 { "Host", "43443", "start", "1000" };
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());
65 public void testPeroidicEventManagerNoOptions() {
66 final String[] eventArgs = new String[]
69 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
71 assertTrue(outputString.contains(
72 "usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
76 public void testPeroidicEventManagerBadOptions() {
77 final String[] eventArgs =
80 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
82 assertTrue(outputString.contains(
83 "usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
87 public void testPeroidicEventManagerNonNumeric3() {
88 final String[] eventArgs =
89 { "aaa", "bbb", "ccc", "ddd" };
91 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
93 assertTrue(outputString.contains("argument port is invalid"));
97 public void testPeroidicEventManagerNonNumeric2() {
98 final String[] eventArgs =
99 { "aaa", "12345", "start", "stop" };
101 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
103 assertTrue(outputString.contains("argument period is invalid"));
107 public void testPeroidicEventManagerNotStartStop() {
108 final String[] eventArgs =
109 { "aaa", "12345", "1000", "1000" };
111 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
113 assertTrue(outputString.contains("argument 1000 must be \"start\" or \"stop\""));
117 public void testPeroidicEventManagerStart() {
118 final String[] eventArgs =
119 { "localhost", "12345", "start", "1000" };
121 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
123 PeriodicEventManager peManager = null;
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");
133 } catch (ApexDeploymentException ade) {
134 assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
139 } catch (ApexDeploymentException ade) {
140 ade.printStackTrace();
141 fail("test should not throw an exception");
145 peManager.runCommand();
146 } catch (ApexDeploymentException ade) {
147 assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
151 peManager.runCommand();
152 } catch (ApexDeploymentException ade) {
153 fail("test should not throw an exception");
160 public void testPeroidicEventManagerStop() {
161 final String[] eventArgs =
162 { "localhost", "12345", "stop", "1000" };
164 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
166 PeriodicEventManager peManager = null;
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");
176 } catch (ApexDeploymentException ade) {
177 assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
182 } catch (ApexDeploymentException ade) {
183 ade.printStackTrace();
184 fail("test should not throw an exception");
188 peManager.runCommand();
189 } catch (ApexDeploymentException ade) {
190 assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
194 peManager.runCommand();
195 } catch (ApexDeploymentException ade) {
196 fail("test should not throw an exception");
203 public void testPeroidicEventManagerStartUninitialized() {
204 final String[] eventArgs =
205 { "localhost", "12345", "start", "1000" };
207 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
209 PeriodicEventManager peManager = null;
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");
218 peManager.runCommand();
219 fail("test should throw an exception");
220 } catch (ApexDeploymentException ade) {
221 assertEquals("connection to apex is not initialized", ade.getMessage());
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();
236 public void testPeroidicEventManagerStopUninitialized() {
237 final String[] eventArgs =
238 { "localhost", "12345", "stop", "1000" };
240 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
242 PeriodicEventManager peManager = null;
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");
251 peManager.runCommand();
252 fail("test should throw an exception");
253 } catch (ApexDeploymentException ade) {
254 assertEquals("connection to apex is not initialized", ade.getMessage());
261 * Run the application.
263 * @param eventArgs the command arguments
264 * @return a string containing the command output
266 private String testPeriodicEventManagerConstructor(final String[] eventArgs) {
267 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
268 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
270 String exceptionString = "";
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();
278 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
279 System.setIn(testInput);
281 String outString = baosOut.toString();
282 String errString = baosErr.toString();
284 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"