14d1a105205648a5bb4a7b8a135c2dbd7a3e2779
[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.DummyPolicyForwarderParameterGroup;
34
35 /**
36  * Class to perform unit test of DistributionParameterHandler.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
39  */
40 public class TestDistributionParameterHandler {
41     @Test
42     public void testParameterHandlerNoParameterFile() throws PolicyDistributionException {
43         final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
44
45         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
46         noArguments.parse(noArgumentString);
47
48         try {
49             new DistributionParameterHandler().getParameters(noArguments);
50             fail("test should throw an exception here");
51         } catch (final Exception e) {
52             assertTrue(e.getMessage().contains("FileNotFoundException"));
53         }
54     }
55
56     @Test
57     public void testParameterHandlerEmptyParameters() throws PolicyDistributionException {
58         final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
59
60         final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments();
61         emptyArguments.parse(emptyArgumentString);
62
63         try {
64             new DistributionParameterHandler().getParameters(emptyArguments);
65             fail("test should throw an exception here");
66         } catch (final Exception e) {
67             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
68         }
69     }
70
71     @Test
72     public void testParameterHandlerBadParameters() throws PolicyDistributionException {
73         final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
74
75         final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments();
76         badArguments.parse(badArgumentString);
77
78         try {
79             new DistributionParameterHandler().getParameters(badArguments);
80             fail("test should throw an exception here");
81         } catch (final Exception e) {
82             assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
83                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
84                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
85         }
86     }
87
88     @Test
89     public void testParameterHandlerInvalidParameters() throws PolicyDistributionException {
90         final String[] invalidArgumentString = {"-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 = {"-c", "parameters/NoParameters.json"};
108
109         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
110         noArguments.parse(noArgumentString);
111
112         try {
113             new DistributionParameterHandler().getParameters(noArguments);
114             fail("test should throw an exception here");
115         } catch (final Exception e) {
116             assertEquals("map parameter \"receptionHandlerParameters\" is null", e.getMessage());
117         }
118     }
119
120     @Test
121     public void testParameterHandlerMinumumParameters() throws PolicyDistributionException {
122         final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
123
124         final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments();
125         minArguments.parse(minArgumentString);
126
127         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments);
128         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
129     }
130
131     @Test
132     public void testDistributionParameterGroup() throws PolicyDistributionException {
133         final String[] distributionConfigParameters = {"-c", "parameters/DistributionConfigParameters.json"};
134
135         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
136         arguments.parse(distributionConfigParameters);
137
138         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments);
139         assertTrue(arguments.checkSetConfigurationFilePath());
140         assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName());
141         assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, parGroup.getReceptionHandlerParameters()
142                 .get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY).getReceptionHandlerType());
143         assertEquals(CommonTestData.DECODER_TYPE,
144                 parGroup.getReceptionHandlerParameters().get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY)
145                         .getPluginHandlerParameters().getPolicyDecoders().get(CommonTestData.DUMMY_DECODER_KEY)
146                         .getDecoderType());
147         assertEquals(CommonTestData.FORWARDER_TYPE,
148                 parGroup.getReceptionHandlerParameters().get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY)
149                         .getPluginHandlerParameters().getPolicyForwarders()
150                         .get(CommonTestData.DUMMY_ENGINE_FORWARDER_KEY).getForwarderType());
151         assertEquals(CommonTestData.FORWARDER_HOST,
152                 ((DummyPolicyForwarderParameterGroup) parGroup.getPolicyForwarderConfigurationParameters()
153                         .get(CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS)).getHostname());
154     }
155
156     @Test
157     public void testDistributionParameterGroup_InvalidForwarderConfigurationClassName()
158             throws PolicyDistributionException {
159         final String[] distributionConfigParameters =
160             {"-c", "parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json"};
161
162         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
163         arguments.parse(distributionConfigParameters);
164
165         try {
166             new DistributionParameterHandler().getParameters(arguments);
167             fail("test should throw an exception here");
168         } catch (final Exception e) {
169             assertTrue(e.getMessage().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file"));
170         }
171     }
172
173     @Test
174     public void testDistributionParameterGroup_UnknownForwarderConfigurationClassName()
175             throws PolicyDistributionException {
176         final String[] distributionConfigParameters =
177             {"-c", "parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json"};
178
179         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
180         arguments.parse(distributionConfigParameters);
181
182         try {
183             new DistributionParameterHandler().getParameters(arguments);
184             fail("test should throw an exception here");
185         } catch (final Exception e) {
186             assertTrue(e.getMessage().contains(
187                     "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"));
188         }
189     }
190
191     @Test
192     public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException {
193         final String[] distributionConfigParameters =
194             {"-c", "parameters/DistributionConfigParameters_InvalidName.json"};
195
196         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
197         arguments.parse(distributionConfigParameters);
198
199         try {
200             new DistributionParameterHandler().getParameters(arguments);
201             fail("test should throw an exception here");
202         } catch (final Exception e) {
203             assertTrue(e.getMessage().contains(
204                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
205         }
206     }
207
208     @Test
209     public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException {
210         final String[] distributionConfigParameters =
211             {"-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json"};
212
213         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
214         arguments.parse(distributionConfigParameters);
215
216         try {
217             new DistributionParameterHandler().getParameters(arguments);
218             fail("test should throw an exception here");
219         } catch (final Exception e) {
220             assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null"));
221         }
222     }
223
224     @Test
225     public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException {
226         final String[] distributionConfigParameters =
227             {"-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.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("must have at least one reception handler\n"));
237         }
238     }
239
240     @Test
241     public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException {
242         final String[] distributionConfigParameters =
243             {"-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.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("map parameter \"policyDecoders\" is null"));
253         }
254     }
255
256     @Test
257     public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException {
258         final String[] distributionConfigParameters =
259             {"-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.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 \"policyForwarders\" is null"));
269         }
270     }
271
272     @Test
273     public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException {
274         final String[] distributionConfigParameters =
275             {"-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.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("must have at least one policy decoder\n"));
285         }
286     }
287
288     @Test
289     public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException {
290         final String[] distributionConfigParameters =
291             {"-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.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().endsWith("must have at least one policy forwarder\n"));
301         }
302     }
303
304     @Test
305     public void testDistributionParameterGroup_InvalidReceptionHandlerParameters()
306             throws PolicyDistributionException, IOException {
307         final String[] distributionConfigParameters =
308             {"-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json"};
309
310         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
311         arguments.parse(distributionConfigParameters);
312
313         try {
314             new DistributionParameterHandler().getParameters(arguments);
315             fail("test should throw an exception here");
316         } catch (final Exception e) {
317             final String expectedResult = new String(Files.readAllBytes(
318                     Paths.get("src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt")))
319                             .replaceAll("\\s+", "");
320             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
321         }
322     }
323
324     @Test
325     public void testDistributionParameterGroup_InvalidDecoderAndForwarderParameters()
326             throws PolicyDistributionException, IOException {
327         final String[] distributionConfigParameters =
328             {"-c", "parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json"};
329
330         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
331         arguments.parse(distributionConfigParameters);
332
333         try {
334             new DistributionParameterHandler().getParameters(arguments);
335             fail("test should throw an exception here");
336         } catch (final Exception e) {
337             final String expectedResult = new String(Files.readAllBytes(
338                     Paths.get("src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt")))
339                             .replaceAll("\\s+", "");
340             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
341         }
342     }
343
344     @Test
345     public void testDistributionParameterGroup_InvalidRestServerParameters()
346             throws PolicyDistributionException, IOException {
347         final String[] distributionConfigParameters =
348             {"-c", "parameters/DistributionConfigParameters_InvalidRestServerParameters.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             final String expectedResult = new String(Files.readAllBytes(
358                     Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")))
359                             .replaceAll("\\s+", "");
360             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
361         }
362     }
363
364     @Test
365     public void testDistributionVersion() throws PolicyDistributionException {
366         final String[] distributionConfigParameters = {"-v"};
367         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
368         final String version = arguments.parse(distributionConfigParameters);
369         assertTrue(version.startsWith("ONAP Policy Framework Distribution Service"));
370     }
371
372     @Test
373     public void testDistributionHelp() throws PolicyDistributionException {
374         final String[] distributionConfigParameters = {"-h"};
375         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
376         final String help = arguments.parse(distributionConfigParameters);
377         assertTrue(help.startsWith("usage:"));
378     }
379
380     @Test
381     public void testDistributionInvalidOption() throws PolicyDistributionException {
382         final String[] distributionConfigParameters = {"-d"};
383         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
384         try {
385             arguments.parse(distributionConfigParameters);
386         } catch (final Exception exp) {
387             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
388         }
389     }
390 }