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