a5eab8b1ec44f166c520f89fdfcc9b6a837682d5
[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.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     }
163
164     @Test
165     public void testDistributionParameterGroup_InvalidForwarderConfigurationClassName()
166             throws PolicyDistributionException {
167         final String[] distributionConfigParameters =
168         { "-c", "parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.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().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file"));
178         }
179     }
180
181     @Test
182     public void testDistributionParameterGroup_UnknownForwarderConfigurationClassName()
183             throws PolicyDistributionException {
184         final String[] distributionConfigParameters =
185         { "-c", "parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json" };
186
187         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
188         arguments.parse(distributionConfigParameters);
189
190         try {
191             new DistributionParameterHandler().getParameters(arguments);
192             fail("test should throw an exception here");
193         } catch (final Exception e) {
194             assertTrue(e.getMessage().contains(
195                     "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"));
196         }
197     }
198
199     @Test
200     public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException {
201         final String[] distributionConfigParameters =
202         { "-c", "parameters/DistributionConfigParameters_InvalidName.json" };
203
204         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
205         arguments.parse(distributionConfigParameters);
206
207         try {
208             new DistributionParameterHandler().getParameters(arguments);
209             fail("test should throw an exception here");
210         } catch (final Exception e) {
211             assertTrue(e.getMessage().contains(
212                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
213         }
214     }
215
216     @Test
217     public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException {
218         final String[] distributionConfigParameters =
219         { "-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json" };
220
221         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
222         arguments.parse(distributionConfigParameters);
223
224         try {
225             new DistributionParameterHandler().getParameters(arguments);
226             fail("test should throw an exception here");
227         } catch (final Exception e) {
228             assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null"));
229         }
230     }
231
232     @Test
233     public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException {
234         final String[] distributionConfigParameters =
235         { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json" };
236
237         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
238         arguments.parse(distributionConfigParameters);
239
240         try {
241             new DistributionParameterHandler().getParameters(arguments);
242             fail("test should throw an exception here");
243         } catch (final Exception e) {
244             assertTrue(e.getMessage().contains("must have at least one reception handler\n"));
245         }
246     }
247
248     @Test
249     public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException {
250         final String[] distributionConfigParameters =
251         { "-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json" };
252
253         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
254         arguments.parse(distributionConfigParameters);
255
256         try {
257             new DistributionParameterHandler().getParameters(arguments);
258             fail("test should throw an exception here");
259         } catch (final Exception e) {
260             assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null"));
261         }
262     }
263
264     @Test
265     public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException {
266         final String[] distributionConfigParameters =
267         { "-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json" };
268
269         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
270         arguments.parse(distributionConfigParameters);
271
272         try {
273             new DistributionParameterHandler().getParameters(arguments);
274             fail("test should throw an exception here");
275         } catch (final Exception e) {
276             assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null"));
277         }
278     }
279
280     @Test
281     public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException {
282         final String[] distributionConfigParameters =
283         { "-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json" };
284
285         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
286         arguments.parse(distributionConfigParameters);
287
288         try {
289             new DistributionParameterHandler().getParameters(arguments);
290             fail("test should throw an exception here");
291         } catch (final Exception e) {
292             assertTrue(e.getMessage().contains("must have at least one policy decoder\n"));
293         }
294     }
295
296     @Test
297     public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException {
298         final String[] distributionConfigParameters =
299         { "-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json" };
300
301         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
302         arguments.parse(distributionConfigParameters);
303
304         try {
305             new DistributionParameterHandler().getParameters(arguments);
306             fail("test should throw an exception here");
307         } catch (final Exception e) {
308             assertTrue(e.getMessage().endsWith("must have at least one policy forwarder\n"));
309         }
310     }
311
312     @Test
313     public void testDistributionParameterGroup_InvalidReceptionHandlerParameters()
314             throws PolicyDistributionException, IOException {
315         final String[] distributionConfigParameters =
316         { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.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             final String expectedResult = new String(Files.readAllBytes(
326                     Paths.get("src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt")))
327                             .replaceAll("\\s+", "");
328             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
329         }
330     }
331
332     @Test
333     public void testDistributionParameterGroup_InvalidDecoderAndForwarderParameters()
334             throws PolicyDistributionException, IOException {
335         final String[] distributionConfigParameters =
336         { "-c", "parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json" };
337
338         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
339         arguments.parse(distributionConfigParameters);
340
341         try {
342             new DistributionParameterHandler().getParameters(arguments);
343             fail("test should throw an exception here");
344         } catch (final Exception e) {
345             final String expectedResult = new String(Files.readAllBytes(
346                     Paths.get("src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt")))
347                             .replaceAll("\\s+", "");
348             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
349         }
350     }
351
352     @Test
353     public void testDistributionParameterGroup_InvalidRestServerParameters()
354             throws PolicyDistributionException, IOException {
355         final String[] distributionConfigParameters =
356         { "-c", "parameters/DistributionConfigParameters_InvalidRestServerParameters.json" };
357
358         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
359         arguments.parse(distributionConfigParameters);
360
361         try {
362             new DistributionParameterHandler().getParameters(arguments);
363             fail("test should throw an exception here");
364         } catch (final Exception e) {
365             final String expectedResult = new String(Files.readAllBytes(
366                     Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")))
367                             .replaceAll("\\s+", "");
368             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
369         }
370     }
371
372     @Test
373     public void testDistributionVersion() throws PolicyDistributionException {
374         final String[] distributionConfigParameters =
375         { "-v" };
376         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
377         final String version = arguments.parse(distributionConfigParameters);
378         assertTrue(version.startsWith("ONAP Policy Framework Distribution Service"));
379     }
380
381     @Test
382     public void testDistributionHelp() throws PolicyDistributionException {
383         final String[] distributionConfigParameters =
384         { "-h" };
385         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
386         final String help = arguments.parse(distributionConfigParameters);
387         assertTrue(help.startsWith("usage:"));
388     }
389
390     @Test
391     public void testDistributionInvalidOption() throws PolicyDistributionException {
392         final String[] distributionConfigParameters =
393         { "-d" };
394         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
395         try {
396             arguments.parse(distributionConfigParameters);
397         } catch (final Exception exp) {
398             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
399         }
400     }
401
402     @Test
403     public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException {
404         final String[] distributionConfigParameters =
405         { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" };
406
407         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
408         arguments.parse(distributionConfigParameters);
409
410         try {
411             new DistributionParameterHandler().getParameters(arguments);
412         } catch (final Exception e) {
413             assertTrue(e.getMessage().contains("could not find class"));
414         }
415     }
416
417     @Test
418     public void testDistributionParameterGroup_EmptyReceptionHandlerClass() throws PolicyDistributionException {
419         final String[] distributionConfigParameters =
420         { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json" };
421
422         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
423         arguments.parse(distributionConfigParameters);
424
425         try {
426             new DistributionParameterHandler().getParameters(arguments);
427         } catch (final Exception e) {
428             assertTrue(e.getMessage().contains("invalid in JSON file"));
429         }
430     }
431 }