99c95465f3f1545e0ccbe61d05968e997d730c8f
[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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
26
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;
32
33 /**
34  * Test the periodic event manager utility.
35  */
36 public class PeriodicEventManagerTest {
37     @Test
38     public void testPeroidicEventManagerBad() {
39         final String[] eventArgs = { "-h" };
40
41         assertThatThrownBy(() -> PeriodicEventManager.main(eventArgs))
42             .hasMessageContaining("invalid arguments: [-h]");
43     }
44
45     @Test
46     public void testPeroidicEventManagerOk() {
47         final String[] eventArgs = { "Host", "43443", "start", "1000" };
48
49         assertThatThrownBy(() -> PeriodicEventManager.main(eventArgs))
50             .hasMessage("periodic event setting failed on parameters Host 43443 true");
51     }
52
53     @Test
54     public void testPeroidicEventManagerNoOptions() {
55         final String[] eventArgs = new String[] {};
56
57         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
58
59         assertTrue(outputString
60             .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
61     }
62
63     @Test
64     public void testPeroidicEventManagerBadOptions() {
65         final String[] eventArgs = { "-zabbu" };
66
67         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
68
69         assertTrue(outputString
70             .contains("usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>"));
71     }
72
73     @Test
74     public void testPeroidicEventManagerNonNumeric3() {
75         final String[] eventArgs = { "aaa", "bbb", "ccc", "ddd" };
76
77         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
78
79         assertTrue(outputString.contains("argument port is invalid"));
80     }
81
82     @Test
83     public void testPeroidicEventManagerNonNumeric2() {
84         final String[] eventArgs = { "aaa", "12345", "start", "stop" };
85
86         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
87
88         assertTrue(outputString.contains("argument period is invalid"));
89     }
90
91     @Test
92     public void testPeroidicEventManagerNotStartStop() {
93         final String[] eventArgs = { "aaa", "12345", "1000", "1000" };
94
95         final String outputString = testPeriodicEventManagerConstructor(eventArgs);
96
97         assertTrue(outputString.contains("argument 1000 must be \"start\" or \"stop\""));
98     }
99
100     @Test
101     public void testPeroidicEventManagerStart() throws ApexDeploymentException {
102         final String[] eventArgs = { "localhost", "12345", "start", "1000" };
103
104         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
105
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);
110
111         dummyDeploymentClient.setInitSuccessful(false);
112         assertThatThrownBy(peManager::init)
113             .hasMessage("periodic event setting failed on parameters localhost 12345 true");
114         dummyDeploymentClient.setInitSuccessful(true);
115         peManager.init();
116
117         assertThatThrownBy(peManager::runCommand)
118             .hasMessage("failed response Operation failed received from serverlocalhost:12345");
119
120         peManager.close();
121     }
122
123     @Test
124     public void testPeroidicEventManagerStop() throws ApexDeploymentException {
125
126         final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
127
128         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
129
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);
134
135         dummyDeploymentClient.setInitSuccessful(false);
136         assertThatThrownBy(peManager::init)
137             .hasMessage("periodic event setting failed on parameters localhost 12345 false");
138         dummyDeploymentClient.setInitSuccessful(true);
139         peManager.init();
140
141         assertThatThrownBy(peManager::runCommand)
142             .hasMessage("failed response Operation failed received from serverlocalhost:12345");
143         peManager.runCommand();
144
145         peManager.close();
146     }
147
148     @Test
149     public void testPeroidicEventManagerStartUninitialized() throws ApexDeploymentException {
150
151         final String[] eventArgs = { "localhost", "12345", "start", "1000" };
152
153         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
154
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);
159
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");
166
167         peManager.close();
168     }
169
170     @Test
171     public void testPeroidicEventManagerStopUninitialized() throws ApexDeploymentException {
172
173         final String[] eventArgs = { "localhost", "12345", "stop", "1000" };
174
175         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
176
177         PeriodicEventManager peManager = null;
178         peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
179         peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
180
181         assertThatThrownBy(peManager::runCommand)
182             .hasMessage("connection to apex is not initialized");
183         peManager.close();
184     }
185
186     /**
187      * Run the application.
188      *
189      * @param eventArgs the command arguments
190      * @return a string containing the command output
191      */
192     private String testPeriodicEventManagerConstructor(final String[] eventArgs) {
193         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
194         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
195
196         String exceptionString = "";
197         try {
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();
202         }
203
204         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
205         System.setIn(testInput);
206
207         String outString = baosOut.toString();
208         String errString = baosErr.toString();
209
210         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"
211             + exceptionString;
212     }
213 }