Use Coder class
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / TestPapParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.parameters;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.FileNotFoundException;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.pap.main.PolicyPapException;
32 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
33
34 /**
35  * Class to perform unit test of {@link PapParameterHandler}.
36  *
37  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
38  */
39 public class TestPapParameterHandler {
40
41     @Test
42     public void testParameterHandlerNoParameterFile() throws PolicyPapException {
43         final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
44
45         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
46         noArguments.parse(noArgumentString);
47
48         try {
49             new PapParameterHandler().getParameters(noArguments);
50             fail("test should throw an exception here");
51         } catch (final Exception e) {
52             assertTrue(e.getCause() instanceof CoderException);
53             assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
54         }
55     }
56
57     @Test
58     public void testParameterHandlerEmptyParameters() throws PolicyPapException {
59         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
60
61         final PapCommandLineArguments emptyArguments = new PapCommandLineArguments();
62         emptyArguments.parse(emptyArgumentString);
63
64         try {
65             new PapParameterHandler().getParameters(emptyArguments);
66             fail("test should throw an exception here");
67         } catch (final Exception e) {
68             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
69         }
70     }
71
72     @Test
73     public void testParameterHandlerInvalidParameters() throws PolicyPapException {
74         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
75
76         final PapCommandLineArguments invalidArguments = new PapCommandLineArguments();
77         invalidArguments.parse(invalidArgumentString);
78
79         try {
80             new PapParameterHandler().getParameters(invalidArguments);
81             fail("test should throw an exception here");
82         } catch (final Exception e) {
83             assertTrue(e.getMessage().startsWith(
84                             "error reading parameters from \"parameters/InvalidParameters.json\""));
85             assertTrue(e.getCause() instanceof CoderException);
86         }
87     }
88
89     @Test
90     public void testParameterHandlerNoParameters() throws PolicyPapException {
91         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
92
93         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
94         noArguments.parse(noArgumentString);
95
96         try {
97             new PapParameterHandler().getParameters(noArguments);
98             fail("test should throw an exception here");
99         } catch (final Exception e) {
100             assertTrue(e.getMessage().contains(
101                     "field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string"));
102         }
103     }
104
105     @Test
106     public void testParameterHandlerMinumumParameters() throws PolicyPapException {
107         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
108
109         final PapCommandLineArguments minArguments = new PapCommandLineArguments();
110         minArguments.parse(minArgumentString);
111
112         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(minArguments);
113         assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
114     }
115
116     @Test
117     public void testPapParameterGroup() throws PolicyPapException {
118         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
119
120         final PapCommandLineArguments arguments = new PapCommandLineArguments();
121         arguments.parse(papConfigParameters);
122
123         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
124         assertTrue(arguments.checkSetConfigurationFilePath());
125         assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
126     }
127
128     @Test
129     public void testPapParameterGroup_InvalidName() throws PolicyPapException {
130         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" };
131
132         final PapCommandLineArguments arguments = new PapCommandLineArguments();
133         arguments.parse(papConfigParameters);
134
135         try {
136             new PapParameterHandler().getParameters(arguments);
137             fail("test should throw an exception here");
138         } catch (final Exception e) {
139             assertTrue(e.getMessage().contains(
140                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
141         }
142     }
143
144     @Test
145     public void testPapVersion() throws PolicyPapException {
146         final String[] papConfigParameters = { "-v" };
147         final PapCommandLineArguments arguments = new PapCommandLineArguments();
148         final String version = arguments.parse(papConfigParameters);
149         assertTrue(version.startsWith("ONAP Policy Framework PAP Service"));
150     }
151
152     @Test
153     public void testPapHelp() throws PolicyPapException {
154         final String[] papConfigParameters = { "-h" };
155         final PapCommandLineArguments arguments = new PapCommandLineArguments();
156         final String help = arguments.parse(papConfigParameters);
157         assertTrue(help.startsWith("usage:"));
158     }
159
160     @Test
161     public void testPapInvalidOption() throws PolicyPapException {
162         final String[] papConfigParameters = { "-d" };
163         final PapCommandLineArguments arguments = new PapCommandLineArguments();
164         try {
165             arguments.parse(papConfigParameters);
166         } catch (final Exception exp) {
167             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
168         }
169     }
170 }