0b097a0d0a77405660ef08bfbbe2e3fa96bf222b
[policy/apex-pdp.git] / tools / simple-wsclient / src / test / java / org / onap / policy / apex / tools / simple / wsclient / WsClientTest.java
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.tools.simple.wsclient;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.InputStream;
29 import java.io.PrintStream;
30
31 import org.junit.Test;
32
33 /**
34  * Test the WsClient utility.
35  */
36 public class WsClientTest {
37     @Test
38     public void testWsClient() {
39         try {
40             final String[] EventArgs =
41                 { "-h" };
42
43             WsClientMain.main(EventArgs);
44         } catch (Exception exc) {
45             fail("test should not throw an exception");
46         }
47     }
48
49     @Test
50     public void testWsClientNoOptions() {
51         final String[] EventArgs = new String[]
52             {};
53
54         final String outputString = runWsClient(EventArgs);
55
56         assertTrue(outputString.contains("ws-client: starting simple event echo"));
57     }
58
59     @Test
60     public void testWsClientBadOptions() {
61         final String[] EventArgs =
62             { "-zabbu" };
63
64         final String outputString = runWsClient(EventArgs);
65
66         assertTrue(outputString.contains("usage: ws-client"));
67     }
68
69     @Test
70     public void testWsClientHelp() {
71         final String[] EventArgs =
72             { "-h" };
73
74         final String outputString = runWsClient(EventArgs);
75
76         assertTrue(outputString.contains("usage: ws-client"));
77     }
78
79     @Test
80     public void testWsClientVersion() {
81         final String[] EventArgs =
82             { "-v" };
83
84         final String outputString = runWsClient(EventArgs);
85
86         assertTrue(outputString.contains("ws-client"));
87     }
88
89     @Test
90     public void testWsClientNoServerArg() {
91         final String[] EventArgs =
92             { "-s" };
93
94         final String outputString = runWsClient(EventArgs);
95
96         assertTrue(outputString.contains("ws-client"));
97     }
98
99     @Test
100     public void testWsClientNoPortArg() {
101         final String[] EventArgs =
102             { "-p" };
103
104         final String outputString = runWsClient(EventArgs);
105
106         assertTrue(outputString.contains("ws-client"));
107     }
108
109     @Test
110     public void testWsClientBadPortArg() {
111         final String[] EventArgs =
112             { "-p", "hello" };
113
114         final String outputString = runWsClient(EventArgs);
115
116         assertTrue(outputString.contains("ws-client"));
117     }
118
119     @Test
120     public void testWsClientBadServerArg() {
121         final String[] EventArgs =
122             { "-s", "asdsadadasd:asdasdsadasd@@" };
123
124         final String outputString = runWsClient(EventArgs);
125
126         assertTrue(outputString.contains("ws-client"));
127     }
128
129     @Test
130     public void testWsClientConsole() {
131         final String[] EventArgs =
132             { "-c", "-s", "AServerThatDoesntExist", "-p", "99999999" };
133
134         final String outputString = runWsClient(EventArgs);
135
136         assertTrue(outputString.contains("terminate the application typing"));
137     }
138
139     @Test
140     public void testWsClientEcho() {
141         final String[] EventArgs =
142             { "-s", "AServerThatDoesntExist", "-p", "99999999" };
143
144         final String outputString = runWsClient(EventArgs);
145
146         assertTrue(outputString.contains(
147                         "Once started, the application will simply print out all received events to standard out"));
148     }
149
150     /**
151      * Run the application.
152      * 
153      * @param eventArgs the command arguments
154      * @return a string containing the command output
155      */
156     private String runWsClient(final String[] eventArgs) {
157         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
158         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
159
160         new WsClientMain(eventArgs, new PrintStream(baosOut, true));
161
162         InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes());
163         System.setIn(testInput);
164
165         String outString = baosOut.toString();
166         String errString = baosErr.toString();
167
168         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
169     }
170 }