6b15ae45452ac8c3de1949c656344a08dcd6cb31
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
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() throws ParseException {
39         EventGeneratorParameterHandler handler = new EventGeneratorParameterHandler();
40         assertNotNull(handler);
41
42         String[] args = { "-h" };
43         EventGeneratorParameters parameters = handler.parse(args);
44         assertNull(parameters);
45         assertEquals("usage: EventGenerator [options...]",
46                         handler.getHelp(EventGenerator.class.getSimpleName()).substring(0, 34));
47
48         args = new String[] {};
49         parameters = handler.parse(args);
50         assertEquals("localhost", parameters.getHost());
51         assertEquals(32801, parameters.getPort());
52
53         args = new String[] { "-H", "MyHost" };
54         parameters = handler.parse(args);
55         assertEquals("MyHost", parameters.getHost());
56
57         args = new String[] { "-p", "12345" };
58         parameters = handler.parse(args);
59         assertEquals(12345, parameters.getPort());
60
61
62         args = new String[] { "-H", "MyHost", "-p", "12345" };
63         parameters = handler.parse(args);
64         assertEquals("MyHost", parameters.getHost());
65         assertEquals(12345, parameters.getPort());
66
67         args = new String[] { "-c", "src/test/resources/parameters/unit/Valid.json" };
68         parameters = handler.parse(args);
69         assertEquals("ValidPars", parameters.getName());
70         assertEquals("FileHost", parameters.getHost());
71         assertEquals(54321, parameters.getPort());
72
73         args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json" };
74         parameters = handler.parse(args);
75         assertEquals("localhost", parameters.getHost());
76         assertEquals(32801, parameters.getPort());
77
78         args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "100" };
79         parameters = handler.parse(args);
80         assertEquals(100, parameters.getBatchCount());
81
82         assertThatThrownBy(() -> {
83             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bc", "-1" };
84             handler.parse(arguments);
85         }).hasMessage("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
86                             + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
87                             + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
88                             + "  field \"batchCount\" type \"int\" value \"-1\" INVALID, "
89                             + "batchCount must be an integer with a value of zero or more, "
90                             + "zero means generate batches forever\n");
91         args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "12345" };
92         parameters = handler.parse(args);
93         assertEquals(12345, parameters.getBatchSize());
94
95         assertThatThrownBy(() -> {
96             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bs", "0" };
97             handler.parse(arguments);
98         }).hasMessage("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
99                             + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
100                             + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
101                             + "  field \"batchSize\" type \"int\" value \"0\" INVALID, "
102                             + "batchSize must be an integer greater than zero\n");
103         args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "1000" };
104         parameters = handler.parse(args);
105         assertEquals(1000, parameters.getDelayBetweenBatches());
106
107         assertThatThrownBy(() -> {
108             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-bd", "-1" };
109             handler.parse(arguments);
110         }).hasMessage("specified parameters are not valid: parameter group \"EventGeneratorParameters\" "
111                             + "type \"org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator."
112                             + "EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
113                             + "  field \"batchSize\" type \"int\" value \"1\" INVALID, "
114                             + "batchSize must be an integer with a value of zero or more\n");
115
116         args = new String[] { "-c", "src/test/resources/parameters/unit/Default.json", "-o", "Zooby" };
117         parameters = handler.parse(args);
118         assertEquals("Zooby", parameters.getOutFile());
119
120         assertThatThrownBy(() -> {
121             String[] arguments = new String[] { "-z" };
122             handler.parse(arguments);
123         }).hasMessage("Unrecognized option: -z");
124
125         assertThatThrownBy(() -> {
126             String[] arguments = new String[] { "-H" };
127             handler.parse(arguments);
128         }).hasMessage("Missing argument for option: H");
129
130         assertThatThrownBy(() -> {
131             String[] arguments = new String[] { "-p" };
132             handler.parse(arguments);
133         }).hasMessage("Missing argument for option: p");
134
135         assertThatThrownBy(() -> {
136             String[] arguments = new String[] { "-p", "12345", "-z" };
137             handler.parse(arguments);
138         }).hasMessage("Unrecognized option: -z");
139
140         assertThatThrownBy(() -> {
141             String[] arguments = new String[] { "-p", "12345", "somethingElse" };
142             handler.parse(arguments);
143         }).hasMessage("too many command line arguments specified : [somethingElse]");
144
145         assertThatThrownBy(() -> {
146             String[] arguments = new String[] { "-c" };
147             handler.parse(arguments);
148         }).hasMessage("Missing argument for option: c");
149
150         args = new String[] { "-H", "MyHost", "-c", "src/test/resources/parameters/unit/Valid.json" };
151         parameters = handler.parse(args);
152         assertEquals("MyHost", parameters.getHost());
153
154         assertThatThrownBy(() -> {
155             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/NonExistant.json" };
156             handler.parse(arguments);
157         }).hasMessageStartingWith("Could not read parameters from configuration file ");
158
159         assertThatThrownBy(() -> {
160             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/BadHost.json" };
161             handler.parse(arguments);
162         }).hasMessage("Error parsing JSON parameters from configuration file "
163                             + "\"src/test/resources/parameters/unit/BadHost.json\": "
164                             + "com.google.gson.stream.MalformedJsonException: "
165                             + "Unexpected value at line 3 column 14 path $.host");
166
167         assertThatThrownBy(() -> {
168             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/BadPort.json" };
169             handler.parse(arguments);
170         }).hasMessage("Error parsing JSON parameters from configuration file "
171                             + "\"src/test/resources/parameters/unit/BadPort.json\": "
172                             + "java.lang.IllegalStateException: Expected an int "
173                             + "but was BOOLEAN at line 4 column 18 path $.port");
174
175         assertThatThrownBy(() -> {
176             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/Empty.json" };
177             handler.parse(arguments);
178         }).hasMessage("No parameters found in configuration file "
179                 + "\"src/test/resources/parameters/unit/Empty.json\"");
180
181         assertThatThrownBy(() -> {
182             String[] arguments = new String[] { "-c", "src/test/resources/parameters/unit/NullHost.json" };
183             handler.parse(arguments);
184         }).hasMessage("specified parameters are not valid: parameter group \"ValidPars\" "
185                             + "type \"org.onap.policy.apex.testsuites.performance."
186                             + "benchmark.eventgenerator.EventGeneratorParameters\" INVALID, "
187                             + "parameter group has status INVALID\n" + "  field \"host\" type \"java.lang.String\" "
188                             + "value \"null\" INVALID, host must be a non-blank string\n");
189
190         assertThatThrownBy(() -> {
191             String[] arguments = new String[] { "-p", "1023" };
192             handler.parse(arguments);
193         }).hasMessage("specified parameters are not valid: parameter group \""
194                             + "EventGeneratorParameters\" type \"org.onap.policy.apex.testsuites.performance.benchmark."
195                             + "eventgenerator.EventGeneratorParameters\" INVALID, parameter group has status INVALID\n"
196                             + "  field \"port\" type \"int\" value \"1023\" INVALID, "
197                             + "port must be an integer between 1024 and 65535 inclusive\n" + "");
198     }
199 }