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 testPeroidicEventManager() {
41 final String[] EventArgs =
44 PeriodicEventManager.main(EventArgs);
45 } catch (Exception exc) {
46 fail("test should not throw an exception");
51 public void testPeroidicEventManagerNoOptions() {
52 final String[] EventArgs = new String[]
55 final String outputString = runPeriodicEventManager(EventArgs);
57 assertTrue(outputString
58 .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>"));
62 public void testPeroidicEventManagerBadOptions() {
63 final String[] EventArgs =
66 final String outputString = runPeriodicEventManager(EventArgs);
68 assertTrue(outputString
69 .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>"));
73 public void testPeroidicEventManagerNonNumeric3() {
74 final String[] EventArgs =
75 { "aaa", "bbb", "ccc", "ddd" };
78 runPeriodicEventManager(EventArgs);
79 } catch (NumberFormatException nfe) {
80 assertEquals("For input string: \"bbb\"", nfe.getMessage());
85 public void testPeroidicEventManagerNonNumeric2() {
86 final String[] EventArgs =
87 { "aaa", "12345", "ccc", "1000" };
90 runPeriodicEventManager(EventArgs);
91 } catch (NumberFormatException nfe) {
92 assertEquals("For input string: \"ddd\"", nfe.getMessage());
97 public void testPeroidicEventManagerStart() {
98 final String[] EventArgs =
99 { "localhost", "12345", "start", "1000" };
101 final String outputString = runPeriodicEventManager(EventArgs);
103 assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***"));
108 public void testPeroidicEventManagerStop() {
109 final String[] EventArgs =
110 { "localhost", "12345", "stop", "1000" };
112 final String outputString = runPeriodicEventManager(EventArgs);
114 assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***"));
118 * Run the application.
120 * @param eventArgs the command arguments
121 * @return a string containing the command output
123 private String runPeriodicEventManager(final String[] eventArgs) {
124 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
125 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
127 PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
129 String exceptionString = "";
132 } catch (ApexDeploymentException ade) {
133 exceptionString = ade.getCascadedMessage();
136 InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
137 System.setIn(testInput);
139 String outString = baosOut.toString();
140 String errString = baosErr.toString();
142 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"