2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import org.junit.Test;
32 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
33 import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments;
36 * Class to perform unit test of XacmlPdpParameterHandler.
39 public class TestXacmlPdpParameterHandler {
41 public void testParameterHandlerNoParameterFile() throws PolicyXacmlPdpException {
42 final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
44 final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
45 noArguments.parse(noArgumentString);
47 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
48 .isInstanceOf(PolicyXacmlPdpException.class);
52 public void testParameterHandlerEmptyParameters() throws PolicyXacmlPdpException {
53 final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
55 final XacmlPdpCommandLineArguments emptyArguments = new XacmlPdpCommandLineArguments();
56 emptyArguments.parse(emptyArgumentString);
58 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(emptyArguments))
59 .hasMessage("no parameters found in \"parameters/EmptyParameters.json\"");
63 public void testParameterHandlerBadParameters() throws PolicyXacmlPdpException {
64 final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
66 final XacmlPdpCommandLineArguments badArguments = new XacmlPdpCommandLineArguments();
67 badArguments.parse(badArgumentString);
69 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(badArguments))
70 .hasMessageContaining("error reading parameters from", "parameters/BadParameters.json",
71 "JsonSyntaxException", "java.lang.IllegalStateException",
72 "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
77 public void testParameterHandlerInvalidParameters() throws PolicyXacmlPdpException {
78 final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"};
80 final XacmlPdpCommandLineArguments invalidArguments = new XacmlPdpCommandLineArguments();
81 invalidArguments.parse(invalidArgumentString);
83 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(invalidArguments))
84 .hasMessageContaining("error reading parameters from", "parameters/InvalidParameters.json",
85 "JsonSyntaxException", "java.lang.IllegalStateException",
86 "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
90 public void testParameterHandlerNoParameters() throws PolicyXacmlPdpException {
91 final String[] noArgumentString = {"-c", "parameters/NoParameters.json"};
93 final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
94 noArguments.parse(noArgumentString);
96 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
97 .hasMessageContaining("validation error(s) on parameters from \"parameters/NoParameters.json\"",
98 "\"XacmlPdpParameterGroup\" INVALID, item has status INVALID",
99 "\"name\" value \"null\" INVALID, is null",
100 "\"pdpGroup\" value \"null\" INVALID, is null",
101 "\"pdpType\" value \"null\" INVALID, is null",
102 "\"applicationPath\" value \"null\" INVALID, is null");
106 public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
107 final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
109 final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
110 minArguments.parse(minArgumentString);
112 final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments);
113 assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
114 assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
115 assertEquals(CommonTestData.PDPX_TYPE, parGroup.getPdpType());
119 public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
120 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
122 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
123 arguments.parse(xacmlPdpConfigParameters);
125 final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
126 assertTrue(arguments.checkSetConfigurationFilePath());
127 assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
128 assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
132 public void testXacmlPdpParameterGroup_Invalid() throws PolicyXacmlPdpException {
133 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
135 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
136 arguments.parse(xacmlPdpConfigParameters);
138 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
139 "\"name\" value \" \" INVALID, is blank");
140 xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json";
142 arguments.parse(xacmlPdpConfigParameters);
144 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
145 "\"pdpGroup\" value \" \" INVALID, is blank");
147 xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_InvalidPdpType.json";
149 arguments.parse(xacmlPdpConfigParameters);
151 assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
152 "\"pdpType\" value \"\" INVALID, is blank");
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() {
189 final String[] xacmlPdpConfigParameters = {"-d"};
190 final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
191 assertThatThrownBy(() ->
192 arguments.parse(xacmlPdpConfigParameters)
193 ).isInstanceOf(PolicyXacmlPdpException.class)
194 .hasMessageContaining("invalid command line arguments specified");