Changed Xacml-pdp to report pdp group defined in XacmlPdpParameters config file
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / parameters / TestXacmlPdpParameterHandler.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.parameters;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
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;
33
34 /**
35  * Class to perform unit test of XacmlPdpParameterHandler.
36  *
37  */
38 public class TestXacmlPdpParameterHandler {
39     @Test
40     public void testParameterHandlerNoParameterFile() throws PolicyXacmlPdpException {
41         final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
42
43         final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
44         noArguments.parse(noArgumentString);
45
46         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments))
47                 .isInstanceOf(PolicyXacmlPdpException.class);
48     }
49
50     @Test
51     public void testParameterHandlerEmptyParameters() throws PolicyXacmlPdpException {
52         final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
53
54         final XacmlPdpCommandLineArguments emptyArguments = new XacmlPdpCommandLineArguments();
55         emptyArguments.parse(emptyArgumentString);
56
57         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(emptyArguments))
58                 .hasMessage("no parameters found in \"parameters/EmptyParameters.json\"");
59     }
60
61     @Test
62     public void testParameterHandlerBadParameters() throws PolicyXacmlPdpException {
63         final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
64
65         final XacmlPdpCommandLineArguments badArguments = new XacmlPdpCommandLineArguments();
66         badArguments.parse(badArgumentString);
67
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");
72
73     }
74
75     @Test
76     public void testParameterHandlerInvalidParameters() throws PolicyXacmlPdpException {
77         final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"};
78
79         final XacmlPdpCommandLineArguments invalidArguments = new XacmlPdpCommandLineArguments();
80         invalidArguments.parse(invalidArgumentString);
81
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");
86     }
87
88     @Test
89     public void testParameterHandlerNoParameters() throws PolicyXacmlPdpException {
90         final String[] noArgumentString = {"-c", "parameters/NoParameters.json"};
91
92         final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments();
93         noArguments.parse(noArgumentString);
94
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");
106     }
107
108     @Test
109     public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
110         final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
111
112         final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
113         minArguments.parse(minArgumentString);
114
115         final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments);
116         assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
117         assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
118     }
119
120     @Test
121     public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
122         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
123
124         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
125         arguments.parse(xacmlPdpConfigParameters);
126
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());
131     }
132
133     @Test
134     public void testXacmlPdpParameterGroup_InvalidName() throws PolicyXacmlPdpException {
135         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
136
137         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
138         arguments.parse(xacmlPdpConfigParameters);
139
140         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
141                 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
142     }
143
144     @Test
145     public void testXacmlPdpParameterGroup_InvalidPdpGroup() throws PolicyXacmlPdpException {
146         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json"};
147
148         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
149         arguments.parse(xacmlPdpConfigParameters);
150
151         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
152                 "field \"pdpGroup\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
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() throws PolicyXacmlPdpException {
189         final String[] xacmlPdpConfigParameters = {"-d"};
190         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
191         try {
192             arguments.parse(xacmlPdpConfigParameters);
193         } catch (final Exception exp) {
194             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
195         }
196     }
197 }