Tie XACML REST Decision
[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 \"applicationPath\" type \"java.lang.String\" value \"null\" INVALID, "
103                         + "must have application path for applications to store policies and data.\n");
104     }
105
106     @Test
107     public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException {
108         final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
109
110         final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments();
111         minArguments.parse(minArgumentString);
112
113         final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments);
114         assertEquals(CommonTestData.PDPX_GROUP_NAME, parGroup.getName());
115     }
116
117     @Test
118     public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException {
119         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
120
121         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
122         arguments.parse(xacmlPdpConfigParameters);
123
124         final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
125         assertTrue(arguments.checkSetConfigurationFilePath());
126         assertEquals(CommonTestData.PDPX_GROUP_NAME, parGroup.getName());
127     }
128
129     @Test
130     public void testXacmlPdpParameterGroup_InvalidName() throws PolicyXacmlPdpException {
131         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"};
132
133         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
134         arguments.parse(xacmlPdpConfigParameters);
135
136         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining(
137                 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
138     }
139
140     @Test
141     public void testXacmlPdpParameterGroup_InvalidRestServerParameters() throws PolicyXacmlPdpException, IOException {
142         final String[] xacmlPdpConfigParameters =
143             {"-c", "parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json"};
144
145         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
146         arguments.parse(xacmlPdpConfigParameters);
147
148         new String(Files.readAllBytes(
149                 Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")));
150
151         assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments))
152                 .hasMessageContaining("validation error(s) on parameters from "
153                         + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\"");
154     }
155
156     @Test
157     public void testXacmlPdpVersion() throws PolicyXacmlPdpException {
158         final String[] xacmlPdpConfigParameters = {"-v"};
159         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
160         final String version = arguments.parse(xacmlPdpConfigParameters);
161         assertTrue(version.startsWith("ONAP Policy Framework Xacml PDP Service"));
162     }
163
164     @Test
165     public void testXacmlPdpHelp() throws PolicyXacmlPdpException {
166         final String[] xacmlPdpConfigParameters = {"-h"};
167         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
168         final String help = arguments.parse(xacmlPdpConfigParameters);
169         assertTrue(help.startsWith("usage:"));
170     }
171
172     @Test
173     public void testXacmlPdpInvalidOption() throws PolicyXacmlPdpException {
174         final String[] xacmlPdpConfigParameters = {"-d"};
175         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
176         try {
177             arguments.parse(xacmlPdpConfigParameters);
178         } catch (final Exception exp) {
179             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
180         }
181     }
182 }