57610b23ea27ff5392f77dde0b29514ac610c3e9
[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 = { "-c", "parameters/NoParameterFile.json" };
40
41         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
42         noArguments.parse(noArgumentString);
43
44         try {
45             new DistributionParameterHandler().getParameters(noArguments);
46             fail("test should throw an exception here");
47         } catch (final Exception e) {
48             assertTrue(e.getMessage().contains("FileNotFoundException"));
49         }
50     }
51
52     @Test
53     public void testParameterHandlerEmptyParameters() throws PolicyDistributionException {
54         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
55
56         final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments();
57         emptyArguments.parse(emptyArgumentString);
58
59         try {
60             new DistributionParameterHandler().getParameters(emptyArguments);
61             fail("test should throw an exception here");
62         } catch (final Exception e) {
63             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
64         }
65     }
66
67     @Test
68     public void testParameterHandlerBadParameters() throws PolicyDistributionException {
69         final String[] badArgumentString = { "-c", "parameters/BadParameters.json" };
70
71         final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments();
72         badArguments.parse(badArgumentString);
73
74         try {
75             new DistributionParameterHandler().getParameters(badArguments);
76             fail("test should throw an exception here");
77         } catch (final Exception e) {
78             assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
79                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
80                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
81         }
82     }
83
84     @Test
85     public void testParameterHandlerInvalidParameters() throws PolicyDistributionException {
86         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
87
88         final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments();
89         invalidArguments.parse(invalidArgumentString);
90
91         try {
92             new DistributionParameterHandler().getParameters(invalidArguments);
93             fail("test should throw an exception here");
94         } catch (final Exception e) {
95             assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
96                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
97                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
98         }
99     }
100
101     @Test
102     public void testParameterHandlerNoParameters() throws PolicyDistributionException {
103         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
104
105         final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments();
106         noArguments.parse(noArgumentString);
107
108         try {
109             new DistributionParameterHandler().getParameters(noArguments);
110             fail("test should throw an exception here");
111         } catch (final Exception e) {
112             assertEquals("map parameter \"receptionHandlerParameters\" is null", e.getMessage());
113         }
114     }
115
116     @Test
117     public void testParameterHandlerMinumumParameters() throws PolicyDistributionException {
118         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
119
120         final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments();
121         minArguments.parse(minArgumentString);
122
123         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments);
124         assertEquals("SDCDistributionGroup", parGroup.getName());
125     }
126
127     @Test
128     public void testDistributionParameterGroup() throws PolicyDistributionException {
129         final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" };
130
131         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
132         arguments.parse(distributionConfigParameters);
133
134         final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments);
135         assertEquals("SDCDistributionGroup", parGroup.getName());
136         assertEquals("SDC",
137                 parGroup.getReceptionHandlerParameters().get("SDCReceptionHandler").getReceptionHandlerType());
138         assertEquals("TOSCA", parGroup.getReceptionHandlerParameters().get("SDCReceptionHandler")
139                 .getPluginHandlerParameters().getPolicyDecoders().get("TOSCADecoder").getDecoderType());
140         assertEquals("PAPEngine", parGroup.getReceptionHandlerParameters().get("SDCReceptionHandler")
141                 .getPluginHandlerParameters().getPolicyForwarders().get("PAPEngineForwarder").getForwarderType());
142     }
143
144     @Test
145     public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException {
146         final String[] distributionConfigParameters =
147                 { "-c", "parameters/DistributionConfigParameters_InvalidName.json" };
148
149         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
150         arguments.parse(distributionConfigParameters);
151
152         try {
153             new DistributionParameterHandler().getParameters(arguments);
154             fail("test should throw an exception here");
155         } catch (final Exception e) {
156             assertTrue(e.getMessage().contains(
157                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
158         }
159     }
160
161     @Test
162     public void testDistributionParameterGroup_InvalidReceptionHandlerType() throws PolicyDistributionException {
163         final String[] distributionConfigParameters =
164                 { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerType.json" };
165
166         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
167         arguments.parse(distributionConfigParameters);
168
169         try {
170             new DistributionParameterHandler().getParameters(arguments);
171             fail("test should throw an exception here");
172         } catch (final Exception e) {
173             assertTrue(e.getMessage()
174                     .contains("field \"receptionHandlerType\" type \"java.lang.String\" value \" \" INVALID, "
175                             + "must be a non-blank string"));
176         }
177     }
178
179     @Test
180     public void testDistributionParameterGroup_InvalidPolicyDecoderType() throws PolicyDistributionException {
181         final String[] distributionConfigParameters =
182                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyDecoderType.json" };
183
184         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
185         arguments.parse(distributionConfigParameters);
186
187         try {
188             new DistributionParameterHandler().getParameters(arguments);
189             fail("test should throw an exception here");
190         } catch (final Exception e) {
191             assertTrue(e.getMessage().contains(
192                     "field \"decoderType\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
193         }
194     }
195
196     @Test
197     public void testDistributionParameterGroup_InvalidPolicyForwarderType() throws PolicyDistributionException {
198         final String[] distributionConfigParameters =
199                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyForwarderType.json" };
200
201         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
202         arguments.parse(distributionConfigParameters);
203
204         try {
205             new DistributionParameterHandler().getParameters(arguments);
206             fail("test should throw an exception here");
207         } catch (final Exception e) {
208             assertTrue(e.getMessage().contains("field \"forwarderType\" type \"java.lang.String\" value \" \" INVALID, "
209                     + "must be a non-blank string"));
210         }
211     }
212
213     @Test
214     public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException {
215         final String[] distributionConfigParameters =
216                 { "-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json" };
217
218         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
219         arguments.parse(distributionConfigParameters);
220
221         try {
222             new DistributionParameterHandler().getParameters(arguments);
223             fail("test should throw an exception here");
224         } catch (final Exception e) {
225             assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null"));
226         }
227     }
228
229     @Test
230     public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException {
231         final String[] distributionConfigParameters =
232                 { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json" };
233
234         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
235         arguments.parse(distributionConfigParameters);
236
237         try {
238             new DistributionParameterHandler().getParameters(arguments);
239             fail("test should throw an exception here");
240         } catch (final Exception e) {
241             assertTrue(e.getMessage().contains("parameter not a regular parameter: receptionHandlerParameters"));
242         }
243     }
244
245     @Test
246     public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException {
247         final String[] distributionConfigParameters =
248                 { "-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json" };
249
250         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
251         arguments.parse(distributionConfigParameters);
252
253         try {
254             new DistributionParameterHandler().getParameters(arguments);
255             fail("test should throw an exception here");
256         } catch (final Exception e) {
257             assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null"));
258         }
259     }
260
261     @Test
262     public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException {
263         final String[] distributionConfigParameters =
264                 { "-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json" };
265
266         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
267         arguments.parse(distributionConfigParameters);
268
269         try {
270             new DistributionParameterHandler().getParameters(arguments);
271             fail("test should throw an exception here");
272         } catch (final Exception e) {
273             assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null"));
274         }
275     }
276
277     @Test
278     public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException {
279         final String[] distributionConfigParameters =
280                 { "-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json" };
281
282         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
283         arguments.parse(distributionConfigParameters);
284
285         try {
286             new DistributionParameterHandler().getParameters(arguments);
287             fail("test should throw an exception here");
288         } catch (final Exception e) {
289             assertTrue(e.getMessage().contains("parameter not a regular parameter: policyDecoders"));
290         }
291     }
292
293     @Test
294     public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException {
295         final String[] distributionConfigParameters =
296                 { "-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json" };
297
298         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
299         arguments.parse(distributionConfigParameters);
300
301         try {
302             new DistributionParameterHandler().getParameters(arguments);
303             fail("test should throw an exception here");
304         } catch (final Exception e) {
305             assertTrue(e.getMessage().contains("parameter not a regular parameter: policyForwarders"));
306         }
307     }
308
309     @Test
310     public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException {
311         final String[] distributionConfigParameters =
312                 { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" };
313
314         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
315         arguments.parse(distributionConfigParameters);
316
317         try {
318             new DistributionParameterHandler().getParameters(arguments);
319             fail("test should throw an exception here");
320         } catch (final Exception e) {
321             assertTrue(e.getMessage().contains("reception handler class not found in classpath"));
322         }
323     }
324
325     @Test
326     public void testDistributionParameterGroup_InvalidPolicyDecoderClass() throws PolicyDistributionException {
327         final String[] distributionConfigParameters =
328                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyDecoderClass.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             assertTrue(e.getMessage().contains("policy decoder class not found in classpath"));
338         }
339     }
340
341     @Test
342     public void testDistributionParameterGroup_InvalidPolicyForwarderClass() throws PolicyDistributionException {
343         final String[] distributionConfigParameters =
344                 { "-c", "parameters/DistributionConfigParameters_InvalidPolicyForwarderClass.json" };
345
346         final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
347         arguments.parse(distributionConfigParameters);
348
349         try {
350             new DistributionParameterHandler().getParameters(arguments);
351             fail("test should throw an exception here");
352         } catch (final Exception e) {
353             assertTrue(e.getMessage().contains("policy forwarder class not found in classpath"));
354         }
355     }
356 }