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.testsuites.performance.benchmark.eventgenerator;
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;
29 import org.apache.commons.cli.ParseException;
30 import org.junit.Test;
33 * Test event generator parameters.
35 public class EventGeneratorParametersHandlerTest {
38 public void testEventGeneratorParameterhandler() {
39 EventGeneratorParameterHandler handler = new EventGeneratorParameterHandler();
40 assertNotNull(handler);
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");
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");
66 EventGeneratorParameters parameters = handler.parse(args);
67 assertEquals("MyHost", parameters.getHost());
68 } catch (ParseException pe) {
69 fail("test should not throw an exception");
75 EventGeneratorParameters parameters = handler.parse(args);
76 assertEquals(12345, parameters.getPort());
77 } catch (ParseException pe) {
78 fail("test should not throw an exception");
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");
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");
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");
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");
123 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "-1" };
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());
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");
146 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "0" };
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());
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");
168 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "-1" };
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());
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");
192 fail("test should throw an exception");
193 } catch (ParseException pe) {
194 assertEquals("Unrecognized option: -z", pe.getMessage());
201 fail("test should throw an exception");
202 } catch (ParseException pe) {
203 assertEquals("Missing argument for option: H", pe.getMessage());
210 fail("test should throw an exception");
211 } catch (ParseException pe) {
212 assertEquals("Missing argument for option: p", pe.getMessage());
217 { "-p", "12345", "-z" };
219 fail("test should throw an exception");
220 } catch (ParseException pe) {
221 assertEquals("Unrecognized option: -z", pe.getMessage());
226 { "-p", "12345", "somethingElse" };
228 fail("test should throw an exception");
229 } catch (ParseException pe) {
230 assertEquals("too many command line arguments specified : [somethingElse]", pe.getMessage());
237 fail("test should throw an exception");
238 } catch (ParseException pe) {
239 assertEquals("Missing argument for option: c", pe.getMessage());
244 { "-H", "MyHost", "-c", "src/test/resources/parameters/unit/Valid.json" };
245 EventGeneratorParameters pars = handler.parse(args);
246 assertEquals("MyHost", pars.getHost());
248 } catch (ParseException pe) {
249 fail("test should not throw an exception");
254 { "-c", "src/test/resources/parameters/unit/NonExistant.json" };
256 fail("test should throw an exception");
257 } catch (ParseException pe) {
258 assertTrue(pe.getMessage().startsWith("Could not read parameters from configuration file "));
263 { "-c", "src/test/resources/parameters/unit/BadHost.json" };
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());
275 { "-c", "src/test/resources/parameters/unit/BadPort.json" };
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());
287 { "-c", "src/test/resources/parameters/unit/Empty.json" };
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());
297 { "-c", "src/test/resources/parameters/unit/NullHost.json" };
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());
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());