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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.parameters;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
31 import org.junit.Test;
32 import org.onap.policy.api.main.PolicyApiException;
33 import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
36 * Class to perform unit test of ApiParameterHandler.
39 public class TestApiParameterHandler {
41 public void testParameterHandlerNoParameterFile() throws PolicyApiException {
42 final String[] noArgumentString =
43 { "-c", "parameters/NoParameterFile.json" };
45 final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
46 noArguments.parse(noArgumentString);
49 new ApiParameterHandler().getParameters(noArguments);
50 fail("test should throw an exception here");
51 } catch (final Exception e) {
52 assertTrue(e.getMessage().contains("FileNotFoundException"));
57 public void testParameterHandlerEmptyParameters() throws PolicyApiException {
58 final String[] emptyArgumentString =
59 { "-c", "parameters/EmptyParameters.json" };
61 final ApiCommandLineArguments emptyArguments = new ApiCommandLineArguments();
62 emptyArguments.parse(emptyArgumentString);
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());
73 public void testParameterHandlerBadParameters() throws PolicyApiException {
74 final String[] badArgumentString =
75 { "-c", "parameters/BadParameters.json" };
77 final ApiCommandLineArguments badArguments = new ApiCommandLineArguments();
78 badArguments.parse(badArgumentString);
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());
91 public void testParameterHandlerInvalidParameters() throws PolicyApiException {
92 final String[] invalidArgumentString =
93 { "-c", "parameters/InvalidParameters.json" };
95 final ApiCommandLineArguments invalidArguments = new ApiCommandLineArguments();
96 invalidArguments.parse(invalidArgumentString);
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());
109 public void testParameterHandlerNoParameters() throws PolicyApiException {
110 final String[] noArgumentString =
111 { "-c", "parameters/NoParameters.json" };
113 final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
114 noArguments.parse(noArgumentString);
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());
129 public void testParameterHandlerMinumumParameters() throws PolicyApiException {
130 final String[] minArgumentString =
131 { "-c", "parameters/MinimumParameters.json" };
133 final ApiCommandLineArguments minArguments = new ApiCommandLineArguments();
134 minArguments.parse(minArgumentString);
136 final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(minArguments);
137 assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
141 public void testApiParameterGroup() throws PolicyApiException {
142 final String[] apiConfigParameters =
143 { "-c", "parameters/ApiConfigParameters.json" };
145 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
146 arguments.parse(apiConfigParameters);
148 final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments);
149 assertTrue(arguments.checkSetConfigurationFilePath());
150 assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
154 public void testApiParameterGroup_InvalidName() throws PolicyApiException {
155 final String[] apiConfigParameters =
156 { "-c", "parameters/ApiConfigParameters_InvalidName.json" };
158 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
159 arguments.parse(apiConfigParameters);
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"));
171 public void testApiParameterGroup_InvalidRestServerParameters()
172 throws PolicyApiException, IOException {
173 final String[] apiConfigParameters =
174 { "-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json" };
176 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
177 arguments.parse(apiConfigParameters);
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+", ""));
191 public void testApiVersion() throws PolicyApiException {
192 final String[] apiConfigParameters =
194 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
195 final String version = arguments.parse(apiConfigParameters);
196 assertTrue(version.startsWith("ONAP Policy Framework Api Service"));
200 public void testApiHelp() throws PolicyApiException {
201 final String[] apiConfigParameters =
203 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
204 final String help = arguments.parse(apiConfigParameters);
205 assertTrue(help.startsWith("usage:"));
209 public void testApiInvalidOption() throws PolicyApiException {
210 final String[] apiConfigParameters =
212 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
214 arguments.parse(apiConfigParameters);
215 } catch (final Exception exp) {
216 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));