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