Cleanup unused imports and deprecated methods
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / parameters / TestXacmlPdpParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.parameters;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
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;
34
35 /**
36  * Class to perform unit test of XacmlPdpParameterHandler.
37  *
38  */
39 public class TestXacmlPdpParameterHandler {
40     @Test
41     public void testParameterHandlerNoParameterFile() throws PolicyXacmlPdpException {
42         final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
43
44         final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
45         noArguments.parse(noArgumentString);
46
47         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
48                 .isInstanceOf(PolicyXacmlPdpException.class);
49     }
50
51     @Test
52     public void testParameterHandlerEmptyParameters() throws PolicyXacmlPdpException {
53         final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
54
55         final XacmlPdpCommandLineArguments emptyArguments = new XacmlPdpCommandLineArguments();
56         emptyArguments.parse(emptyArgumentString);
57
58         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(emptyArguments))
59                 .hasMessage("no parameters found in \"parameters/EmptyParameters.json\"");
60     }
61
62     @Test
63     public void testParameterHandlerBadParameters() throws PolicyXacmlPdpException {
64         final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
65
66         final XacmlPdpCommandLineArguments badArguments = new XacmlPdpCommandLineArguments();
67         badArguments.parse(badArgumentString);
68
69         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(badArguments))
70                 .hasMessage("error reading parameters from \"parameters/BadParameters.json\"\n"
71                         + "(JsonSyntaxException):java.lang.IllegalStateException: "
72                         + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
73
74     }
75
76     @Test
77     public void testParameterHandlerInvalidParameters() throws PolicyXacmlPdpException {
78         final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"};
79
80         final XacmlPdpCommandLineArguments invalidArguments = new XacmlPdpCommandLineArguments();
81         invalidArguments.parse(invalidArgumentString);
82
83         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(invalidArguments))
84                 .hasMessage("error reading parameters from \"parameters/InvalidParameters.json\"\n"
85                         + "(JsonSyntaxException):java.lang.IllegalStateException: "
86                         + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name");
87     }
88
89     @Test
90     public void testParameterHandlerNoParameters() throws PolicyXacmlPdpException {
91         final String[] noArgumentString = {"-c", "parameters/NoParameters.json"};
92
93         final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
94         noArguments.parse(noArgumentString);
95
96         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
97                 .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\n"
98                         + "parameter group \"null\" type "
99                         + "\"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\""
100                         + " INVALID, parameter group has status INVALID\n"
101                         + "  field \"name\" type \"java.lang.String\" value \"null\" INVALID, "
102                         + "must be a non-blank string\n"
103                         + "  field \"pdpGroup\" type \"java.lang.String\" value \"null\" INVALID, "
104                         + "must be a non-blank string\n"
105                         + "  field \"applicationPath\" type \"java.lang.String\" value \"null\" INVALID, "
106                         + "must have application path for applications to store policies and data.\n");
107     }
108
109     @Test
110     public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
111         final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
112
113         final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
114         minArguments.parse(minArgumentString);
115
116         final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments);
117         assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
118         assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
119     }
120
121     @Test
122     public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
123         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
124
125         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
126         arguments.parse(xacmlPdpConfigParameters);
127
128         final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
129         assertTrue(arguments.checkSetConfigurationFilePath());
130         assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
131         assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
132     }
133
134     @Test
135     public void testXacmlPdpParameterGroup_InvalidName() throws PolicyXacmlPdpException {
136         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
137
138         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
139         arguments.parse(xacmlPdpConfigParameters);
140
141         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
142                 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
143     }
144
145     @Test
146     public void testXacmlPdpParameterGroup_InvalidPdpGroup() throws PolicyXacmlPdpException {
147         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json"};
148
149         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
150         arguments.parse(xacmlPdpConfigParameters);
151
152         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
153                 "field \"pdpGroup\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
154     }
155
156     @Test
157     public void testXacmlPdpParameterGroup_InvalidRestServerParameters() throws PolicyXacmlPdpException, IOException {
158         final String[] xacmlPdpConfigParameters =
159             {"-c", "parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json"};
160
161         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
162         arguments.parse(xacmlPdpConfigParameters);
163
164         new String(Files.readAllBytes(
165                 Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")));
166
167         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments))
168                 .hasMessageContaining("validation error(s) on parameters from "
169                         + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\"");
170     }
171
172     @Test
173     public void testXacmlPdpVersion() throws PolicyXacmlPdpException {
174         final String[] xacmlPdpConfigParameters = {"-v"};
175         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
176         final String version = arguments.parse(xacmlPdpConfigParameters);
177         assertTrue(version.startsWith("ONAP Policy Framework Xacml PDP Service"));
178     }
179
180     @Test
181     public void testXacmlPdpHelp() throws PolicyXacmlPdpException {
182         final String[] xacmlPdpConfigParameters = {"-h"};
183         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
184         final String help = arguments.parse(xacmlPdpConfigParameters);
185         assertTrue(help.startsWith("usage:"));
186     }
187
188     @Test
189     public void testXacmlPdpInvalidOption() {
190         final String[] xacmlPdpConfigParameters = {"-d"};
191         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
192         assertThatThrownBy(() ->
193             arguments.parse(xacmlPdpConfigParameters)
194         ).isInstanceOf(PolicyXacmlPdpException.class)
195         .hasMessageContaining("invalid command line arguments specified");
196     }
197 }