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.ApexParameterException;
38 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
39 import org.onap.policy.apex.service.parameters.ApexParameters;
40 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
41 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
44 * Test for an empty parameter file.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class ParameterTests {
50 public void invalidParametersNoFileTest() throws ApexParameterException {
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 ApexParameterException 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 ApexParameterException e) {
72 assertTrue(e.getMessage()
73 .startsWith("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 ApexParameterException e) {
86 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/noParams.json\"\n"
87 + "Apex parameters invalid\n" + " engine service parameters are not specified\n"
88 + " at least one event output and one event input must be specified", e.getMessage());
93 public void invalidParametersBlankParamsTest() {
94 final String[] args = {"-c", "src/test/resources/parameters/blankParams.json"};
95 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
98 new ApexParameterHandler().getParameters(arguments);
99 fail("This test should throw an exception");
100 } catch (final ApexParameterException e) {
102 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/blankParams.json\"\n"
103 + "Apex parameters invalid\n" + " engine service parameters invalid\n"
104 + " id not specified or specified value [-1] invalid, must be specified as id >= 0\n"
105 + " at least one event output and one event input must be specified", e.getMessage());
110 public void invalidParametersTest() {
111 final String[] args = {"-c", "src/test/resources/parameters/badParams.json"};
112 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
115 new ApexParameterHandler().getParameters(arguments);
116 fail("This test should throw an exception");
117 } catch (final ApexParameterException e) {
118 assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/badParams.json\"\n"
119 + "Apex parameters invalid\n" + " engine service parameters invalid\n"
120 + " name [hello there] and/or version [PA1] invalid\n"
121 + " parameter \"name\": value \"hello there\","
122 + " does not match regular expression \"[A-Za-z0-9\\-_\\.]+\"\n"
123 + " id not specified or specified value [-45] invalid, must be specified as id >= 0\n"
124 + " instanceCount [-345] invalid, must be specified as instanceCount >= 1\n"
125 + " deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n"
126 + " policyModelFileName [/some/file/name.xml] not found or is not a plain file\n"
127 + " event input (TheFileConsumer1) parameters invalid\n"
128 + " fileName not specified or is blank or null, it must be specified as a valid file location\n"
129 + " event output (FirstProducer) parameters invalid\n"
130 + " fileName not specified or is blank or null, it must be specified as a valid file location",
136 public void goodParametersTest() {
137 final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"};
138 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
141 final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
143 assertEquals(2, parameters.getEventInputParameters().size());
144 assertEquals(2, parameters.getEventOutputParameters().size());
146 assertTrue(parameters.getEventOutputParameters().containsKey("FirstProducer"));
147 assertTrue(parameters.getEventOutputParameters().containsKey("MyOtherProducer"));
148 assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer")
149 .getCarrierTechnologyParameters().getLabel());
150 assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer")
151 .getCarrierTechnologyParameters().getLabel());
152 assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventOutputParameters()
153 .get("MyOtherProducer").getCarrierTechnologyParameters().getEventProducerPluginClass());
154 assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventOutputParameters()
155 .get("MyOtherProducer").getCarrierTechnologyParameters().getEventConsumerPluginClass());
157 parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel());
158 assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer")
159 .getEventProtocolParameters().getLabel());
161 assertTrue(parameters.getEventInputParameters().containsKey("TheFileConsumer1"));
162 assertTrue(parameters.getEventInputParameters().containsKey("MySuperDooperConsumer1"));
163 assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1")
164 .getCarrierTechnologyParameters().getLabel());
165 assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
166 .getCarrierTechnologyParameters().getLabel());
167 assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1")
168 .getEventProtocolParameters().getLabel());
169 assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
170 .getEventProtocolParameters().getLabel());
171 assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventInputParameters()
172 .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass());
173 assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventInputParameters()
174 .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass());
175 assertEquals(SuperDooperEventProducer.class.getCanonicalName(), parameters.getEventInputParameters()
176 .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass());
177 assertEquals(SuperDooperEventSubscriber.class.getCanonicalName(), parameters.getEventInputParameters()
178 .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass());
179 } catch (final ApexParameterException e) {
180 fail("This test should not throw an exception");
185 public void superDooperParametersTest() {
186 final String[] args = {"-c", "src/test/resources/parameters/superDooperParams.json"};
187 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
190 final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
192 assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName());
193 assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion());
194 assertEquals(45, parameters.getEngineServiceParameters().getId());
195 assertEquals(345, parameters.getEngineServiceParameters().getInstanceCount());
196 assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort());
198 final CarrierTechnologyParameters prodCT =
199 parameters.getEventOutputParameters().get("FirstProducer").getCarrierTechnologyParameters();
200 final EventProtocolParameters prodEP =
201 parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters();
202 final CarrierTechnologyParameters consCT =
203 parameters.getEventInputParameters().get("MySuperDooperConsumer1").getCarrierTechnologyParameters();
204 final EventProtocolParameters consEP =
205 parameters.getEventInputParameters().get("MySuperDooperConsumer1").getEventProtocolParameters();
207 assertEquals("SUPER_DOOPER", prodCT.getLabel());
208 assertEquals("SUPER_TOK_DEL", prodEP.getLabel());
209 assertEquals("SUPER_DOOPER", consCT.getLabel());
210 assertEquals("JSON", consEP.getLabel());
212 assertTrue(prodCT instanceof SuperDooperCarrierTechnologyParameters);
214 final SuperDooperCarrierTechnologyParameters superDooperParameters =
215 (SuperDooperCarrierTechnologyParameters) prodCT;
216 assertEquals("somehost:12345", superDooperParameters.getBootstrapServers());
217 assertEquals("0", superDooperParameters.getAcks());
218 assertEquals(25, superDooperParameters.getRetries());
219 assertEquals(98765, superDooperParameters.getBatchSize());
220 assertEquals(21, superDooperParameters.getLingerTime());
221 assertEquals(50505050, superDooperParameters.getBufferMemory());
222 assertEquals("first-group-id", superDooperParameters.getGroupId());
223 assertFalse(superDooperParameters.isEnableAutoCommit());
224 assertEquals(441, superDooperParameters.getAutoCommitTime());
225 assertEquals(987, superDooperParameters.getSessionTimeout());
226 assertEquals("producer-out", superDooperParameters.getProducerTopic());
227 assertEquals(101, superDooperParameters.getConsumerPollTime());
228 assertEquals("some.key.serailizer", superDooperParameters.getKeySerializer());
229 assertEquals("some.value.serailizer", superDooperParameters.getValueSerializer());
230 assertEquals("some.key.deserailizer", superDooperParameters.getKeyDeserializer());
231 assertEquals("some.value.deserailizer", superDooperParameters.getValueDeserializer());
233 final String[] consumerTopics = {"consumer-out-0", "consumer-out-1", "consumer-out-2"};
234 assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList());
235 } catch (final ApexParameterException e) {
236 fail("This test should not throw an exception");