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