Remove GroupValidationResult
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / parameters / TestXacmlPdpParameterHandler.java
1 /*-
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
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                 .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");
103     }
104
105     @Test
106     public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
107         final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
108
109         final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
110         minArguments.parse(minArgumentString);
111
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());
116     }
117
118     @Test
119     public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
120         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
121
122         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
123         arguments.parse(xacmlPdpConfigParameters);
124
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());
129     }
130
131     @Test
132     public void testXacmlPdpParameterGroup_Invalid() throws PolicyXacmlPdpException {
133         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
134
135         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
136         arguments.parse(xacmlPdpConfigParameters);
137
138         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
139                 "\"name\" value \" \" INVALID, is blank");
140         xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json";
141
142         arguments.parse(xacmlPdpConfigParameters);
143
144         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
145                 "\"pdpGroup\" value \" \" INVALID, is blank");
146
147         xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_InvalidPdpType.json";
148
149         arguments.parse(xacmlPdpConfigParameters);
150
151         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
152                 "\"pdpType\" value \"\" INVALID, is blank");
153     }
154
155     @Test
156     public void testXacmlPdpParameterGroup_InvalidRestServerParameters() throws PolicyXacmlPdpException, IOException {
157         final String[] xacmlPdpConfigParameters =
158             {"-c", "parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json"};
159
160         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
161         arguments.parse(xacmlPdpConfigParameters);
162
163         new String(Files.readAllBytes(
164                 Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")));
165
166         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments))
167                 .hasMessageContaining("validation error(s) on parameters from "
168                         + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\"");
169     }
170
171     @Test
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"));
177     }
178
179     @Test
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:"));
185     }
186
187     @Test
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");
195     }
196 }