2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.parameters;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import org.junit.Test;
33 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
34 import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments;
37 * Class to perform unit test of XacmlPdpParameterHandler.
40 public class TestXacmlPdpParameterHandler {
42 public void testParameterHandlerNoParameterFile() throws PolicyXacmlPdpException {
43 final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
45 final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
46 noArguments.parse(noArgumentString);
48 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
49 .isInstanceOf(PolicyXacmlPdpException.class);
53 public void testParameterHandlerEmptyParameters() throws PolicyXacmlPdpException {
54 final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
56 final XacmlPdpCommandLineArguments emptyArguments = new XacmlPdpCommandLineArguments();
57 emptyArguments.parse(emptyArgumentString);
59 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(emptyArguments))
60 .hasMessage("no parameters found in \"parameters/EmptyParameters.json\"");
64 public void testParameterHandlerBadParameters() throws PolicyXacmlPdpException {
65 final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
67 final XacmlPdpCommandLineArguments badArguments = new XacmlPdpCommandLineArguments();
68 badArguments.parse(badArgumentString);
70 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(badArguments))
71 .hasMessage("error reading parameters from \"parameters/BadParameters.json\"\n"
72 + "(JsonSyntaxException):java.lang.IllegalStateException: "
73 + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
78 public void testParameterHandlerInvalidParameters() throws PolicyXacmlPdpException {
79 final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"};
81 final XacmlPdpCommandLineArguments invalidArguments = new XacmlPdpCommandLineArguments();
82 invalidArguments.parse(invalidArgumentString);
84 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(invalidArguments))
85 .hasMessage("error reading parameters from \"parameters/InvalidParameters.json\"\n"
86 + "(JsonSyntaxException):java.lang.IllegalStateException: "
87 + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
91 public void testParameterHandlerNoParameters() throws PolicyXacmlPdpException {
92 final String[] noArgumentString = {"-c", "parameters/NoParameters.json"};
94 final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
95 noArguments.parse(noArgumentString);
97 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
98 .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\n"
99 + "parameter group \"null\" type "
100 + "\"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\""
101 + " INVALID, parameter group has status INVALID\n"
102 + " field \"name\" type \"java.lang.String\" value \"null\" INVALID, "
103 + "must be a non-blank string\n"
104 + " field \"pdpGroup\" type \"java.lang.String\" value \"null\" INVALID, "
105 + "must be a non-blank string\n"
106 + " field \"applicationPath\" type \"java.lang.String\" value \"null\" INVALID, "
107 + "must have application path for applications to store policies and data.\n");
111 public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
112 final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
114 final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
115 minArguments.parse(minArgumentString);
117 final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments);
118 assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
119 assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
123 public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
124 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
126 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
127 arguments.parse(xacmlPdpConfigParameters);
129 final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
130 assertTrue(arguments.checkSetConfigurationFilePath());
131 assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
132 assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
136 public void testXacmlPdpParameterGroup_InvalidName() throws PolicyXacmlPdpException {
137 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
139 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
140 arguments.parse(xacmlPdpConfigParameters);
142 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
143 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
147 public void testXacmlPdpParameterGroup_InvalidPdpGroup() throws PolicyXacmlPdpException {
148 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json"};
150 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
151 arguments.parse(xacmlPdpConfigParameters);
153 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
154 "field \"pdpGroup\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
158 public void testXacmlPdpParameterGroup_InvalidRestServerParameters() throws PolicyXacmlPdpException, IOException {
159 final String[] xacmlPdpConfigParameters =
160 {"-c", "parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json"};
162 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
163 arguments.parse(xacmlPdpConfigParameters);
165 new String(Files.readAllBytes(
166 Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")));
168 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments))
169 .hasMessageContaining("validation error(s) on parameters from "
170 + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\"");
174 public void testXacmlPdpVersion() throws PolicyXacmlPdpException {
175 final String[] xacmlPdpConfigParameters = {"-v"};
176 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
177 final String version = arguments.parse(xacmlPdpConfigParameters);
178 assertTrue(version.startsWith("ONAP Policy Framework Xacml PDP Service"));
182 public void testXacmlPdpHelp() throws PolicyXacmlPdpException {
183 final String[] xacmlPdpConfigParameters = {"-h"};
184 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
185 final String help = arguments.parse(xacmlPdpConfigParameters);
186 assertTrue(help.startsWith("usage:"));
190 public void testXacmlPdpInvalidOption() {
191 final String[] xacmlPdpConfigParameters = {"-d"};
192 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
193 assertThatThrownBy(() ->
194 arguments.parse(xacmlPdpConfigParameters)
195 ).isInstanceOf(PolicyXacmlPdpException.class)
196 .hasMessageContaining("invalid command line arguments specified");