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