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