2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-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.service.engine.parameters;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.util.Arrays;
30 import org.junit.Test;
31 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
32 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer;
33 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
34 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters;
35 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventProducer;
36 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventSubscriber;
37 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
38 import org.onap.policy.apex.service.parameters.ApexParameters;
39 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
40 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
41 import org.onap.policy.common.parameters.ParameterException;
44 * Test for an empty parameter file.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class ParameterTests {
50 public void invalidParametersNoFileTest() throws ParameterException {
52 { "-c", "src/test/resources/parameters/invalidNoFile.json" };
53 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
56 new ApexParameterHandler().getParameters(arguments);
57 fail("This test should throw an exception");
58 } catch (final ParameterException e) {
59 assertTrue(e.getMessage().startsWith("error reading parameters from \"src"));
60 assertTrue(e.getMessage().contains("FileNotFoundException"));
65 public void invalidParametersEmptyTest() {
67 { "-c", "src/test/resources/parameters/empty.json" };
68 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
71 new ApexParameterHandler().getParameters(arguments);
72 fail("This test should throw an exception");
73 } catch (final ParameterException e) {
74 assertTrue(e.getMessage().startsWith(
75 "validation error(s) on parameters from \"src/test/resources/parameters/empty.json\""));
80 public void invalidParametersNoParamsTest() {
82 { "-c", "src/test/resources/parameters/noParams.json" };
83 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
86 new ApexParameterHandler().getParameters(arguments);
87 fail("This test should throw an exception");
88 } catch (final ParameterException e) {
89 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/noParams.json\"\n"
90 + "parameter group \"APEX_PARAMETERS\" type "
91 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
92 + "parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, "
93 + "engine service parameters are not specified\n"
94 + " parameter group map \"eventOutputParameters\" INVALID, "
95 + "at least one event output must be specified\n"
96 + " parameter group map \"eventInputParameters\" INVALID, "
97 + "at least one event input must be specified\n", e.getMessage());
102 public void invalidParametersBlankParamsTest() {
103 final String[] args =
104 { "-c", "src/test/resources/parameters/blankParams.json" };
105 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
108 new ApexParameterHandler().getParameters(arguments);
109 fail("This test should throw an exception");
110 } catch (final ParameterException e) {
112 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/blankParams.json\"\n"
113 + "parameter group \"APEX_PARAMETERS\" type "
114 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
115 + "parameter group has status INVALID\n"
116 + " parameter group \"ENGINE_SERVICE_PARAMETERS\" type "
117 + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" "
118 + "INVALID, parameter group has status INVALID\n"
119 + " field \"id\" type \"int\" value \"-1\" INVALID, "
120 + "id not specified or specified value [-1] invalid, must be specified as id >= 0\n"
121 + " parameter group map \"eventOutputParameters\" INVALID, "
122 + "at least one event output must be specified\n"
123 + " parameter group map \"eventInputParameters\" INVALID, "
124 + "at least one event input must be specified\n", e.getMessage());
129 public void invalidParametersTest() {
130 final String[] args =
131 { "-c", "src/test/resources/parameters/badParams.json" };
132 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
135 new ApexParameterHandler().getParameters(arguments);
136 fail("This test should throw an exception");
137 } catch (final ParameterException e) {
138 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/badParams.json\"\n"
139 + "parameter group \"APEX_PARAMETERS\" type "
140 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
141 + "parameter group has status INVALID\n" + " parameter group \"hello there\" type "
142 + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" "
143 + "INVALID, parameter group has status INVALID\n"
144 + " field \"name\" type \"java.lang.String\" value \"hello there\" INVALID, "
145 + "name is invalid, it must match regular expression[A-Za-z0-9\\-_\\.]+\n"
146 + " field \"id\" type \"int\" value \"-45\" INVALID, id not specified or "
147 + "specified value [-45] invalid, must be specified as id >= 0\n"
148 + " field \"instanceCount\" type \"int\" value \"-345\" INVALID, "
149 + "instanceCount [-345] invalid, must be specified as instanceCount >= 1\n"
150 + " field \"deploymentPort\" type \"int\" value \"65536\" INVALID, "
151 + "deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n"
152 + " field \"policyModelFileName\" type \"java.lang.String\" "
153 + "value \"/some/file/name.xml\" INVALID, not found or is not a plain file\n"
154 + " parameter group map \"eventOutputParameters\" INVALID, "
155 + "parameter group has status INVALID\n" + " parameter group \"FirstProducer\" type "
156 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID"
157 + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type "
158 + "\"org.onap.policy.apex.service.engine.event.impl."
159 + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, "
160 + "parameter group has status INVALID\n"
161 + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, "
162 + "fileName not specified or is blank or null, "
163 + "it must be specified as a valid file location\n"
164 + " parameter group map \"eventInputParameters\" INVALID, "
165 + "parameter group has status INVALID\n" + " parameter group \"TheFileConsumer1\" type "
166 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID"
167 + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type "
168 + "\"org.onap.policy.apex.service.engine.event.impl."
169 + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, "
170 + "parameter group has status INVALID\n"
171 + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, "
172 + "fileName not specified or is blank or null, "
173 + "it must be specified as a valid file location\n", e.getMessage());
178 public void goodParametersTest() {
179 final String[] args =
180 { "-c", "src/test/resources/parameters/goodParams.json" };
181 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
184 final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
186 assertEquals(2, parameters.getEventInputParameters().size());
187 assertEquals(2, parameters.getEventOutputParameters().size());
189 assertTrue(parameters.getEventOutputParameters().containsKey("FirstProducer"));
190 assertTrue(parameters.getEventOutputParameters().containsKey("MyOtherProducer"));
191 assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer")
192 .getCarrierTechnologyParameters().getLabel());
193 assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer")
194 .getCarrierTechnologyParameters().getLabel());
195 assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventOutputParameters()
196 .get("MyOtherProducer").getCarrierTechnologyParameters().getEventProducerPluginClass());
197 assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventOutputParameters()
198 .get("MyOtherProducer").getCarrierTechnologyParameters().getEventConsumerPluginClass());
199 assertEquals("JSON", parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters()
201 assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer")
202 .getEventProtocolParameters().getLabel());
204 assertTrue(parameters.getEventInputParameters().containsKey("TheFileConsumer1"));
205 assertTrue(parameters.getEventInputParameters().containsKey("MySuperDooperConsumer1"));
206 assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1")
207 .getCarrierTechnologyParameters().getLabel());
208 assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
209 .getCarrierTechnologyParameters().getLabel());
210 assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1")
211 .getEventProtocolParameters().getLabel());
212 assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
213 .getEventProtocolParameters().getLabel());
214 assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventInputParameters()
215 .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass());
216 assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventInputParameters()
217 .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass());
218 assertEquals(SuperDooperEventProducer.class.getCanonicalName(),
219 parameters.getEventInputParameters().get("MySuperDooperConsumer1")
220 .getCarrierTechnologyParameters().getEventProducerPluginClass());
221 assertEquals(SuperDooperEventSubscriber.class.getCanonicalName(),
222 parameters.getEventInputParameters().get("MySuperDooperConsumer1")
223 .getCarrierTechnologyParameters().getEventConsumerPluginClass());
224 } catch (final ParameterException e) {
225 fail("This test should not throw an exception");
230 public void superDooperParametersTest() {
231 final String[] args =
232 { "-c", "src/test/resources/parameters/superDooperParams.json" };
233 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
236 final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
238 assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName());
239 assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion());
240 assertEquals(45, parameters.getEngineServiceParameters().getId());
241 assertEquals(345, parameters.getEngineServiceParameters().getInstanceCount());
242 assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort());
244 final CarrierTechnologyParameters prodCarrierTech = parameters.getEventOutputParameters()
245 .get("FirstProducer").getCarrierTechnologyParameters();
246 final EventProtocolParameters prodEventProt = parameters.getEventOutputParameters().get("FirstProducer")
247 .getEventProtocolParameters();
248 final CarrierTechnologyParameters consCarrierTech = parameters.getEventInputParameters()
249 .get("MySuperDooperConsumer1").getCarrierTechnologyParameters();
250 final EventProtocolParameters consEventProt = parameters.getEventInputParameters()
251 .get("MySuperDooperConsumer1").getEventProtocolParameters();
253 assertEquals("SUPER_DOOPER", prodCarrierTech.getLabel());
254 assertEquals("SUPER_TOK_DEL", prodEventProt.getLabel());
255 assertEquals("SUPER_DOOPER", consCarrierTech.getLabel());
256 assertEquals("JSON", consEventProt.getLabel());
258 assertTrue(prodCarrierTech instanceof SuperDooperCarrierTechnologyParameters);
260 final SuperDooperCarrierTechnologyParameters superDooperParameters =
261 (SuperDooperCarrierTechnologyParameters) prodCarrierTech;
262 assertEquals("somehost:12345", superDooperParameters.getBootstrapServers());
263 assertEquals("0", superDooperParameters.getAcks());
264 assertEquals(25, superDooperParameters.getRetries());
265 assertEquals(98765, superDooperParameters.getBatchSize());
266 assertEquals(21, superDooperParameters.getLingerTime());
267 assertEquals(50505050, superDooperParameters.getBufferMemory());
268 assertEquals("first-group-id", superDooperParameters.getGroupId());
269 assertFalse(superDooperParameters.isEnableAutoCommit());
270 assertEquals(441, superDooperParameters.getAutoCommitTime());
271 assertEquals(987, superDooperParameters.getSessionTimeout());
272 assertEquals("producer-out", superDooperParameters.getProducerTopic());
273 assertEquals(101, superDooperParameters.getConsumerPollTime());
274 assertEquals("some.key.serailizer", superDooperParameters.getKeySerializer());
275 assertEquals("some.value.serailizer", superDooperParameters.getValueSerializer());
276 assertEquals("some.key.deserailizer", superDooperParameters.getKeyDeserializer());
277 assertEquals("some.value.deserailizer", superDooperParameters.getValueDeserializer());
279 final String[] consumerTopics =
280 { "consumer-out-0", "consumer-out-1", "consumer-out-2" };
281 assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList());
282 } catch (final ParameterException e) {
283 fail("This test should not throw an exception");