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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.deployment;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
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;
35 * Test the periodic event manager utility.
37 public class PeriodicEventManagerTest {
39 public void testPeroidicEventManagerBad() {
41 final String[] eventArgs = { "-h" };
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));
51 public void testPeroidicEventManagerOk() {
53 final String[] eventArgs = { "Host", "43443", "start", "1000" };
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());
63 public void testPeroidicEventManagerNoOptions() {
64 final String[] eventArgs = new String[] {};
66 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
68 assertTrue(outputString
69 .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
73 public void testPeroidicEventManagerBadOptions() {
74 final String[] eventArgs = { "-zabbu" };
76 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
78 assertTrue(outputString
79 .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
83 public void testPeroidicEventManagerNonNumeric3() {
84 final String[] eventArgs = { "aaa", "bbb", "ccc", "ddd" };
86 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
88 assertTrue(outputString.contains("argument port is invalid"));
92 public void testPeroidicEventManagerNonNumeric2() {
93 final String[] eventArgs = { "aaa", "12345", "start", "stop" };
95 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
97 assertTrue(outputString.contains("argument period is invalid"));
101 public void testPeroidicEventManagerNotStartStop() {
102 final String[] eventArgs = { "aaa", "12345", "1000", "1000" };
104 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
106 assertTrue(outputString.contains("argument 1000 must be \"start\" or \"stop\""));
110 public void testPeroidicEventManagerStart() {
111 final String[] eventArgs = { "localhost", "12345", "start", "1000" };
113 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
115 PeriodicEventManager peManager = null;
116 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
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");
124 dummyDeploymentClient.setInitSuccessful(false);
127 fail("test should throw an exception");
128 } catch (ApexDeploymentException ade) {
129 assertEquals("periodic event setting failed on parameters localhost 12345 true", ade.getMessage());
132 dummyDeploymentClient.setInitSuccessful(true);
135 } catch (ApexDeploymentException ade) {
136 ade.printStackTrace();
137 fail("test should not throw an exception");
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());
148 peManager.runCommand();
149 } catch (ApexDeploymentException ade) {
150 fail("test should not throw an exception");
157 public void testPeroidicEventManagerStop() throws ApexDeploymentException {
158 final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
160 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
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);
167 dummyDeploymentClient.setInitSuccessful(false);
170 fail("test should throw an exception");
171 } catch (ApexDeploymentException ade) {
172 assertEquals("periodic event setting failed on parameters localhost 12345 false", ade.getMessage());
175 dummyDeploymentClient.setInitSuccessful(true);
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());
185 peManager.runCommand();
191 public void testPeroidicEventManagerStartUninitialized() throws ApexDeploymentException {
192 final String[] eventArgs = { "localhost", "12345", "start", "1000" };
194 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
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);
201 dummyDeploymentClient.setInitSuccessful(false);
203 peManager.runCommand();
204 fail("test should throw an exception");
205 } catch (ApexDeploymentException ade) {
206 assertEquals("connection to apex is not initialized", ade.getMessage());
209 dummyDeploymentClient.setInitSuccessful(true);
211 peManager.runCommand();
212 fail("test should throw an exception");
213 } catch (ApexDeploymentException ade) {
214 assertEquals("connection to apex is not initialized", ade.getMessage());
221 public void testPeroidicEventManagerStopUninitialized() throws ApexDeploymentException {
222 final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
224 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
226 PeriodicEventManager peManager = null;
227 peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
228 peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
231 peManager.runCommand();
232 fail("test should throw an exception");
233 } catch (ApexDeploymentException ade) {
234 assertEquals("connection to apex is not initialized", ade.getMessage());
241 * Run the application.
243 * @param eventArgs the command arguments
244 * @return a string containing the command output
246 private String testPeriodicEventManagerConstructor(final String[] eventArgs) {
247 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
248 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
250 String exceptionString = "";
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();
258 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
259 System.setIn(testInput);
261 String outString = baosOut.toString();
262 String errString = baosErr.toString();
264 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"