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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.InputStream;
30 import java.io.PrintStream;
31 import org.junit.Test;
34 * Test the periodic event manager utility.
36 public class PeriodicEventManagerTest {
38 public void testPeroidicEventManagerBad() {
39 final String[] eventArgs = { "-h" };
41 assertThatThrownBy(() -> PeriodicEventManager.main(eventArgs))
42 .hasMessageContaining("invalid arguments: [-h]");
46 public void testPeroidicEventManagerOk() {
47 final String[] eventArgs = { "Host", "43443", "start", "1000" };
49 assertThatThrownBy(() -> PeriodicEventManager.main(eventArgs))
50 .hasMessage("periodic event setting failed on parameters Host 43443 true");
54 public void testPeroidicEventManagerNoOptions() {
55 final String[] eventArgs = new String[] {};
57 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
59 assertTrue(outputString
60 .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
64 public void testPeroidicEventManagerBadOptions() {
65 final String[] eventArgs = { "-zabbu" };
67 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
69 assertTrue(outputString
70 .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
74 public void testPeroidicEventManagerNonNumeric3() {
75 final String[] eventArgs = { "aaa", "bbb", "ccc", "ddd" };
77 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
79 assertTrue(outputString.contains("argument port is invalid"));
83 public void testPeroidicEventManagerNonNumeric2() {
84 final String[] eventArgs = { "aaa", "12345", "start", "stop" };
86 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
88 assertTrue(outputString.contains("argument period is invalid"));
92 public void testPeroidicEventManagerNotStartStop() {
93 final String[] eventArgs = { "aaa", "12345", "1000", "1000" };
95 final String outputString = testPeriodicEventManagerConstructor(eventArgs);
97 assertTrue(outputString.contains("argument 1000 must be \"start\" or \"stop\""));
101 public void testPeroidicEventManagerStart() throws ApexDeploymentException {
102 final String[] eventArgs = { "localhost", "12345", "start", "1000" };
104 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
106 PeriodicEventManager peManager = null;
107 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
108 peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
109 peManager.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
111 dummyDeploymentClient.setInitSuccessful(false);
112 assertThatThrownBy(peManager::init)
113 .hasMessage("periodic event setting failed on parameters localhost 12345 true");
114 dummyDeploymentClient.setInitSuccessful(true);
117 assertThatThrownBy(peManager::runCommand)
118 .hasMessage("failed response Operation failed received from serverlocalhost:12345");
124 public void testPeroidicEventManagerStop() throws ApexDeploymentException {
126 final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
128 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
130 PeriodicEventManager peManager = null;
131 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
132 peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
133 peManager.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
135 dummyDeploymentClient.setInitSuccessful(false);
136 assertThatThrownBy(peManager::init)
137 .hasMessage("periodic event setting failed on parameters localhost 12345 false");
138 dummyDeploymentClient.setInitSuccessful(true);
141 assertThatThrownBy(peManager::runCommand)
142 .hasMessage("failed response Operation failed received from serverlocalhost:12345");
143 peManager.runCommand();
149 public void testPeroidicEventManagerStartUninitialized() throws ApexDeploymentException {
151 final String[] eventArgs = { "localhost", "12345", "start", "1000" };
153 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
155 PeriodicEventManager peManager = null;
156 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
157 peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
158 peManager.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
160 dummyDeploymentClient.setInitSuccessful(false);
161 assertThatThrownBy(peManager::runCommand)
162 .hasMessage("connection to apex is not initialized");
163 dummyDeploymentClient.setInitSuccessful(true);
164 assertThatThrownBy(peManager::runCommand)
165 .hasMessage("connection to apex is not initialized");
171 public void testPeroidicEventManagerStopUninitialized() throws ApexDeploymentException {
173 final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
175 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
177 PeriodicEventManager peManager = null;
178 peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
179 peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
181 assertThatThrownBy(peManager::runCommand)
182 .hasMessage("connection to apex is not initialized");
187 * Run the application.
189 * @param eventArgs the command arguments
190 * @return a string containing the command output
192 private String testPeriodicEventManagerConstructor(final String[] eventArgs) {
193 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
194 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
196 String exceptionString = "";
198 PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
199 peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
200 } catch (ApexDeploymentException ade) {
201 exceptionString = ade.getCascadedMessage();
204 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
205 System.setIn(testInput);
207 String outString = baosOut.toString();
208 String errString = baosErr.toString();
210 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"