Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / test / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventGeneratorParametersHandlerTest.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.testsuites.performance.benchmark.eventgenerator;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.apache.commons.cli.ParseException;
30 import org.junit.Test;
31
32 /**
33  * Test event generator parameters.
34  */
35 public class EventGeneratorParametersHandlerTest {
36
37     @Test
38     public void testEventGeneratorParameterhandler() {
39         EventGeneratorParameterHandler handler = new EventGeneratorParameterHandler();
40         assertNotNull(handler);
41
42         try {
43             String[] args =
44                 { "-h" };
45             EventGeneratorParameters parameters = handler.parse(args);
46             assertNull(parameters);
47             assertEquals("usage: EventGenerator [options...]",
48                             handler.getHelp(EventGenerator.class.getSimpleName()).substring(0, 34));
49         } catch (ParseException pe) {
50             fail("test should not throw an exception");
51         }
52
53         try {
54             String[] args =
55                 {};
56             EventGeneratorParameters parameters = handler.parse(args);
57             assertEquals("localhost", parameters.getHost());
58             assertEquals(32801, parameters.getPort());
59         } catch (ParseException pe) {
60             fail("test should not throw an exception");
61         }
62
63         try {
64             String[] args =
65                 { "-H", "MyHost" };
66             EventGeneratorParameters parameters = handler.parse(args);
67             assertEquals("MyHost", parameters.getHost());
68         } catch (ParseException pe) {
69             fail("test should not throw an exception");
70         }
71
72         try {
73             String[] args =
74                 { "-p", "12345" };
75             EventGeneratorParameters parameters = handler.parse(args);
76             assertEquals(12345, parameters.getPort());
77         } catch (ParseException pe) {
78             fail("test should not throw an exception");
79         }
80
81         try {
82             String[] args =
83                 { "-H", "MyHost", "-p", "12345" };
84             EventGeneratorParameters parameters = handler.parse(args);
85             assertEquals("MyHost", parameters.getHost());
86             assertEquals(12345, parameters.getPort());
87         } catch (ParseException pe) {
88             fail("test should not throw an exception");
89         }
90
91         try {
92             String[] args =
93                 { "-c", "src/test/resources/parameters/unit/Valid.json" };
94             EventGeneratorParameters parameters = handler.parse(args);
95             assertEquals("ValidPars", parameters.getName());
96             assertEquals("FileHost", parameters.getHost());
97             assertEquals(54321, parameters.getPort());
98         } catch (ParseException pe) {
99             fail("test should not throw an exception");
100         }
101
102         try {
103             String[] args =
104                 { "-c", "src/test/resources/parameters/unit/Default.json" };
105             EventGeneratorParameters parameters = handler.parse(args);
106             assertEquals("localhost", parameters.getHost());
107             assertEquals(32801, parameters.getPort());
108         } catch (ParseException pe) {
109             fail("test should not throw an exception");
110         }
111
112         try {
113             String[] args =
114                 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "100" };
115             EventGeneratorParameters parameters = handler.parse(args);
116             assertEquals(100, parameters.getBatchCount());
117         } catch (ParseException pe) {
118             fail("test should not throw an exception");
119         }
120
121         try {
122             String[] args =
123                 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "-1" };
124             handler.parse(args);
125             fail("test should throw an exception");
126         } catch (ParseException pe) {
127             assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
128                             + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
129                             + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
130                             + "  field \"batchCount\" type \"int\" value \"-1\" INVALID, "
131                             + "batchCount must be an integer with a value of zero or more, "
132                             + "zero means generate batches forever\n", pe.getMessage());
133         }
134
135         try {
136             String[] args =
137                 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "12345" };
138             EventGeneratorParameters parameters = handler.parse(args);
139             assertEquals(12345, parameters.getBatchSize());
140         } catch (ParseException pe) {
141             fail("test should not throw an exception");
142         }
143
144         try {
145             String[] args =
146                 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "0" };
147             handler.parse(args);
148             fail("test should throw an exception");
149         } catch (ParseException pe) {
150             assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
151                             + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
152                             + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
153                             + "  field \"batchSize\" type \"int\" value \"0\" INVALID, "
154                             + "batchSize must be an integer greater than zero\n", pe.getMessage());
155         }
156
157         try {
158             String[] args =
159                 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "1000" };
160             EventGeneratorParameters parameters = handler.parse(args);
161             assertEquals(1000, parameters.getDelayBetweenBatches());
162         } catch (ParseException pe) {
163             fail("test should not throw an exception");
164         }
165
166         try {
167             String[] args =
168                 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "-1" };
169             handler.parse(args);
170             fail("test should throw an exception");
171         } catch (ParseException pe) {
172             assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
173                             + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
174                             + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
175                             + "  field \"batchSize\" type \"int\" value \"1\" INVALID, "
176                             + "batchSize must be an integer with a value of zero or more\n", pe.getMessage());
177         }
178
179         try {
180             String[] args =
181                 { "-c", "src/test/resources/parameters/unit/Default.json", "-o", "Zooby" };
182             EventGeneratorParameters parameters = handler.parse(args);
183             assertEquals("Zooby", parameters.getOutFile());
184         } catch (ParseException pe) {
185             fail("test should not throw an exception");
186         }
187
188         try {
189             String[] args =
190                 { "-z" };
191             handler.parse(args);
192             fail("test should throw an exception");
193         } catch (ParseException pe) {
194             assertEquals("Unrecognized option: -z", pe.getMessage());
195         }
196
197         try {
198             String[] args =
199                 { "-H" };
200             handler.parse(args);
201             fail("test should throw an exception");
202         } catch (ParseException pe) {
203             assertEquals("Missing argument for option: H", pe.getMessage());
204         }
205
206         try {
207             String[] args =
208                 { "-p" };
209             handler.parse(args);
210             fail("test should throw an exception");
211         } catch (ParseException pe) {
212             assertEquals("Missing argument for option: p", pe.getMessage());
213         }
214
215         try {
216             String[] args =
217                 { "-p", "12345", "-z" };
218             handler.parse(args);
219             fail("test should throw an exception");
220         } catch (ParseException pe) {
221             assertEquals("Unrecognized option: -z", pe.getMessage());
222         }
223
224         try {
225             String[] args =
226                 { "-p", "12345", "somethingElse" };
227             handler.parse(args);
228             fail("test should throw an exception");
229         } catch (ParseException pe) {
230             assertEquals("too many command line arguments specified : [somethingElse]", pe.getMessage());
231         }
232
233         try {
234             String[] args =
235                 { "-c" };
236             handler.parse(args);
237             fail("test should throw an exception");
238         } catch (ParseException pe) {
239             assertEquals("Missing argument for option: c", pe.getMessage());
240         }
241
242         try {
243             String[] args =
244                 { "-H", "MyHost", "-c", "src/test/resources/parameters/unit/Valid.json" };
245             EventGeneratorParameters pars = handler.parse(args);
246             assertEquals("MyHost", pars.getHost());
247
248         } catch (ParseException pe) {
249             fail("test should not throw an exception");
250         }
251
252         try {
253             String[] args =
254                 { "-c", "src/test/resources/parameters/unit/NonExistant.json" };
255             handler.parse(args);
256             fail("test should throw an exception");
257         } catch (ParseException pe) {
258             assertTrue(pe.getMessage().startsWith("Could not read parameters from configuration file "));
259         }
260
261         try {
262             String[] args =
263                 { "-c", "src/test/resources/parameters/unit/BadHost.json" };
264             handler.parse(args);
265             fail("test should throw an exception");
266         } catch (ParseException pe) {
267             assertEquals("Error parsing JSON parameters from configuration file "
268                             + "\"src/test/resources/parameters/unit/BadHost.json\": "
269                             + "com.google.gson.stream.MalformedJsonException: "
270                             + "Unexpected value at line 3 column 14 path $.host", pe.getMessage());
271         }
272
273         try {
274             String[] args =
275                 { "-c", "src/test/resources/parameters/unit/BadPort.json" };
276             handler.parse(args);
277             fail("test should throw an exception");
278         } catch (ParseException pe) {
279             assertEquals("Error parsing JSON parameters from configuration file "
280                             + "\"src/test/resources/parameters/unit/BadPort.json\": "
281                             + "java.lang.IllegalStateException: Expected an int "
282                             + "but was BOOLEAN at line 4 column 18 path $.port", pe.getMessage());
283         }
284
285         try {
286             String[] args =
287                 { "-c", "src/test/resources/parameters/unit/Empty.json" };
288             handler.parse(args);
289             fail("test should throw an exception");
290         } catch (ParseException pe) {
291             assertEquals("No parameters found in configuration file "
292                             + "\"src/test/resources/parameters/unit/Empty.json\"", pe.getMessage());
293         }
294
295         try {
296             String[] args =
297                 { "-c", "src/test/resources/parameters/unit/NullHost.json" };
298             handler.parse(args);
299             fail("test should throw an exception");
300         } catch (ParseException pe) {
301             assertEquals("specified parameters are not valid: parameter group \"ValidPars\" "
302                             + "type \"org.onap.policy.apex.testsuites.performance."
303                             + "benchmark.eventgenerator.EventGeneratorParameters\" INVALID, "
304                             + "parameter group has status INVALID\n" + "  field \"host\" type \"java.lang.String\" "
305                             + "value \"null\" INVALID, host must be a non-blank string\n", pe.getMessage());
306         }
307
308         try {
309             String[] args =
310                 { "-p", "1023" };
311             handler.parse(args);
312             fail("test should throw an exception");
313         } catch (ParseException pe) {
314             assertEquals("specified parameters are not valid: parameter group \""
315                             + "EventGeneratorParameters\" type \"org.onap.policy.apex.testsuites.performance.benchmark."
316                             + "eventgenerator.EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
317                             + "  field \"port\" type \"int\" value \"1023\" INVALID, "
318                             + "port must be an integer between 1024 and 65535 inclusive\n" + "", pe.getMessage());
319         }
320     }
321 }