79b1224307b1b4b23f65673be5a2ef4daecddec7
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.main.parameters;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import org.junit.Test;
28 import org.onap.policy.distribution.main.PolicyDistributionException;
29 import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments;
30
31 /**
32  * Class to perform unit test of DistributionParameterHandler.
33  *
34  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
35  */
36 public class TestDistributionParameterHandler {
37     @Test
38     public void testParameterHandlerNoParameterFile() throws PolicyDistributionException {
39         final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
40
41         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
42         noArguments.parse(noArgumentString);
43
44         try {
45             new DistributionParameterHandler().getParameters(noArguments);
46             fail("test should throw an exception here");
47         } catch (final Exception e) {
48             assertTrue(e.getMessage().contains("FileNotFoundException"));
49         }
50     }
51
52     @Test
53     public void testParameterHandlerEmptyParameters() throws PolicyDistributionException {
54         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
55
56         final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments();
57         emptyArguments.parse(emptyArgumentString);
58
59         try {
60             new DistributionParameterHandler().getParameters(emptyArguments);
61             fail("test should throw an exception here");
62         } catch (final Exception e) {
63             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
64         }
65     }
66
67     @Test
68     public void testParameterHandlerBadParameters() throws PolicyDistributionException {
69         final String[] badArgumentString = { "-c", "parameters/BadParameters.json" };
70
71         final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments();
72         badArguments.parse(badArgumentString);
73
74         try {
75             new DistributionParameterHandler().getParameters(badArguments);
76             fail("test should throw an exception here");
77         } catch (final Exception e) {
78             assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
79                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
80                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
81         }
82     }
83
84     @Test
85     public void testParameterHandlerInvalidParameters() throws PolicyDistributionException {
86         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
87
88         final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments();
89         invalidArguments.parse(invalidArgumentString);
90
91         try {
92             new DistributionParameterHandler().getParameters(invalidArguments);
93             fail("test should throw an exception here");
94         } catch (final Exception e) {
95             assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
96                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
97                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
98         }
99     }
100
101     @Test
102     public void testParameterHandlerNoParameters() throws PolicyDistributionException {
103         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
104
105         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
106         noArguments.parse(noArgumentString);
107
108         try {
109             new DistributionParameterHandler().getParameters(noArguments);
110             fail("test should throw an exception here");
111         } catch (final Exception e) {
112             assertEquals("map parameter \"receptionHandlerParameters\" is null", e.getMessage());
113         }
114     }
115
116     @Test
117     public void testParameterHandlerMinumumParameters() throws PolicyDistributionException {
118         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
119
120         final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments();
121         minArguments.parse(minArgumentString);
122
123         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments);
124         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
125     }
126
127     @Test
128     public void testDistributionParameterGroup() throws PolicyDistributionException {
129         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" };
130
131         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
132         arguments.parse(distributionConfigParameters);
133
134         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments);
135         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
136         assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, parGroup.getReceptionHandlerParameters()
137                 .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType());
138         assertEquals(CommonTestData.DECODER_TYPE,
139                 parGroup.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY)
140                         .getPluginHandlerParameters().getPolicyDecoders().get(CommonTestData.TOSCA_DECODER_KEY)
141                         .getDecoderType());
142         assertEquals(CommonTestData.FORWARDER_TYPE,
143                 parGroup.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY)
144                         .getPluginHandlerParameters().getPolicyForwarders().get(CommonTestData.PAP_ENGINE_FORWARDER_KEY)
145                         .getForwarderType());
146     }
147
148     @Test
149     public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException {
150         final String[] distributionConfigParameters =
151                 { "-c", "parameters/DistributionConfigParameters_InvalidName.json" };
152
153         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
154         arguments.parse(distributionConfigParameters);
155
156         try {
157             new DistributionParameterHandler().getParameters(arguments);
158             fail("test should throw an exception here");
159         } catch (final Exception e) {
160             assertTrue(e.getMessage().contains(
161                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
162         }
163     }
164
165     @Test
166     public void testDistributionParameterGroup_InvalidReceptionHandlerType() throws PolicyDistributionException {
167         final String[] distributionConfigParameters =
168                 { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerType.json" };
169
170         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
171         arguments.parse(distributionConfigParameters);
172
173         try {
174             new DistributionParameterHandler().getParameters(arguments);
175             fail("test should throw an exception here");
176         } catch (final Exception e) {
177             assertTrue(e.getMessage()
178                     .contains("field \"receptionHandlerType\" type \"java.lang.String\" value \" \" INVALID, "
179                             + "must be a non-blank string"));
180         }
181     }
182
183     @Test
184     public void testDistributionParameterGroup_InvalidPolicyDecoderType() throws PolicyDistributionException {
185         final String[] distributionConfigParameters =
186                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyDecoderType.json" };
187
188         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
189         arguments.parse(distributionConfigParameters);
190
191         try {
192             new DistributionParameterHandler().getParameters(arguments);
193             fail("test should throw an exception here");
194         } catch (final Exception e) {
195             assertTrue(e.getMessage().contains(
196                     "field \"decoderType\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
197         }
198     }
199
200     @Test
201     public void testDistributionParameterGroup_InvalidPolicyForwarderType() throws PolicyDistributionException {
202         final String[] distributionConfigParameters =
203                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyForwarderType.json" };
204
205         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
206         arguments.parse(distributionConfigParameters);
207
208         try {
209             new DistributionParameterHandler().getParameters(arguments);
210             fail("test should throw an exception here");
211         } catch (final Exception e) {
212             assertTrue(e.getMessage().contains("field \"forwarderType\" type \"java.lang.String\" value \" \" INVALID, "
213                     + "must be a non-blank string"));
214         }
215     }
216
217     @Test
218     public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException {
219         final String[] distributionConfigParameters =
220                 { "-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json" };
221
222         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
223         arguments.parse(distributionConfigParameters);
224
225         try {
226             new DistributionParameterHandler().getParameters(arguments);
227             fail("test should throw an exception here");
228         } catch (final Exception e) {
229             assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null"));
230         }
231     }
232
233     @Test
234     public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException {
235         final String[] distributionConfigParameters =
236                 { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json" };
237
238         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
239         arguments.parse(distributionConfigParameters);
240
241         try {
242             new DistributionParameterHandler().getParameters(arguments);
243             fail("test should throw an exception here");
244         } catch (final Exception e) {
245             assertTrue(e.getMessage().contains("parameter not a regular parameter: receptionHandlerParameters"));
246         }
247     }
248
249     @Test
250     public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException {
251         final String[] distributionConfigParameters =
252                 { "-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json" };
253
254         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
255         arguments.parse(distributionConfigParameters);
256
257         try {
258             new DistributionParameterHandler().getParameters(arguments);
259             fail("test should throw an exception here");
260         } catch (final Exception e) {
261             assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null"));
262         }
263     }
264
265     @Test
266     public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException {
267         final String[] distributionConfigParameters =
268                 { "-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json" };
269
270         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
271         arguments.parse(distributionConfigParameters);
272
273         try {
274             new DistributionParameterHandler().getParameters(arguments);
275             fail("test should throw an exception here");
276         } catch (final Exception e) {
277             assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null"));
278         }
279     }
280
281     @Test
282     public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException {
283         final String[] distributionConfigParameters =
284                 { "-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json" };
285
286         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
287         arguments.parse(distributionConfigParameters);
288
289         try {
290             new DistributionParameterHandler().getParameters(arguments);
291             fail("test should throw an exception here");
292         } catch (final Exception e) {
293             assertTrue(e.getMessage().contains("parameter not a regular parameter: policyDecoders"));
294         }
295     }
296
297     @Test
298     public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException {
299         final String[] distributionConfigParameters =
300                 { "-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json" };
301
302         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
303         arguments.parse(distributionConfigParameters);
304
305         try {
306             new DistributionParameterHandler().getParameters(arguments);
307             fail("test should throw an exception here");
308         } catch (final Exception e) {
309             assertTrue(e.getMessage().contains("parameter not a regular parameter: policyForwarders"));
310         }
311     }
312
313     @Test
314     public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException {
315         final String[] distributionConfigParameters =
316                 { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" };
317
318         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
319         arguments.parse(distributionConfigParameters);
320
321         try {
322             new DistributionParameterHandler().getParameters(arguments);
323             fail("test should throw an exception here");
324         } catch (final Exception e) {
325             assertTrue(e.getMessage().contains("reception handler class not found in classpath"));
326         }
327     }
328
329     @Test
330     public void testDistributionParameterGroup_InvalidPolicyDecoderClass() throws PolicyDistributionException {
331         final String[] distributionConfigParameters =
332                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyDecoderClass.json" };
333
334         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
335         arguments.parse(distributionConfigParameters);
336
337         try {
338             new DistributionParameterHandler().getParameters(arguments);
339             fail("test should throw an exception here");
340         } catch (final Exception e) {
341             assertTrue(e.getMessage().contains("policy decoder class not found in classpath"));
342         }
343     }
344
345     @Test
346     public void testDistributionParameterGroup_InvalidPolicyForwarderClass() throws PolicyDistributionException {
347         final String[] distributionConfigParameters =
348                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyForwarderClass.json" };
349
350         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
351         arguments.parse(distributionConfigParameters);
352
353         try {
354             new DistributionParameterHandler().getParameters(arguments);
355             fail("test should throw an exception here");
356         } catch (final Exception e) {
357             assertTrue(e.getMessage().contains("policy forwarder class not found in classpath"));
358         }
359     }
360 }