b66da8e6d65d49117b15a876dfb84a66bd5fced6
[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 java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30
31 import org.junit.Test;
32 import org.onap.policy.distribution.main.PolicyDistributionException;
33 import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments;
34 import org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup;
35 import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup;
36
37 /**
38  * Class to perform unit test of DistributionParameterHandler.
39  *
40  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
41  */
42 public class TestDistributionParameterHandler {
43     @Test
44     public void testParameterHandlerNoParameterFile() throws PolicyDistributionException {
45         final String[] noArgumentString =
46             { "-c", "parameters/NoParameterFile.json" };
47
48         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
49         noArguments.parse(noArgumentString);
50
51         try {
52             new DistributionParameterHandler().getParameters(noArguments);
53             fail("test should throw an exception here");
54         } catch (final Exception e) {
55             assertTrue(e.getMessage().contains("FileNotFoundException"));
56         }
57     }
58
59     @Test
60     public void testParameterHandlerEmptyParameters() throws PolicyDistributionException {
61         final String[] emptyArgumentString =
62             { "-c", "parameters/EmptyParameters.json" };
63
64         final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments();
65         emptyArguments.parse(emptyArgumentString);
66
67         try {
68             new DistributionParameterHandler().getParameters(emptyArguments);
69             fail("test should throw an exception here");
70         } catch (final Exception e) {
71             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
72         }
73     }
74
75     @Test
76     public void testParameterHandlerBadParameters() throws PolicyDistributionException {
77         final String[] badArgumentString =
78             { "-c", "parameters/BadParameters.json" };
79
80         final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments();
81         badArguments.parse(badArgumentString);
82
83         try {
84             new DistributionParameterHandler().getParameters(badArguments);
85             fail("test should throw an exception here");
86         } catch (final Exception e) {
87             assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
88                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
89                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
90         }
91     }
92
93     @Test
94     public void testParameterHandlerInvalidParameters() throws PolicyDistributionException {
95         final String[] invalidArgumentString =
96             { "-c", "parameters/InvalidParameters.json" };
97
98         final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments();
99         invalidArguments.parse(invalidArgumentString);
100
101         try {
102             new DistributionParameterHandler().getParameters(invalidArguments);
103             fail("test should throw an exception here");
104         } catch (final Exception e) {
105             assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
106                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
107                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
108         }
109     }
110
111     @Test
112     public void testParameterHandlerNoParameters() throws PolicyDistributionException {
113         final String[] noArgumentString =
114             { "-c", "parameters/NoParameters.json" };
115
116         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
117         noArguments.parse(noArgumentString);
118
119         try {
120             new DistributionParameterHandler().getParameters(noArguments);
121             fail("test should throw an exception here");
122         } catch (final Exception e) {
123             assertEquals("map parameter \"receptionHandlerParameters\" is null", e.getMessage());
124         }
125     }
126
127     @Test
128     public void testParameterHandlerMinumumParameters() throws PolicyDistributionException {
129         final String[] minArgumentString =
130             { "-c", "parameters/MinimumParameters.json" };
131
132         final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments();
133         minArguments.parse(minArgumentString);
134
135         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments);
136         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
137     }
138
139     @Test
140     public void testDistributionParameterGroup() throws PolicyDistributionException {
141         final String[] distributionConfigParameters =
142             { "-c", "parameters/DistributionConfigParameters.json" };
143
144         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
145         arguments.parse(distributionConfigParameters);
146
147         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments);
148         assertTrue(arguments.checkSetConfigurationFilePath());
149         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
150         assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, parGroup.getReceptionHandlerParameters()
151                 .get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY).getReceptionHandlerType());
152         assertEquals(CommonTestData.DECODER_TYPE,
153                 parGroup.getReceptionHandlerParameters().get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY)
154                         .getPluginHandlerParameters().getPolicyDecoders().get(CommonTestData.DUMMY_DECODER_KEY)
155                         .getDecoderType());
156         assertEquals(CommonTestData.FORWARDER_TYPE,
157                 parGroup.getReceptionHandlerParameters().get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY)
158                         .getPluginHandlerParameters().getPolicyForwarders()
159                         .get(CommonTestData.DUMMY_ENGINE_FORWARDER_KEY).getForwarderType());
160         assertEquals(CommonTestData.FORWARDER_HOST,
161                 ((DummyPolicyForwarderParameterGroup) parGroup.getPolicyForwarderConfigurationParameters()
162                         .get(CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS)).getHostname());
163         assertEquals(CommonTestData.POLICY_TYPE,
164                 ((DummyPolicyDecoderParameterGroup) parGroup.getPolicyDecoderConfigurationParameters()
165                         .get(CommonTestData.DECODER_CONFIGURATION_PARAMETERS)).getPolicyType());
166         assertEquals(CommonTestData.POLICY_NAME,
167                 ((DummyPolicyDecoderParameterGroup) parGroup.getPolicyDecoderConfigurationParameters()
168                         .get(CommonTestData.DECODER_CONFIGURATION_PARAMETERS)).getPolicyName());
169     }
170
171     @Test
172     public void testDistributionParameterGroup_InvalidForwarderConfigurationClassName()
173             throws PolicyDistributionException {
174         final String[] distributionConfigParameters =
175             { "-c", "parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.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().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file"));
185         }
186     }
187
188     @Test
189     public void testDistributionParameterGroup_UnknownForwarderConfigurationClassName()
190             throws PolicyDistributionException {
191         final String[] distributionConfigParameters =
192             { "-c", "parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json" };
193
194         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
195         arguments.parse(distributionConfigParameters);
196
197         try {
198             new DistributionParameterHandler().getParameters(arguments);
199             fail("test should throw an exception here");
200         } catch (final Exception e) {
201             assertTrue(e.getMessage().contains(
202                     "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"));
203         }
204     }
205
206     @Test
207     public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException {
208         final String[] distributionConfigParameters =
209             { "-c", "parameters/DistributionConfigParameters_InvalidName.json" };
210
211         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
212         arguments.parse(distributionConfigParameters);
213
214         try {
215             new DistributionParameterHandler().getParameters(arguments);
216             fail("test should throw an exception here");
217         } catch (final Exception e) {
218             assertTrue(e.getMessage().contains(
219                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
220         }
221     }
222
223     @Test
224     public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException {
225         final String[] distributionConfigParameters =
226             { "-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json" };
227
228         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
229         arguments.parse(distributionConfigParameters);
230
231         try {
232             new DistributionParameterHandler().getParameters(arguments);
233             fail("test should throw an exception here");
234         } catch (final Exception e) {
235             assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null"));
236         }
237     }
238
239     @Test
240     public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException {
241         final String[] distributionConfigParameters =
242             { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json" };
243
244         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
245         arguments.parse(distributionConfigParameters);
246
247         try {
248             new DistributionParameterHandler().getParameters(arguments);
249             fail("test should throw an exception here");
250         } catch (final Exception e) {
251             assertTrue(e.getMessage().contains("must have at least one reception handler\n"));
252         }
253     }
254
255     @Test
256     public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException {
257         final String[] distributionConfigParameters =
258             { "-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json" };
259
260         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
261         arguments.parse(distributionConfigParameters);
262
263         try {
264             new DistributionParameterHandler().getParameters(arguments);
265             fail("test should throw an exception here");
266         } catch (final Exception e) {
267             assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null"));
268         }
269     }
270
271     @Test
272     public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException {
273         final String[] distributionConfigParameters =
274             { "-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json" };
275
276         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
277         arguments.parse(distributionConfigParameters);
278
279         try {
280             new DistributionParameterHandler().getParameters(arguments);
281             fail("test should throw an exception here");
282         } catch (final Exception e) {
283             assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null"));
284         }
285     }
286
287     @Test
288     public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException {
289         final String[] distributionConfigParameters =
290             { "-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json" };
291
292         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
293         arguments.parse(distributionConfigParameters);
294
295         try {
296             new DistributionParameterHandler().getParameters(arguments);
297             fail("test should throw an exception here");
298         } catch (final Exception e) {
299             assertTrue(e.getMessage().contains("must have at least one policy decoder\n"));
300         }
301     }
302
303     @Test
304     public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException {
305         final String[] distributionConfigParameters =
306             { "-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json" };
307
308         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
309         arguments.parse(distributionConfigParameters);
310
311         try {
312             new DistributionParameterHandler().getParameters(arguments);
313             fail("test should throw an exception here");
314         } catch (final Exception e) {
315             assertTrue(e.getMessage().endsWith("must have at least one policy forwarder\n"));
316         }
317     }
318
319     @Test
320     public void testDistributionParameterGroup_InvalidReceptionHandlerParameters()
321             throws PolicyDistributionException, IOException {
322         final String[] distributionConfigParameters =
323             { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.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             final String expectedResult = new String(Files.readAllBytes(
333                     Paths.get("src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt")))
334                             .replaceAll("\\s+", "");
335             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
336         }
337     }
338
339     @Test
340     public void testDistributionParameterGroup_InvalidDecoderAndForwarderParameters()
341             throws PolicyDistributionException, IOException {
342         final String[] distributionConfigParameters =
343             { "-c", "parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json" };
344
345         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
346         arguments.parse(distributionConfigParameters);
347
348         try {
349             new DistributionParameterHandler().getParameters(arguments);
350             fail("test should throw an exception here");
351         } catch (final Exception e) {
352             final String expectedResult = new String(Files.readAllBytes(
353                     Paths.get("src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt")))
354                             .replaceAll("\\s+", "");
355             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
356         }
357     }
358
359     @Test
360     public void testDistributionParameterGroup_InvalidRestServerParameters()
361             throws PolicyDistributionException, IOException {
362         final String[] distributionConfigParameters =
363             { "-c", "parameters/DistributionConfigParameters_InvalidRestServerParameters.json" };
364
365         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
366         arguments.parse(distributionConfigParameters);
367
368         try {
369             new DistributionParameterHandler().getParameters(arguments);
370             fail("test should throw an exception here");
371         } catch (final Exception e) {
372             final String expectedResult = new String(Files.readAllBytes(
373                     Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")))
374                             .replaceAll("\\s+", "");
375             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
376         }
377     }
378
379     @Test
380     public void testDistributionVersion() throws PolicyDistributionException {
381         final String[] distributionConfigParameters =
382             { "-v" };
383         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
384         final String version = arguments.parse(distributionConfigParameters);
385         assertTrue(version.startsWith("ONAP Policy Framework Distribution Service"));
386     }
387
388     @Test
389     public void testDistributionHelp() throws PolicyDistributionException {
390         final String[] distributionConfigParameters =
391             { "-h" };
392         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
393         final String help = arguments.parse(distributionConfigParameters);
394         assertTrue(help.startsWith("usage:"));
395     }
396
397     @Test
398     public void testDistributionInvalidOption() throws PolicyDistributionException {
399         final String[] distributionConfigParameters =
400             { "-d" };
401         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
402         try {
403             arguments.parse(distributionConfigParameters);
404         } catch (final Exception exp) {
405             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
406         }
407     }
408
409     @Test
410     public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException {
411         final String[] distributionConfigParameters =
412             { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" };
413
414         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
415         arguments.parse(distributionConfigParameters);
416
417         try {
418             new DistributionParameterHandler().getParameters(arguments);
419         } catch (final Exception e) {
420             assertTrue(e.getMessage().contains("could not find class"));
421         }
422     }
423
424     @Test
425     public void testDistributionParameterGroup_EmptyReceptionHandlerClass() throws PolicyDistributionException {
426         final String[] distributionConfigParameters =
427             { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json" };
428
429         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
430         arguments.parse(distributionConfigParameters);
431
432         try {
433             new DistributionParameterHandler().getParameters(arguments);
434         } catch (final Exception e) {
435             assertTrue(e.getMessage().contains("invalid in JSON file"));
436         }
437     }
438
439     @Test
440     public void testDistributionParameterGroup_InvalidDecoderConfigurationClassName()
441             throws PolicyDistributionException {
442         final String[] distributionConfigParameters =
443             { "-c", "parameters/DistributionConfigParameters_InvalidDecoderConfigurationClassName.json" };
444
445         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
446         arguments.parse(distributionConfigParameters);
447
448         try {
449             new DistributionParameterHandler().getParameters(arguments);
450             fail("test should throw an exception here");
451         } catch (final Exception e) {
452             assertTrue(e.getMessage().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file"));
453         }
454     }
455
456     @Test
457     public void testDistributionParameterGroup_UnknownDecoderConfigurationClassName()
458             throws PolicyDistributionException {
459         final String[] distributionConfigParameters =
460             { "-c", "parameters/DistributionConfigParameters_UnknownDecoderConfigurationClassName.json" };
461
462         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
463         arguments.parse(distributionConfigParameters);
464
465         try {
466             new DistributionParameterHandler().getParameters(arguments);
467             fail("test should throw an exception here");
468         } catch (final Exception e) {
469             assertTrue(e.getMessage().contains(
470                     "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"));
471         }
472     }
473 }