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 {
51 final String[] args = { "-c", "src/test/resources/parameters/invalidNoFile.json" };
52 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
55 new ApexParameterHandler().getParameters(arguments);
56 fail("This test should throw an exception");
57 } catch (final ParameterException e) {
58 assertTrue(e.getMessage().startsWith("error reading parameters from \"src"));
59 assertTrue(e.getMessage().contains("FileNotFoundException"));
64 public void invalidParametersEmptyTest() {
65 final String[] args = { "-c", "src/test/resources/parameters/empty.json" };
66 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
69 new ApexParameterHandler().getParameters(arguments);
70 fail("This test should throw an exception");
71 } catch (final ParameterException e) {
72 assertTrue(e.getMessage().startsWith(
73 "validation error(s) on parameters from \"src/test/resources/parameters/empty.json\""));
78 public void invalidParametersNoParamsTest() {
79 final String[] args = { "-c", "src/test/resources/parameters/noParams.json" };
80 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
83 new ApexParameterHandler().getParameters(arguments);
84 fail("This test should throw an exception");
85 } catch (final ParameterException e) {
86 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/noParams.json\"\n"
87 + "parameter group \"APEX_PARAMETERS\" type "
88 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
89 + "parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, "
90 + "engine service parameters are not specified\n"
91 + " parameter group map \"eventOutputParameters\" INVALID, "
92 + "at least one event output must be specified\n"
93 + " parameter group map \"eventInputParameters\" INVALID, "
94 + "at least one event input must be specified\n", e.getMessage());
99 public void invalidParametersBlankParamsTest() {
100 final String[] args = { "-c", "src/test/resources/parameters/blankParams.json" };
101 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
104 new ApexParameterHandler().getParameters(arguments);
105 fail("This test should throw an exception");
106 } catch (final ParameterException e) {
108 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/blankParams.json\"\n"
109 + "parameter group \"APEX_PARAMETERS\" type "
110 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
111 + "parameter group has status INVALID\n"
112 + " parameter group \"ENGINE_SERVICE_PARAMETERS\" type "
113 + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" "
114 + "INVALID, parameter group has status INVALID\n"
115 + " field \"id\" type \"int\" value \"-1\" INVALID, "
116 + "id not specified or specified value [-1] invalid, must be specified as id >= 0\n"
117 + " parameter group map \"eventOutputParameters\" INVALID, "
118 + "at least one event output must be specified\n"
119 + " parameter group map \"eventInputParameters\" INVALID, "
120 + "at least one event input must be specified\n", e.getMessage());
125 public void invalidParametersTest() {
126 final String[] args = { "-c", "src/test/resources/parameters/badParams.json" };
127 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
130 new ApexParameterHandler().getParameters(arguments);
131 fail("This test should throw an exception");
132 } catch (final ParameterException e) {
133 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/badParams.json\"\n"
134 + "parameter group \"APEX_PARAMETERS\" type "
135 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
136 + "parameter group has status INVALID\n" + " parameter group \"hello there\" type "
137 + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" "
138 + "INVALID, parameter group has status INVALID\n"
139 + " field \"name\" type \"java.lang.String\" value \"hello there\" INVALID, "
140 + "name is invalid, it must match regular expression[A-Za-z0-9\\-_\\.]+\n"
141 + " field \"id\" type \"int\" value \"-45\" INVALID, id not specified or "
142 + "specified value [-45] invalid, must be specified as id >= 0\n"
143 + " field \"instanceCount\" type \"int\" value \"-345\" INVALID, "
144 + "instanceCount [-345] invalid, must be specified as instanceCount >= 1\n"
145 + " field \"deploymentPort\" type \"int\" value \"65536\" INVALID, "
146 + "deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n"
147 + " field \"policyModelFileName\" type \"java.lang.String\" "
148 + "value \"/some/file/name.xml\" INVALID, not found or is not a plain file\n"
149 + " parameter group map \"eventOutputParameters\" INVALID, "
150 + "parameter group has status INVALID\n" + " parameter group \"FirstProducer\" type "
151 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID"
152 + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type "
153 + "\"org.onap.policy.apex.service.engine.event.impl."
154 + "filecarrierplugin.FILECarrierTechnologyParameters\" INVALID, "
155 + "parameter group has status INVALID\n"
156 + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, "
157 + "fileName not specified or is blank or null, "
158 + "it must be specified as a valid file location\n"
159 + " parameter group map \"eventInputParameters\" INVALID, "
160 + "parameter group has status INVALID\n" + " parameter group \"TheFileConsumer1\" type "
161 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID"
162 + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type "
163 + "\"org.onap.policy.apex.service.engine.event.impl."
164 + "filecarrierplugin.FILECarrierTechnologyParameters\" INVALID, "
165 + "parameter group has status INVALID\n"
166 + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, "
167 + "fileName not specified or is blank or null, "
168 + "it must be specified as a valid file location\n", e.getMessage());
173 public void goodParametersTest() {
174 final String[] args = { "-c", "src/test/resources/parameters/goodParams.json" };
175 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
178 final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
180 assertEquals(2, parameters.getEventInputParameters().size());
181 assertEquals(2, parameters.getEventOutputParameters().size());
183 assertTrue(parameters.getEventOutputParameters().containsKey("FirstProducer"));
184 assertTrue(parameters.getEventOutputParameters().containsKey("MyOtherProducer"));
185 assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer")
186 .getCarrierTechnologyParameters().getLabel());
187 assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer")
188 .getCarrierTechnologyParameters().getLabel());
189 assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventOutputParameters()
190 .get("MyOtherProducer").getCarrierTechnologyParameters().getEventProducerPluginClass());
191 assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventOutputParameters()
192 .get("MyOtherProducer").getCarrierTechnologyParameters().getEventConsumerPluginClass());
193 assertEquals("JSON", parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters()
195 assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer")
196 .getEventProtocolParameters().getLabel());
198 assertTrue(parameters.getEventInputParameters().containsKey("TheFileConsumer1"));
199 assertTrue(parameters.getEventInputParameters().containsKey("MySuperDooperConsumer1"));
200 assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1")
201 .getCarrierTechnologyParameters().getLabel());
202 assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
203 .getCarrierTechnologyParameters().getLabel());
204 assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1")
205 .getEventProtocolParameters().getLabel());
206 assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
207 .getEventProtocolParameters().getLabel());
208 assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventInputParameters()
209 .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass());
210 assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventInputParameters()
211 .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass());
212 assertEquals(SuperDooperEventProducer.class.getCanonicalName(),
213 parameters.getEventInputParameters().get("MySuperDooperConsumer1")
214 .getCarrierTechnologyParameters().getEventProducerPluginClass());
215 assertEquals(SuperDooperEventSubscriber.class.getCanonicalName(),
216 parameters.getEventInputParameters().get("MySuperDooperConsumer1")
217 .getCarrierTechnologyParameters().getEventConsumerPluginClass());
218 } catch (final ParameterException e) {
219 fail("This test should not throw an exception");
224 public void superDooperParametersTest() {
225 final String[] args = { "-c", "src/test/resources/parameters/superDooperParams.json" };
226 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
229 final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
231 assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName());
232 assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion());
233 assertEquals(45, parameters.getEngineServiceParameters().getId());
234 assertEquals(345, parameters.getEngineServiceParameters().getInstanceCount());
235 assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort());
237 final CarrierTechnologyParameters prodCT = parameters.getEventOutputParameters().get("FirstProducer")
238 .getCarrierTechnologyParameters();
239 final EventProtocolParameters prodEP = parameters.getEventOutputParameters().get("FirstProducer")
240 .getEventProtocolParameters();
241 final CarrierTechnologyParameters consCT = parameters.getEventInputParameters()
242 .get("MySuperDooperConsumer1").getCarrierTechnologyParameters();
243 final EventProtocolParameters consEP = parameters.getEventInputParameters().get("MySuperDooperConsumer1")
244 .getEventProtocolParameters();
246 assertEquals("SUPER_DOOPER", prodCT.getLabel());
247 assertEquals("SUPER_TOK_DEL", prodEP.getLabel());
248 assertEquals("SUPER_DOOPER", consCT.getLabel());
249 assertEquals("JSON", consEP.getLabel());
251 assertTrue(prodCT instanceof SuperDooperCarrierTechnologyParameters);
253 final SuperDooperCarrierTechnologyParameters superDooperParameters = (SuperDooperCarrierTechnologyParameters) prodCT;
254 assertEquals("somehost:12345", superDooperParameters.getBootstrapServers());
255 assertEquals("0", superDooperParameters.getAcks());
256 assertEquals(25, superDooperParameters.getRetries());
257 assertEquals(98765, superDooperParameters.getBatchSize());
258 assertEquals(21, superDooperParameters.getLingerTime());
259 assertEquals(50505050, superDooperParameters.getBufferMemory());
260 assertEquals("first-group-id", superDooperParameters.getGroupId());
261 assertFalse(superDooperParameters.isEnableAutoCommit());
262 assertEquals(441, superDooperParameters.getAutoCommitTime());
263 assertEquals(987, superDooperParameters.getSessionTimeout());
264 assertEquals("producer-out", superDooperParameters.getProducerTopic());
265 assertEquals(101, superDooperParameters.getConsumerPollTime());
266 assertEquals("some.key.serailizer", superDooperParameters.getKeySerializer());
267 assertEquals("some.value.serailizer", superDooperParameters.getValueSerializer());
268 assertEquals("some.key.deserailizer", superDooperParameters.getKeyDeserializer());
269 assertEquals("some.value.deserailizer", superDooperParameters.getValueDeserializer());
271 final String[] consumerTopics = { "consumer-out-0", "consumer-out-1", "consumer-out-2" };
272 assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList());
273 } catch (final ParameterException e) {
274 fail("This test should not throw an exception");