480a5d8a2fe151d3b787d79a92b6bb87d4e99480
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / parameters / TestApiParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4  *  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.api.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.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import org.junit.Test;
32 import org.onap.policy.api.main.exception.PolicyApiException;
33 import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
34
35 /**
36  * Class to perform unit test of ApiParameterHandler.
37  *
38  */
39 public class TestApiParameterHandler {
40     @Test
41     public void testParameterHandlerNoParameterFile() throws PolicyApiException {
42         final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
43         final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
44         noArguments.parse(noArgumentString);
45
46         try {
47             new ApiParameterHandler().getParameters(noArguments);
48             fail("test should throw an exception here");
49         } catch (final Exception e) {
50             assertTrue(e.getMessage().contains("FileNotFoundException"));
51         }
52     }
53
54     @Test
55     public void testParameterHandlerEmptyParameters() throws PolicyApiException {
56         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
57         final ApiCommandLineArguments emptyArguments = new ApiCommandLineArguments();
58         emptyArguments.parse(emptyArgumentString);
59
60         try {
61             new ApiParameterHandler().getParameters(emptyArguments);
62             fail("test should throw an exception here");
63         } catch (final Exception e) {
64             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
65         }
66     }
67
68     @Test
69     public void testParameterHandlerBadParameters() throws PolicyApiException {
70         final String[] badArgumentString = { "-c", "parameters/BadParameters.json" };
71         final ApiCommandLineArguments badArguments = new ApiCommandLineArguments();
72         badArguments.parse(badArgumentString);
73
74         try {
75             new ApiParameterHandler().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 PolicyApiException {
86         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
87         final ApiCommandLineArguments invalidArguments = new ApiCommandLineArguments();
88         invalidArguments.parse(invalidArgumentString);
89
90         try {
91             new ApiParameterHandler().getParameters(invalidArguments);
92             fail("test should throw an exception here");
93         } catch (final Exception e) {
94             assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
95                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
96                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
97         }
98     }
99
100     @Test
101     public void testParameterHandlerNoParameters() throws PolicyApiException {
102         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
103         final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
104         noArguments.parse(noArgumentString);
105
106         try {
107             new ApiParameterHandler().getParameters(noArguments);
108             fail("test should throw an exception here");
109         } catch (final Exception e) {
110             String expMsg = "validation error(s) on parameters from \"parameters/NoParameters.json\"\nparameter group "
111                     + "\"null\" type \"org.onap.policy.api.main.parameters.ApiParameterGroup\" INVALID, parameter "
112                     + "group has status INVALID\n"
113                     + "  field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string\n";
114             assertEquals(expMsg, e.getMessage());
115         }
116     }
117
118     @Test
119     public void testParameterHandlerMinumumParameters() throws PolicyApiException {
120         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
121         final ApiCommandLineArguments minArguments = new ApiCommandLineArguments();
122         minArguments.parse(minArgumentString);
123         final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(minArguments);
124         assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
125     }
126
127     @Test
128     public void testApiParameterGroup() throws PolicyApiException {
129         final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" };
130         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
131         arguments.parse(apiConfigParameters);
132         final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments);
133         assertTrue(arguments.checkSetConfigurationFilePath());
134         assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
135     }
136
137     @Test
138     public void testApiParameterGroup_InvalidName() throws PolicyApiException {
139         final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters_InvalidName.json" };
140         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
141         arguments.parse(apiConfigParameters);
142
143         try {
144             new ApiParameterHandler().getParameters(arguments);
145             fail("test should throw an exception here");
146         } catch (final Exception e) {
147             assertTrue(e.getMessage().contains(
148                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
149         }
150     }
151
152     @Test
153     public void testApiParameterGroup_InvalidRestServerParameters()
154             throws PolicyApiException, IOException {
155         final String[] apiConfigParameters =
156             { "-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json" };
157         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
158         arguments.parse(apiConfigParameters);
159
160         try {
161             new ApiParameterHandler().getParameters(arguments);
162             fail("test should throw an exception here");
163         } catch (final Exception e) {
164             final String expectedResult = new String(Files.readAllBytes(
165                     Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")))
166                             .replaceAll("\\s+", "");
167             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
168         }
169     }
170
171     @Test
172     public void testApiVersion() throws PolicyApiException {
173         final String[] apiConfigParameters = { "-v" };
174         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
175         final String version = arguments.parse(apiConfigParameters);
176         assertTrue(version.startsWith("ONAP Policy Framework Api Service"));
177     }
178
179     @Test
180     public void testApiHelp() throws PolicyApiException {
181         final String[] apiConfigParameters = { "-h" };
182         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
183         final String help = arguments.parse(apiConfigParameters);
184         assertTrue(help.startsWith("usage:"));
185     }
186
187     @Test
188     public void testApiInvalidOption() throws PolicyApiException {
189         final String[] apiConfigParameters = { "-d" };
190         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
191         try {
192             arguments.parse(apiConfigParameters);
193         } catch (final Exception exp) {
194             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
195         }
196     }
197 }