2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
30 import org.apache.commons.cli.ParseException;
31 import org.junit.Test;
34 * Test event generator parameters.
36 public class EventGeneratorParametersHandlerTest {
39 public void testEventGeneratorParameterhandler() throws ParseException {
40 EventGeneratorParameterHandler handler = new EventGeneratorParameterHandler();
41 assertNotNull(handler);
43 String[] args = { "-h" };
44 EventGeneratorParameters parameters = handler.parse(args);
45 assertNull(parameters);
46 assertEquals("usage: EventGenerator [options...]",
47 handler.getHelp(EventGenerator.class.getSimpleName()).substring(0, 34));
49 args = new String[] {};
50 parameters = handler.parse(args);
51 assertEquals("localhost", parameters.getHost());
52 assertEquals(32801, parameters.getPort());
54 args = new String[] { "-H", "MyHost" };
55 parameters = handler.parse(args);
56 assertEquals("MyHost", parameters.getHost());
58 args = new String[] { "-p", "12345" };
59 parameters = handler.parse(args);
60 assertEquals(12345, parameters.getPort());
63 args = new String[] { "-H", "MyHost", "-p", "12345" };
64 parameters = handler.parse(args);
65 assertEquals("MyHost", parameters.getHost());
66 assertEquals(12345, parameters.getPort());
68 args = new String[] { "-c", "src/test/resources/parameters/unit/Valid.json" };
69 parameters = handler.parse(args);
70 assertEquals("ValidPars", parameters.getName());
71 assertEquals("FileHost", parameters.getHost());
72 assertEquals(54321, parameters.getPort());
74 args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json" };
75 parameters = handler.parse(args);
76 assertEquals("localhost", parameters.getHost());
77 assertEquals(32801, parameters.getPort());
79 args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "100" };
80 parameters = handler.parse(args);
81 assertEquals(100, parameters.getBatchCount());
83 assertThatThrownBy(() -> {
84 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "-1" };
85 handler.parse(arguments);
86 }).hasMessageContaining("specified parameters are not valid", "EventGeneratorParameters",
87 "\"batchCount\" value \"-1\" INVALID, is below the minimum");
88 args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "12345" };
89 parameters = handler.parse(args);
90 assertEquals(12345, parameters.getBatchSize());
92 assertThatThrownBy(() -> {
93 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "0" };
94 handler.parse(arguments);
95 }).hasMessageContaining("specified parameters are not valid", "EventGeneratorParameters",
96 "\"batchSize\" value \"0\" INVALID, is below the minimum");
97 args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "1000" };
98 parameters = handler.parse(args);
99 assertEquals(1000, parameters.getDelayBetweenBatches());
101 assertThatThrownBy(() -> {
102 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "-1" };
103 handler.parse(arguments);
104 }).hasMessageContaining("specified parameters are not valid", "EventGeneratorParameters",
105 "\"batchSize\" value \"1\" INVALID, is below the minimum");
107 args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-o", "Zooby" };
108 parameters = handler.parse(args);
109 assertEquals("Zooby", parameters.getOutFile());
111 assertThatThrownBy(() -> {
112 String[] arguments = new String[] { "-z" };
113 handler.parse(arguments);
114 }).hasMessage("Unrecognized option: -z");
116 assertThatThrownBy(() -> {
117 String[] arguments = new String[] { "-H" };
118 handler.parse(arguments);
119 }).hasMessage("Missing argument for option: H");
121 assertThatThrownBy(() -> {
122 String[] arguments = new String[] { "-p" };
123 handler.parse(arguments);
124 }).hasMessage("Missing argument for option: p");
126 assertThatThrownBy(() -> {
127 String[] arguments = new String[] { "-p", "12345", "-z" };
128 handler.parse(arguments);
129 }).hasMessage("Unrecognized option: -z");
131 assertThatThrownBy(() -> {
132 String[] arguments = new String[] { "-p", "12345", "somethingElse" };
133 handler.parse(arguments);
134 }).hasMessage("too many command line arguments specified : [somethingElse]");
136 assertThatThrownBy(() -> {
137 String[] arguments = new String[] { "-c" };
138 handler.parse(arguments);
139 }).hasMessage("Missing argument for option: c");
141 args = new String[] { "-H", "MyHost", "-c", "src/test/resources/parameters/unit/Valid.json" };
142 parameters = handler.parse(args);
143 assertEquals("MyHost", parameters.getHost());
145 assertThatThrownBy(() -> {
146 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/NonExistant.json" };
147 handler.parse(arguments);
148 }).hasMessageStartingWith("Could not read parameters from configuration file ");
150 assertThatThrownBy(() -> {
151 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/BadHost.json" };
152 handler.parse(arguments);
153 }).hasMessage("Error parsing JSON parameters from configuration file "
154 + "\"src/test/resources/parameters/unit/BadHost.json\": "
155 + "com.google.gson.stream.MalformedJsonException: "
156 + "Unexpected value at line 3 column 14 path $.host");
158 assertThatThrownBy(() -> {
159 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/BadPort.json" };
160 handler.parse(arguments);
161 }).hasMessage("Error parsing JSON parameters from configuration file "
162 + "\"src/test/resources/parameters/unit/BadPort.json\": "
163 + "java.lang.IllegalStateException: Expected an int "
164 + "but was BOOLEAN at line 4 column 18 path $.port");
166 assertThatThrownBy(() -> {
167 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Empty.json" };
168 handler.parse(arguments);
169 }).hasMessage("No parameters found in configuration file "
170 + "\"src/test/resources/parameters/unit/Empty.json\"");
172 assertThatThrownBy(() -> {
173 String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/NullHost.json" };
174 handler.parse(arguments);
175 }).hasMessageContaining("specified parameters are not valid", "EventGeneratorParameters",
176 "\"host\" value \"null\" INVALID, is null");
178 assertThatThrownBy(() -> {
179 String[] arguments = new String[] { "-p", "1023" };
180 handler.parse(arguments);
181 }).hasMessageContaining("specified parameters are not valid", "EventGeneratorParameters",
182 "\"port\" value \"1023\" INVALID, is below the minimum");