fb204c3a320e936110903c6df399f36ac129fe12
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.deployment;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.InputStream;
30 import java.io.PrintStream;
31
32 import org.junit.Test;
33
34 /**
35  * Test the periodic event manager utility.
36  */
37 public class PeriodicEventManagerTest {
38     @Test
39     public void testPeroidicEventManager() {
40         try {
41             final String[] EventArgs =
42                 { "-h" };
43
44             PeriodicEventManager.main(EventArgs);
45         } catch (Exception exc) {
46             fail("test should not throw an exception");
47         }
48     }
49
50     @Test
51     public void testPeroidicEventManagerNoOptions() {
52         final String[] EventArgs = new String[]
53             {};
54
55         final String outputString = runPeriodicEventManager(EventArgs);
56
57         assertTrue(outputString
58                         .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>"));
59     }
60
61     @Test
62     public void testPeroidicEventManagerBadOptions() {
63         final String[] EventArgs =
64             { "-zabbu" };
65
66         final String outputString = runPeriodicEventManager(EventArgs);
67
68         assertTrue(outputString
69                         .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>"));
70     }
71
72     @Test
73     public void testPeroidicEventManagerNonNumeric3() {
74         final String[] EventArgs =
75             { "aaa", "bbb", "ccc", "ddd" };
76
77         try {
78             runPeriodicEventManager(EventArgs);
79         } catch (NumberFormatException nfe) {
80             assertEquals("For input string: \"bbb\"", nfe.getMessage());
81         }
82     }
83
84     @Test
85     public void testPeroidicEventManagerNonNumeric2() {
86         final String[] EventArgs =
87             { "aaa", "12345", "ccc", "1000" };
88
89         try {
90             runPeriodicEventManager(EventArgs);
91         } catch (NumberFormatException nfe) {
92             assertEquals("For input string: \"ddd\"", nfe.getMessage());
93         }
94     }
95
96     @Test
97     public void testPeroidicEventManagerStart() {
98         final String[] EventArgs =
99             { "localhost", "12345", "start", "1000" };
100
101         final String outputString = runPeriodicEventManager(EventArgs);
102
103         assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***"));
104     }
105
106
107     @Test
108     public void testPeroidicEventManagerStop() {
109         final String[] EventArgs =
110             { "localhost", "12345", "stop", "1000" };
111
112         final String outputString = runPeriodicEventManager(EventArgs);
113
114         assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***"));
115     }
116
117     /**
118      * Run the application.
119      * 
120      * @param eventArgs the command arguments
121      * @return a string containing the command output
122      */
123     private String runPeriodicEventManager(final String[] eventArgs) {
124         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
125         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
126
127         PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true));
128
129         String exceptionString = "";
130         try {
131             peManager.init();
132         } catch (ApexDeploymentException ade) {
133             exceptionString = ade.getCascadedMessage();
134         }
135
136         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
137         System.setIn(testInput);
138
139         String outString = baosOut.toString();
140         String errString = baosErr.toString();
141
142         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString + "\n*** exception ***\n"
143                         + exceptionString;
144     }
145 }