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.fail;
28 import org.apache.commons.cli.ParseException;
29 import org.junit.Test;
30 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGenerator;
31 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGeneratorParameterHandler;
32 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGeneratorParameters;
35 * Test event generator parameters.
37 public class EventGeneratorParametersHandlerTest {
40 public void testEventGeneratorParameterhandler() {
41 EventGeneratorParameterHandler handler = new EventGeneratorParameterHandler();
42 assertNotNull(handler);
47 EventGeneratorParameters parameters = handler.parse(args);
48 assertNull(parameters);
49 assertEquals("usage: EventGenerator [options...]",
50 handler.getHelp(EventGenerator.class.getSimpleName()).substring(0, 34));
51 } catch (ParseException pe) {
52 fail("test should not throw an exception");
58 EventGeneratorParameters parameters = handler.parse(args);
59 assertEquals("localhost", parameters.getHost());
60 assertEquals(32801, parameters.getPort());
61 } catch (ParseException pe) {
62 fail("test should not throw an exception");
68 EventGeneratorParameters parameters = handler.parse(args);
69 assertEquals("MyHost", parameters.getHost());
70 } catch (ParseException pe) {
71 fail("test should not throw an exception");
77 EventGeneratorParameters parameters = handler.parse(args);
78 assertEquals(12345, parameters.getPort());
79 } catch (ParseException pe) {
80 fail("test should not throw an exception");
85 { "-H", "MyHost", "-p", "12345" };
86 EventGeneratorParameters parameters = handler.parse(args);
87 assertEquals("MyHost", parameters.getHost());
88 assertEquals(12345, parameters.getPort());
89 } catch (ParseException pe) {
90 fail("test should not throw an exception");
95 { "-c", "src/test/resources/parameters/unit/Valid.json" };
96 EventGeneratorParameters parameters = handler.parse(args);
97 assertEquals("ValidPars", parameters.getName());
98 assertEquals("FileHost", parameters.getHost());
99 assertEquals(54321, parameters.getPort());
100 } catch (ParseException pe) {
101 fail("test should not throw an exception");
106 { "-c", "src/test/resources/parameters/unit/Default.json" };
107 EventGeneratorParameters parameters = handler.parse(args);
108 assertEquals("localhost", parameters.getHost());
109 assertEquals(32801, parameters.getPort());
110 } catch (ParseException pe) {
111 fail("test should not throw an exception");
116 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "100" };
117 EventGeneratorParameters parameters = handler.parse(args);
118 assertEquals(100, parameters.getBatchCount());
119 } catch (ParseException pe) {
120 fail("test should not throw an exception");
125 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "-1" };
127 fail("test should throw an exception");
128 } catch (ParseException pe) {
129 assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
130 + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
131 + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
132 + " field \"batchCount\" type \"int\" value \"-1\" INVALID, "
133 + "batchCount must be an integer with a value of zero or more, "
134 + "zero means generate batches forever\n", pe.getMessage());
139 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "12345" };
140 EventGeneratorParameters parameters = handler.parse(args);
141 assertEquals(12345, parameters.getBatchSize());
142 } catch (ParseException pe) {
143 fail("test should not throw an exception");
148 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "0" };
150 fail("test should throw an exception");
151 } catch (ParseException pe) {
152 assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
153 + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
154 + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
155 + " field \"batchSize\" type \"int\" value \"0\" INVALID, "
156 + "batchSize must be an integer greater than zero\n", pe.getMessage());
161 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "1000" };
162 EventGeneratorParameters parameters = handler.parse(args);
163 assertEquals(1000, parameters.getDelayBetweenBatches());
164 } catch (ParseException pe) {
165 fail("test should not throw an exception");
170 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "-1" };
172 fail("test should throw an exception");
173 } catch (ParseException pe) {
174 assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
175 + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
176 + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
177 + " field \"batchSize\" type \"int\" value \"1\" INVALID, "
178 + "batchSize must be an integer with a value of zero or more\n", pe.getMessage());
183 { "-c", "src/test/resources/parameters/unit/Default.json", "-o", "Zooby" };
184 EventGeneratorParameters parameters = handler.parse(args);
185 assertEquals("Zooby", parameters.getOutFile());
186 } catch (ParseException pe) {
187 fail("test should not throw an exception");
194 fail("test should throw an exception");
195 } catch (ParseException pe) {
196 assertEquals("Unrecognized option: -z", pe.getMessage());
203 fail("test should throw an exception");
204 } catch (ParseException pe) {
205 assertEquals("Missing argument for option: H", pe.getMessage());
212 fail("test should throw an exception");
213 } catch (ParseException pe) {
214 assertEquals("Missing argument for option: p", pe.getMessage());
219 { "-p", "12345", "-z" };
221 fail("test should throw an exception");
222 } catch (ParseException pe) {
223 assertEquals("Unrecognized option: -z", pe.getMessage());
228 { "-p", "12345", "somethingElse" };
230 fail("test should throw an exception");
231 } catch (ParseException pe) {
232 assertEquals("too many command line arguments specified : [somethingElse]", pe.getMessage());
239 fail("test should throw an exception");
240 } catch (ParseException pe) {
241 assertEquals("Missing argument for option: c", pe.getMessage());
246 { "-H", "MyHost", "-c", "src/test/resources/parameters/unit/Valid.json" };
247 EventGeneratorParameters pars = handler.parse(args);
248 assertEquals("MyHost", pars.getHost());
250 } catch (ParseException pe) {
251 fail("test should not throw an exception");
256 { "-c", "src/test/resources/parameters/unit/NonExistant.json" };
258 fail("test should throw an exception");
259 } catch (ParseException pe) {
260 assertEquals("Could not read parameters from configuration file "
261 + "\"src/test/resources/parameters/unit/NonExistant.json\": "
262 + "src/test/resources/parameters/unit/NonExistant.json", pe.getMessage());
267 { "-c", "src/test/resources/parameters/unit/BadHost.json" };
269 fail("test should throw an exception");
270 } catch (ParseException pe) {
271 assertEquals("Error parsing JSON parameters from configuration file "
272 + "\"src/test/resources/parameters/unit/BadHost.json\": "
273 + "com.google.gson.stream.MalformedJsonException: "
274 + "Unexpected value at line 3 column 14 path $.host", pe.getMessage());
279 { "-c", "src/test/resources/parameters/unit/BadPort.json" };
281 fail("test should throw an exception");
282 } catch (ParseException pe) {
283 assertEquals("Error parsing JSON parameters from configuration file "
284 + "\"src/test/resources/parameters/unit/BadPort.json\": "
285 + "java.lang.IllegalStateException: Expected an int "
286 + "but was BOOLEAN at line 4 column 18 path $.port", pe.getMessage());
291 { "-c", "src/test/resources/parameters/unit/Empty.json" };
293 fail("test should throw an exception");
294 } catch (ParseException pe) {
295 assertEquals("No parameters found in configuration file "
296 + "\"src/test/resources/parameters/unit/Empty.json\"", pe.getMessage());
301 { "-c", "src/test/resources/parameters/unit/NullHost.json" };
303 fail("test should throw an exception");
304 } catch (ParseException pe) {
305 assertEquals("specified parameters are not valid: parameter group \"ValidPars\" "
306 + "type \"org.onap.policy.apex.testsuites.performance."
307 + "benchmark.eventgenerator.EventGeneratorParameters\" INVALID, "
308 + "parameter group has status INVALID\n" + " field \"host\" type \"java.lang.String\" "
309 + "value \"null\" INVALID, host must be a non-blank string\n", pe.getMessage());
316 fail("test should throw an exception");
317 } catch (ParseException pe) {
318 assertEquals("specified parameters are not valid: parameter group \""
319 + "EventGeneratorParameters\" type \"org.onap.policy.apex.testsuites.performance.benchmark."
320 + "eventgenerator.EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
321 + " field \"port\" type \"int\" value \"1023\" INVALID, "
322 + "port must be an integer between 1024 and 65535 inclusive\n" + "", pe.getMessage());