a324e3da6248e900a1576cad9576e3e190982698
[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 =
40         { "-c", "parameters/NoParameterFile.json" };
41
42         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
43         noArguments.parse(noArgumentString);
44
45         try {
46             new DistributionParameterHandler().getParameters(noArguments);
47             fail("test should throw an exception here");
48         } catch (final Exception e) {
49             assertTrue(e.getMessage().contains("FileNotFoundException"));
50         }
51     }
52
53     @Test
54     public void testParameterHandlerEmptyParameters() throws PolicyDistributionException {
55         final String[] emptyArgumentString =
56         { "-c", "parameters/EmptyParameters.json" };
57
58         final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments();
59         emptyArguments.parse(emptyArgumentString);
60
61         try {
62             new DistributionParameterHandler().getParameters(emptyArguments);
63             fail("test should throw an exception here");
64         } catch (final Exception e) {
65             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
66         }
67     }
68
69     @Test
70     public void testParameterHandlerBadParameters() throws PolicyDistributionException {
71         final String[] badArgumentString =
72         { "-c", "parameters/BadParameters.json" };
73
74         final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments();
75         badArguments.parse(badArgumentString);
76
77         try {
78             new DistributionParameterHandler().getParameters(badArguments);
79             fail("test should throw an exception here");
80         } catch (final Exception e) {
81             assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
82                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
83                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
84         }
85     }
86
87     @Test
88     public void testParameterHandlerInvalidParameters() throws PolicyDistributionException {
89         final String[] invalidArgumentString =
90         { "-c", "parameters/InvalidParameters.json" };
91
92         final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments();
93         invalidArguments.parse(invalidArgumentString);
94
95         try {
96             new DistributionParameterHandler().getParameters(invalidArguments);
97             fail("test should throw an exception here");
98         } catch (final Exception e) {
99             assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
100                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
101                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
102         }
103     }
104
105     @Test
106     public void testParameterHandlerNoParameters() throws PolicyDistributionException {
107         final String[] noArgumentString =
108         { "-c", "parameters/NoParameters.json" };
109
110         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
111         noArguments.parse(noArgumentString);
112
113         try {
114             new DistributionParameterHandler().getParameters(noArguments);
115             fail("test should throw an exception here");
116         } catch (final Exception e) {
117             assertEquals("map parameter \"receptionHandlerParameters\" is null", e.getMessage());
118         }
119     }
120
121     @Test
122     public void testParameterHandlerMinumumParameters() throws PolicyDistributionException {
123         final String[] minArgumentString =
124         { "-c", "parameters/MinimumParameters.json" };
125
126         final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments();
127         minArguments.parse(minArgumentString);
128
129         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments);
130         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
131     }
132
133     @Test
134     public void testDistributionParameterGroup() throws PolicyDistributionException {
135         final String[] distributionConfigParameters =
136         { "-c", "parameters/DistributionConfigParameters.json" };
137
138         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
139         arguments.parse(distributionConfigParameters);
140
141         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments);
142         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
143         assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, parGroup.getReceptionHandlerParameters()
144                 .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType());
145         assertEquals(CommonTestData.DECODER_TYPE,
146                 parGroup.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY)
147                         .getPluginHandlerParameters().getPolicyDecoders().get(CommonTestData.TOSCA_DECODER_KEY)
148                         .getDecoderType());
149         assertEquals(CommonTestData.FORWARDER_TYPE,
150                 parGroup.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY)
151                         .getPluginHandlerParameters().getPolicyForwarders().get(CommonTestData.PAP_ENGINE_FORWARDER_KEY)
152                         .getForwarderType());
153     }
154
155     @Test
156     public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException {
157         final String[] distributionConfigParameters =
158         { "-c", "parameters/DistributionConfigParameters_InvalidName.json" };
159
160         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
161         arguments.parse(distributionConfigParameters);
162
163         try {
164             new DistributionParameterHandler().getParameters(arguments);
165             fail("test should throw an exception here");
166         } catch (final Exception e) {
167             assertTrue(e.getMessage().contains(
168                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
169         }
170     }
171
172     @Test
173     public void testDistributionParameterGroup_InvalidReceptionHandlerType() throws PolicyDistributionException {
174         final String[] distributionConfigParameters =
175         { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerType.json" };
176
177         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
178         arguments.parse(distributionConfigParameters);
179
180         try {
181             new DistributionParameterHandler().getParameters(arguments);
182             fail("test should throw an exception here");
183         } catch (final Exception e) {
184             assertTrue(e.getMessage()
185                     .contains("field \"receptionHandlerType\" type \"java.lang.String\" value \" \" INVALID, "
186                             + "must be a non-blank string"));
187         }
188     }
189
190     @Test
191     public void testDistributionParameterGroup_InvalidPolicyDecoderType() throws PolicyDistributionException {
192         final String[] distributionConfigParameters =
193         { "-c", "parameters/DistributionConfigParameters_InvalidPolicyDecoderType.json" };
194
195         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
196         arguments.parse(distributionConfigParameters);
197
198         try {
199             new DistributionParameterHandler().getParameters(arguments);
200             fail("test should throw an exception here");
201         } catch (final Exception e) {
202             assertTrue(e.getMessage().contains(
203                     "field \"decoderType\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
204         }
205     }
206
207     @Test
208     public void testDistributionParameterGroup_InvalidPolicyForwarderType() throws PolicyDistributionException {
209         final String[] distributionConfigParameters =
210         { "-c", "parameters/DistributionConfigParameters_InvalidPolicyForwarderType.json" };
211
212         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
213         arguments.parse(distributionConfigParameters);
214
215         try {
216             new DistributionParameterHandler().getParameters(arguments);
217             fail("test should throw an exception here");
218         } catch (final Exception e) {
219             assertTrue(e.getMessage().contains("field \"forwarderType\" type \"java.lang.String\" value \" \" INVALID, "
220                     + "must be a non-blank string"));
221         }
222     }
223
224     @Test
225     public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException {
226         final String[] distributionConfigParameters =
227         { "-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json" };
228
229         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
230         arguments.parse(distributionConfigParameters);
231
232         try {
233             new DistributionParameterHandler().getParameters(arguments);
234             fail("test should throw an exception here");
235         } catch (final Exception e) {
236             assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null"));
237         }
238     }
239
240     @Test
241     public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException {
242         final String[] distributionConfigParameters =
243         { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json" };
244
245         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
246         arguments.parse(distributionConfigParameters);
247
248         try {
249             new DistributionParameterHandler().getParameters(arguments);
250             fail("test should throw an exception here");
251         } catch (final Exception e) {
252             assertTrue(e.getMessage().contains("parameter not a regular parameter: receptionHandlerParameters"));
253         }
254     }
255
256     @Test
257     public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException {
258         final String[] distributionConfigParameters =
259         { "-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json" };
260
261         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
262         arguments.parse(distributionConfigParameters);
263
264         try {
265             new DistributionParameterHandler().getParameters(arguments);
266             fail("test should throw an exception here");
267         } catch (final Exception e) {
268             assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null"));
269         }
270     }
271
272     @Test
273     public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException {
274         final String[] distributionConfigParameters =
275         { "-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json" };
276
277         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
278         arguments.parse(distributionConfigParameters);
279
280         try {
281             new DistributionParameterHandler().getParameters(arguments);
282             fail("test should throw an exception here");
283         } catch (final Exception e) {
284             assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null"));
285         }
286     }
287
288     @Test
289     public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException {
290         final String[] distributionConfigParameters =
291         { "-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json" };
292
293         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
294         arguments.parse(distributionConfigParameters);
295
296         try {
297             new DistributionParameterHandler().getParameters(arguments);
298             fail("test should throw an exception here");
299         } catch (final Exception e) {
300             assertTrue(e.getMessage().contains("parameter not a regular parameter: policyDecoders"));
301         }
302     }
303
304     @Test
305     public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException {
306         final String[] distributionConfigParameters =
307         { "-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json" };
308
309         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
310         arguments.parse(distributionConfigParameters);
311
312         try {
313             new DistributionParameterHandler().getParameters(arguments);
314             fail("test should throw an exception here");
315         } catch (final Exception e) {
316             assertTrue(e.getMessage().contains("parameter not a regular parameter: policyForwarders"));
317         }
318     }
319
320     @Test
321     public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException {
322         final String[] distributionConfigParameters =
323         { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" };
324
325         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
326         arguments.parse(distributionConfigParameters);
327
328         try {
329             new DistributionParameterHandler().getParameters(arguments);
330             fail("test should throw an exception here");
331         } catch (final Exception e) {
332             assertTrue(e.getMessage().contains("reception handler class not found in classpath"));
333         }
334     }
335
336     @Test
337     public void testDistributionParameterGroup_InvalidPolicyDecoderClass() throws PolicyDistributionException {
338         final String[] distributionConfigParameters =
339         { "-c", "parameters/DistributionConfigParameters_InvalidPolicyDecoderClass.json" };
340
341         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
342         arguments.parse(distributionConfigParameters);
343
344         try {
345             new DistributionParameterHandler().getParameters(arguments);
346             fail("test should throw an exception here");
347         } catch (final Exception e) {
348             assertTrue(e.getMessage().contains("policy decoder class not found in classpath"));
349         }
350     }
351
352     @Test
353     public void testDistributionParameterGroup_InvalidPolicyForwarderClass() throws PolicyDistributionException {
354         final String[] distributionConfigParameters =
355         { "-c", "parameters/DistributionConfigParameters_InvalidPolicyForwarderClass.json" };
356
357         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
358         arguments.parse(distributionConfigParameters);
359
360         try {
361             new DistributionParameterHandler().getParameters(arguments);
362             fail("test should throw an exception here");
363         } catch (final Exception e) {
364             assertTrue(e.getMessage().contains("policy forwarder class not found in classpath"));
365         }
366     }
367
368     @Test
369     public void testDistributionParameterGroup_InvalidRestServerHost() throws PolicyDistributionException {
370         final String[] distributionConfigParameters =
371         { "-c", "parameters/DistributionConfigParameters_InvalidRestServerHost.json" };
372
373         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
374         arguments.parse(distributionConfigParameters);
375
376         try {
377             new DistributionParameterHandler().getParameters(arguments);
378             fail("test should throw an exception here");
379         } catch (final Exception e) {
380             assertTrue(e.getMessage().contains(
381                     "must be a non-blank string containing hostname/ipaddress of the distribution rest server"));
382         }
383     }
384
385     @Test
386     public void testDistributionParameterGroup_InvalidRestServerPort() throws PolicyDistributionException {
387         final String[] distributionConfigParameters =
388         { "-c", "parameters/DistributionConfigParameters_InvalidRestServerPort.json" };
389
390         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
391         arguments.parse(distributionConfigParameters);
392
393         try {
394             new DistributionParameterHandler().getParameters(arguments);
395             fail("test should throw an exception here");
396         } catch (final Exception e) {
397             assertTrue(e.getMessage()
398                     .contains("must be a positive integer containing port of the distribution rest server"));
399         }
400     }
401
402     @Test
403     public void testDistributionParameterGroup_InvalidRestServerUser() throws PolicyDistributionException {
404         final String[] distributionConfigParameters =
405         { "-c", "parameters/DistributionConfigParameters_InvalidRestServerUser.json" };
406
407         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
408         arguments.parse(distributionConfigParameters);
409
410         try {
411             new DistributionParameterHandler().getParameters(arguments);
412             fail("test should throw an exception here");
413         } catch (final Exception e) {
414             assertTrue(e.getMessage().contains(
415                     "must be a non-blank string containing userName for distribution rest server credentials"));
416         }
417     }
418
419     @Test
420     public void testDistributionParameterGroup_InvalidRestServerPassword() throws PolicyDistributionException {
421         final String[] distributionConfigParameters =
422         { "-c", "parameters/DistributionConfigParameters_InvalidRestServerPassword.json" };
423
424         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
425         arguments.parse(distributionConfigParameters);
426
427         try {
428             new DistributionParameterHandler().getParameters(arguments);
429             fail("test should throw an exception here");
430         } catch (final Exception e) {
431             assertTrue(e.getMessage().contains(
432                     "must be a non-blank string containing password for distribution rest server credentials"));
433         }
434     }
435 }