7a065240dd2fffd24e2838a4425e17b0d13319ce
[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  * ================================================================================
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.api.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
31 import org.junit.Test;
32 import org.onap.policy.api.main.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 =
43         { "-c", "parameters/NoParameterFile.json" };
44
45         final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
46         noArguments.parse(noArgumentString);
47
48         try {
49             new ApiParameterHandler().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 PolicyApiException {
58         final String[] emptyArgumentString =
59         { "-c", "parameters/EmptyParameters.json" };
60
61         final ApiCommandLineArguments emptyArguments = new ApiCommandLineArguments();
62         emptyArguments.parse(emptyArgumentString);
63
64         try {
65             new ApiParameterHandler().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 testParameterHandlerBadParameters() throws PolicyApiException {
74         final String[] badArgumentString =
75         { "-c", "parameters/BadParameters.json" };
76
77         final ApiCommandLineArguments badArguments = new ApiCommandLineArguments();
78         badArguments.parse(badArgumentString);
79
80         try {
81             new ApiParameterHandler().getParameters(badArguments);
82             fail("test should throw an exception here");
83         } catch (final Exception e) {
84             assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
85                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
86                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
87         }
88     }
89
90     @Test
91     public void testParameterHandlerInvalidParameters() throws PolicyApiException {
92         final String[] invalidArgumentString =
93         { "-c", "parameters/InvalidParameters.json" };
94
95         final ApiCommandLineArguments invalidArguments = new ApiCommandLineArguments();
96         invalidArguments.parse(invalidArgumentString);
97
98         try {
99             new ApiParameterHandler().getParameters(invalidArguments);
100             fail("test should throw an exception here");
101         } catch (final Exception e) {
102             assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
103                     + "(JsonSyntaxException):java.lang.IllegalStateException: "
104                     + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
105         }
106     }
107
108     @Test
109     public void testParameterHandlerNoParameters() throws PolicyApiException {
110         final String[] noArgumentString =
111         { "-c", "parameters/NoParameters.json" };
112
113         final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
114         noArguments.parse(noArgumentString);
115
116         try {
117             new ApiParameterHandler().getParameters(noArguments);
118             fail("test should throw an exception here");
119         } catch (final Exception e) {
120             String expMsg = "validation error(s) on parameters from \"parameters/NoParameters.json\"\nparameter group "
121                     + "\"null\" type \"org.onap.policy.api.main.parameters.ApiParameterGroup\" INVALID, parameter "
122                     + "group has status INVALID\n"
123                     + "  field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string\n";
124             assertEquals(expMsg, e.getMessage());
125         }
126     }
127
128     @Test
129     public void testParameterHandlerMinumumParameters() throws PolicyApiException {
130         final String[] minArgumentString =
131         { "-c", "parameters/MinimumParameters.json" };
132
133         final ApiCommandLineArguments minArguments = new ApiCommandLineArguments();
134         minArguments.parse(minArgumentString);
135
136         final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(minArguments);
137         assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
138     }
139
140     @Test
141     public void testApiParameterGroup() throws PolicyApiException {
142         final String[] apiConfigParameters =
143         { "-c", "parameters/ApiConfigParameters.json" };
144
145         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
146         arguments.parse(apiConfigParameters);
147
148         final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments);
149         assertTrue(arguments.checkSetConfigurationFilePath());
150         assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
151     }
152
153     @Test
154     public void testApiParameterGroup_InvalidName() throws PolicyApiException {
155         final String[] apiConfigParameters =
156         { "-c", "parameters/ApiConfigParameters_InvalidName.json" };
157
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             assertTrue(e.getMessage().contains(
166                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
167         }
168     }
169
170     @Test
171     public void testApiParameterGroup_InvalidRestServerParameters()
172             throws PolicyApiException, IOException {
173         final String[] apiConfigParameters =
174         { "-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json" };
175
176         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
177         arguments.parse(apiConfigParameters);
178
179         try {
180             new ApiParameterHandler().getParameters(arguments);
181             fail("test should throw an exception here");
182         } catch (final Exception e) {
183             final String expectedResult = new String(Files.readAllBytes(
184                     Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")))
185                             .replaceAll("\\s+", "");
186             assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
187         }
188     }
189
190     @Test
191     public void testApiVersion() throws PolicyApiException {
192         final String[] apiConfigParameters =
193         { "-v" };
194         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
195         final String version = arguments.parse(apiConfigParameters);
196         assertTrue(version.startsWith("ONAP Policy Framework Api Service"));
197     }
198
199     @Test
200     public void testApiHelp() throws PolicyApiException {
201         final String[] apiConfigParameters =
202         { "-h" };
203         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
204         final String help = arguments.parse(apiConfigParameters);
205         assertTrue(help.startsWith("usage:"));
206     }
207
208     @Test
209     public void testApiInvalidOption() throws PolicyApiException {
210         final String[] apiConfigParameters =
211         { "-d" };
212         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
213         try {
214             arguments.parse(apiConfigParameters);
215         } catch (final Exception exp) {
216             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
217         }
218     }
219 }