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;
31 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGenerator;
32 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGeneratorParameterHandler;
33 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGeneratorParameters;
36 * Test event generator parameters.
38 public class EventGeneratorParametersHandlerTest {
41 public void testEventGeneratorParameterhandler() {
42 EventGeneratorParameterHandler handler = new EventGeneratorParameterHandler();
43 assertNotNull(handler);
48 EventGeneratorParameters parameters = handler.parse(args);
49 assertNull(parameters);
50 assertEquals("usage: EventGenerator [options...]",
51 handler.getHelp(EventGenerator.class.getSimpleName()).substring(0, 34));
52 } catch (ParseException pe) {
53 fail("test should not throw an exception");
59 EventGeneratorParameters parameters = handler.parse(args);
60 assertEquals("localhost", parameters.getHost());
61 assertEquals(32801, parameters.getPort());
62 } catch (ParseException pe) {
63 fail("test should not throw an exception");
69 EventGeneratorParameters parameters = handler.parse(args);
70 assertEquals("MyHost", parameters.getHost());
71 } catch (ParseException pe) {
72 fail("test should not throw an exception");
78 EventGeneratorParameters parameters = handler.parse(args);
79 assertEquals(12345, parameters.getPort());
80 } catch (ParseException pe) {
81 fail("test should not throw an exception");
86 { "-H", "MyHost", "-p", "12345" };
87 EventGeneratorParameters parameters = handler.parse(args);
88 assertEquals("MyHost", parameters.getHost());
89 assertEquals(12345, parameters.getPort());
90 } catch (ParseException pe) {
91 fail("test should not throw an exception");
96 { "-c", "src/test/resources/parameters/unit/Valid.json" };
97 EventGeneratorParameters parameters = handler.parse(args);
98 assertEquals("ValidPars", parameters.getName());
99 assertEquals("FileHost", parameters.getHost());
100 assertEquals(54321, parameters.getPort());
101 } catch (ParseException pe) {
102 fail("test should not throw an exception");
107 { "-c", "src/test/resources/parameters/unit/Default.json" };
108 EventGeneratorParameters parameters = handler.parse(args);
109 assertEquals("localhost", parameters.getHost());
110 assertEquals(32801, parameters.getPort());
111 } catch (ParseException pe) {
112 fail("test should not throw an exception");
117 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "100" };
118 EventGeneratorParameters parameters = handler.parse(args);
119 assertEquals(100, parameters.getBatchCount());
120 } catch (ParseException pe) {
121 fail("test should not throw an exception");
126 { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "-1" };
128 fail("test should throw an exception");
129 } catch (ParseException pe) {
130 assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
131 + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
132 + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
133 + " field \"batchCount\" type \"int\" value \"-1\" INVALID, "
134 + "batchCount must be an integer with a value of zero or more, "
135 + "zero means generate batches forever\n", pe.getMessage());
140 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "12345" };
141 EventGeneratorParameters parameters = handler.parse(args);
142 assertEquals(12345, parameters.getBatchSize());
143 } catch (ParseException pe) {
144 fail("test should not throw an exception");
149 { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "0" };
151 fail("test should throw an exception");
152 } catch (ParseException pe) {
153 assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
154 + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
155 + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
156 + " field \"batchSize\" type \"int\" value \"0\" INVALID, "
157 + "batchSize must be an integer greater than zero\n", pe.getMessage());
162 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "1000" };
163 EventGeneratorParameters parameters = handler.parse(args);
164 assertEquals(1000, parameters.getDelayBetweenBatches());
165 } catch (ParseException pe) {
166 fail("test should not throw an exception");
171 { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "-1" };
173 fail("test should throw an exception");
174 } catch (ParseException pe) {
175 assertEquals("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
176 + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
177 + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
178 + " field \"batchSize\" type \"int\" value \"1\" INVALID, "
179 + "batchSize must be an integer with a value of zero or more\n", pe.getMessage());
184 { "-c", "src/test/resources/parameters/unit/Default.json", "-o", "Zooby" };
185 EventGeneratorParameters parameters = handler.parse(args);
186 assertEquals("Zooby", parameters.getOutFile());
187 } catch (ParseException pe) {
188 fail("test should not throw an exception");
195 fail("test should throw an exception");
196 } catch (ParseException pe) {
197 assertEquals("Unrecognized option: -z", pe.getMessage());
204 fail("test should throw an exception");
205 } catch (ParseException pe) {
206 assertEquals("Missing argument for option: H", pe.getMessage());
213 fail("test should throw an exception");
214 } catch (ParseException pe) {
215 assertEquals("Missing argument for option: p", pe.getMessage());
220 { "-p", "12345", "-z" };
222 fail("test should throw an exception");
223 } catch (ParseException pe) {
224 assertEquals("Unrecognized option: -z", pe.getMessage());
229 { "-p", "12345", "somethingElse" };
231 fail("test should throw an exception");
232 } catch (ParseException pe) {
233 assertEquals("too many command line arguments specified : [somethingElse]", pe.getMessage());
240 fail("test should throw an exception");
241 } catch (ParseException pe) {
242 assertEquals("Missing argument for option: c", pe.getMessage());
247 { "-H", "MyHost", "-c", "src/test/resources/parameters/unit/Valid.json" };
248 EventGeneratorParameters pars = handler.parse(args);
249 assertEquals("MyHost", pars.getHost());
251 } catch (ParseException pe) {
252 fail("test should not throw an exception");
257 { "-c", "src/test/resources/parameters/unit/NonExistant.json" };
259 fail("test should throw an exception");
260 } catch (ParseException pe) {
261 assertTrue(pe.getMessage().startsWith("Could not read parameters from configuration file "));
266 { "-c", "src/test/resources/parameters/unit/BadHost.json" };
268 fail("test should throw an exception");
269 } catch (ParseException pe) {
270 assertEquals("Error parsing JSON parameters from configuration file "
271 + "\"src/test/resources/parameters/unit/BadHost.json\": "
272 + "com.google.gson.stream.MalformedJsonException: "
273 + "Unexpected value at line 3 column 14 path $.host", pe.getMessage());
278 { "-c", "src/test/resources/parameters/unit/BadPort.json" };
280 fail("test should throw an exception");
281 } catch (ParseException pe) {
282 assertEquals("Error parsing JSON parameters from configuration file "
283 + "\"src/test/resources/parameters/unit/BadPort.json\": "
284 + "java.lang.IllegalStateException: Expected an int "
285 + "but was BOOLEAN at line 4 column 18 path $.port", pe.getMessage());
290 { "-c", "src/test/resources/parameters/unit/Empty.json" };
292 fail("test should throw an exception");
293 } catch (ParseException pe) {
294 assertEquals("No parameters found in configuration file "
295 + "\"src/test/resources/parameters/unit/Empty.json\"", pe.getMessage());
300 { "-c", "src/test/resources/parameters/unit/NullHost.json" };
302 fail("test should throw an exception");
303 } catch (ParseException pe) {
304 assertEquals("specified parameters are not valid: parameter group \"ValidPars\" "
305 + "type \"org.onap.policy.apex.testsuites.performance."
306 + "benchmark.eventgenerator.EventGeneratorParameters\" INVALID, "
307 + "parameter group has status INVALID\n" + " field \"host\" type \"java.lang.String\" "
308 + "value \"null\" INVALID, host must be a non-blank string\n", pe.getMessage());
315 fail("test should throw an exception");
316 } catch (ParseException pe) {
317 assertEquals("specified parameters are not valid: parameter group \""
318 + "EventGeneratorParameters\" type \"org.onap.policy.apex.testsuites.performance.benchmark."
319 + "eventgenerator.EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
320 + " field \"port\" type \"int\" value \"1023\" INVALID, "
321 + "port must be an integer between 1024 and 65535 inclusive\n" + "", pe.getMessage());