2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. 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.pdpx.main.parameters;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import org.junit.Test;
31 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
32 import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments;
35 * Class to perform unit test of XacmlPdpParameterHandler.
38 public class TestXacmlPdpParameterHandler {
40 public void testParameterHandlerNoParameterFile() throws PolicyXacmlPdpException {
41 final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
43 final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
44 noArguments.parse(noArgumentString);
46 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
47 .isInstanceOf(PolicyXacmlPdpException.class);
51 public void testParameterHandlerEmptyParameters() throws PolicyXacmlPdpException {
52 final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
54 final XacmlPdpCommandLineArguments emptyArguments = new XacmlPdpCommandLineArguments();
55 emptyArguments.parse(emptyArgumentString);
57 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(emptyArguments))
58 .hasMessage("no parameters found in \"parameters/EmptyParameters.json\"");
62 public void testParameterHandlerBadParameters() throws PolicyXacmlPdpException {
63 final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
65 final XacmlPdpCommandLineArguments badArguments = new XacmlPdpCommandLineArguments();
66 badArguments.parse(badArgumentString);
68 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(badArguments))
69 .hasMessage("error reading parameters from \"parameters/BadParameters.json\"\n"
70 + "(JsonSyntaxException):java.lang.IllegalStateException: "
71 + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
76 public void testParameterHandlerInvalidParameters() throws PolicyXacmlPdpException {
77 final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"};
79 final XacmlPdpCommandLineArguments invalidArguments = new XacmlPdpCommandLineArguments();
80 invalidArguments.parse(invalidArgumentString);
82 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(invalidArguments))
83 .hasMessage("error reading parameters from \"parameters/InvalidParameters.json\"\n"
84 + "(JsonSyntaxException):java.lang.IllegalStateException: "
85 + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
89 public void testParameterHandlerNoParameters() throws PolicyXacmlPdpException {
90 final String[] noArgumentString = {"-c", "parameters/NoParameters.json"};
92 final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
93 noArguments.parse(noArgumentString);
95 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
96 .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\n"
97 + "parameter group \"null\" type "
98 + "\"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\""
99 + " INVALID, parameter group has status INVALID\n"
100 + " field \"name\" type \"java.lang.String\" value \"null\" INVALID, "
101 + "must be a non-blank string\n"
102 + " field \"pdpGroup\" type \"java.lang.String\" value \"null\" INVALID, "
103 + "must be a non-blank string\n"
104 + " field \"applicationPath\" type \"java.lang.String\" value \"null\" INVALID, "
105 + "must have application path for applications to store policies and data.\n");
109 public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
110 final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
112 final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
113 minArguments.parse(minArgumentString);
115 final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments);
116 assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
117 assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
121 public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
122 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
124 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
125 arguments.parse(xacmlPdpConfigParameters);
127 final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
128 assertTrue(arguments.checkSetConfigurationFilePath());
129 assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
130 assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
134 public void testXacmlPdpParameterGroup_InvalidName() throws PolicyXacmlPdpException {
135 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
137 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
138 arguments.parse(xacmlPdpConfigParameters);
140 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
141 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
145 public void testXacmlPdpParameterGroup_InvalidPdpGroup() throws PolicyXacmlPdpException {
146 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json"};
148 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
149 arguments.parse(xacmlPdpConfigParameters);
151 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
152 "field \"pdpGroup\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
156 public void testXacmlPdpParameterGroup_InvalidRestServerParameters() throws PolicyXacmlPdpException, IOException {
157 final String[] xacmlPdpConfigParameters =
158 {"-c", "parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json"};
160 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
161 arguments.parse(xacmlPdpConfigParameters);
163 new String(Files.readAllBytes(
164 Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")));
166 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments))
167 .hasMessageContaining("validation error(s) on parameters from "
168 + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\"");
172 public void testXacmlPdpVersion() throws PolicyXacmlPdpException {
173 final String[] xacmlPdpConfigParameters = {"-v"};
174 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
175 final String version = arguments.parse(xacmlPdpConfigParameters);
176 assertTrue(version.startsWith("ONAP Policy Framework Xacml PDP Service"));
180 public void testXacmlPdpHelp() throws PolicyXacmlPdpException {
181 final String[] xacmlPdpConfigParameters = {"-h"};
182 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
183 final String help = arguments.parse(xacmlPdpConfigParameters);
184 assertTrue(help.startsWith("usage:"));
188 public void testXacmlPdpInvalidOption() throws PolicyXacmlPdpException {
189 final String[] xacmlPdpConfigParameters = {"-d"};
190 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
192 arguments.parse(xacmlPdpConfigParameters);
193 } catch (final Exception exp) {
194 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));