Replace try/catch with assertj
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / parameters / ProducerConsumerTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.service.engine.parameters;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.junit.Test;
30 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
31 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
32 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
33 import org.onap.policy.apex.service.parameters.ApexParameters;
34 import org.onap.policy.common.parameters.ParameterException;
35
36 /**
37  * Test for an empty parameter file.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class ProducerConsumerTests {
42     @Test
43     public void testGoodParametersTest() throws ParameterException {
44         final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"};
45         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
46
47         final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
48
49         assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName());
50         assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion());
51         assertEquals(45, parameters.getEngineServiceParameters().getId());
52         assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount());
53         assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort());
54         assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer")
55                 .getCarrierTechnologyParameters().getLabel());
56         assertEquals("JSON",
57                 parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel());
58         assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer")
59                 .getCarrierTechnologyParameters().getLabel());
60         assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer")
61                 .getEventProtocolParameters().getLabel());
62         assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1")
63                 .getCarrierTechnologyParameters().getLabel());
64         assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1")
65                 .getEventProtocolParameters().getLabel());
66         assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
67                 .getCarrierTechnologyParameters().getLabel());
68         assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1")
69                 .getEventProtocolParameters().getLabel());
70     }
71
72     @Test
73     public void testNoCarrierTechnology() {
74         final String[] args = {"-c", "src/test/resources/parameters/prodConsNoCT.json"};
75         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
76
77         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
78             .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoCT.json\"\n"
79                 + "parameter group \"APEX_PARAMETERS\" type "
80                 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
81                 + "parameter group has status INVALID\n"
82                 + "  parameter group map \"eventInputParameters\" INVALID, "
83                 + "parameter group map has status INVALID\n" + "    parameter group \"aConsumer\" type "
84                 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID,"
85                 + " parameter group has status INVALID\n" + "      parameter group \"UNDEFINED\" INVALID, "
86                 + "event handler carrierTechnologyParameters not specified or blank\n");
87     }
88
89     @Test
90     public void testNoEventProcol() {
91         final String[] args = {"-c", "src/test/resources/parameters/prodConsNoEP.json"};
92         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
93
94         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
95             .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoEP.json\"\n"
96                 + "parameter group \"APEX_PARAMETERS\" type "
97                 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
98                 + "parameter group has status INVALID\n"
99                 + "  parameter group map \"eventOutputParameters\" INVALID, "
100                 + "parameter group map has status INVALID\n" + "    parameter group \"aProducer\" type "
101                 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID"
102                 + ", parameter group has status INVALID\n" + "      parameter group \"UNDEFINED\" INVALID, "
103                 + "event handler eventProtocolParameters not specified or blank\n"
104                 + "  parameter group map \"eventInputParameters\" INVALID, "
105                 + "parameter group map has status INVALID\n" + "    parameter group \"aConsumer\" type "
106                 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID"
107                 + ", parameter group has status INVALID\n" + "      parameter group \"FILE\" type "
108                 + "\"org.onap.policy.apex.service.engine.event.impl."
109                 + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, "
110                 + "parameter group has status INVALID\n"
111                 + "        field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, "
112                 + "\"null\" invalid, must be specified as a non-empty string\n");
113     }
114
115     @Test
116     public void testNoCarrierTechnologyParClass() {
117         final String[] args = {"-c", "src/test/resources/parameters/prodConsNoCTParClass.json"};
118         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
119
120         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
121             .hasMessage("error reading parameters from \"src/test/resources/parameters/prodConsNoCTParClass.json\"\n"
122                 + "(ParameterRuntimeException):carrier technology \"SUPER_DOOPER\" "
123                 + "parameter \"parameterClassName\" value \"null\" invalid in JSON file");
124     }
125
126     @Test
127     public void testMismatchCarrierTechnologyParClass() {
128         final String[] args = {"-c", "src/test/resources/parameters/prodConsMismatchCTParClass.json"};
129         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
130
131         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
132             .hasMessage("error reading parameters from "
133                 + "\"src/test/resources/parameters/prodConsMismatchCTParClass.json\"\n"
134                 + "(ParameterRuntimeException):carrier technology \"SUPER_LOOPER\" "
135                 + "does not match plugin \"SUPER_DOOPER\" in \"" + "org.onap.policy.apex.service.engine."
136                 + "parameters.dummyclasses.SuperDooperCarrierTechnologyParameters"
137                 + "\", specify correct carrier technology parameter plugin "
138                 + "in parameter \"parameterClassName\"");
139     }
140
141     @Test
142     public void testWrongTypeCarrierTechnologyParClass() {
143         final String[] args = {"-c", "src/test/resources/parameters/prodConsWrongTypeCTParClass.json"};
144         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
145
146         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
147             .hasMessage("error reading parameters from "
148                 + "\"src/test/resources/parameters/prodConsWrongTypeCTParClass.json\"\n"
149                 + "(ParameterRuntimeException):could not create default parameters for carrier technology "
150                 + "\"SUPER_DOOPER\"\n" + "class org.onap.policy.apex.service.engine.parameters.dummyclasses."
151                 + "SuperTokenDelimitedEventProtocolParameters cannot be cast to class "
152                 + "org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters (org.onap."
153                 + "policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters "
154                 + "and org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters are in"
155                 + " unnamed module of loader 'app')");
156     }
157
158     @Test
159     public void testOkFileNameCarrierTechnology() throws ParameterException {
160         final String[] args = {"-c", "src/test/resources/parameters/prodConsOKFileName.json"};
161         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
162
163         final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
164         final FileCarrierTechnologyParameters fileParams = (FileCarrierTechnologyParameters) parameters
165                 .getEventOutputParameters().get("aProducer").getCarrierTechnologyParameters();
166         assertTrue(fileParams.getFileName().endsWith("target/aaa.json"));
167         assertEquals(false, fileParams.isStandardError());
168         assertEquals(false, fileParams.isStandardIo());
169         assertEquals(false, fileParams.isStreamingMode());
170
171     }
172
173     @Test
174     public void testBadFileNameCarrierTechnology() {
175         final String[] args = {"-c", "src/test/resources/parameters/prodConsBadFileName.json"};
176         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
177
178         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
179             .hasMessage("validation error(s) on parameters from "
180                 + "\"src/test/resources/parameters/prodConsBadFileName.json\"\n"
181                 + "parameter group \"APEX_PARAMETERS\" type "
182                 + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, "
183                 + "parameter group has status INVALID\n"
184                 + "  parameter group map \"eventOutputParameters\" INVALID, "
185                 + "parameter group map has status INVALID\n" + "    parameter group \"aProducer\" type "
186                 + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" "
187                 + "INVALID, parameter group has status INVALID\n" + "      parameter group \"FILE\" type "
188                 + "\"org.onap.policy.apex.service.engine.event.impl."
189                 + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, "
190                 + "parameter group has status INVALID\n" + "        field \"fileName\" type "
191                 + "\"java.lang.String\" value \"null\" INVALID, "
192                 + "\"null\" invalid, must be specified as a non-empty string\n");
193     }
194
195     @Test
196     public void testBadEventProtocolParClass() {
197         final String[] args = {"-c", "src/test/resources/parameters/prodConsBadEPParClass.json"};
198         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
199
200         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
201             .hasMessage("error reading parameters from \"src/test/resources/parameters/prodConsBadEPParClass.json\"\n"
202                 + "(ParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" "
203                 + "does not match plugin \"JSON\" in \"org.onap.policy.apex.service.engine.event.impl"
204                 + ".jsonprotocolplugin.JsonEventProtocolParameters"
205                 + "\", specify correct event protocol parameter plugin in parameter \"parameterClassName\"");
206     }
207
208     @Test
209     public void testNoEventProtocolParClass() {
210         final String[] args = {"-c", "src/test/resources/parameters/prodConsNoEPParClass.json"};
211         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
212
213         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
214             .hasMessage("error reading parameters from \"src/test/resources/parameters/prodConsNoEPParClass.json\"\n"
215                 + "(ParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" parameter "
216                 + "\"parameterClassName\" value \"null\" invalid in JSON file");
217     }
218
219     @Test
220     public void testMismatchEventProtocolParClass() {
221         final String[] args = {"-c", "src/test/resources/parameters/prodConsMismatchEPParClass.json"};
222         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
223
224         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
225             .hasMessage("error reading parameters from "
226                 + "\"src/test/resources/parameters/prodConsMismatchEPParClass.json\"\n"
227                 + "(ParameterRuntimeException):event protocol \"SUPER_TOK_BEL\" "
228                 + "does not match plugin \"SUPER_TOK_DEL\" in "
229                 + "\"org.onap.policy.apex.service.engine.parameters.dummyclasses."
230                 + "SuperTokenDelimitedEventProtocolParameters\", "
231                 + "specify correct event protocol parameter plugin in parameter \"parameterClassName\"");
232     }
233
234     @Test
235     public void testWrongTypeEventProtocolParClass() {
236         final String[] args = {"-c", "src/test/resources/parameters/prodConsWrongTypeEPParClass.json"};
237         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
238
239         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
240             .hasMessage("error reading parameters from "
241                 + "\"src/test/resources/parameters/prodConsWrongTypeEPParClass.json\"\n"
242                 + "(ParameterRuntimeException):could not create default parameters for event protocol "
243                 + "\"SUPER_TOK_DEL\"\n" + "class org.onap.policy.apex.service.engine."
244                 + "parameters.dummyclasses.SuperDooperCarrierTechnologyParameters "
245                 + "cannot be cast to class org.onap.policy.apex.service."
246                 + "parameters.eventprotocol.EventProtocolParameters (org.onap.policy.apex.service.engine.parameters"
247                 + ".dummyclasses.SuperDooperCarrierTechnologyParameters and org.onap.policy.apex.service.parameters"
248                 + ".eventprotocol.EventProtocolParameters are in unnamed module of loader 'app')");
249     }
250 }