Update snapshot and/or references of policy/pap to latest snapshots
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / TestPapParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.parameters;
24
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.FileNotFoundException;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.cmd.CommandLineException;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.pap.main.PolicyPapException;
34 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
35
36 /**
37  * Class to perform unit test of {@link PapParameterHandler}.
38  *
39  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40  */
41 public class TestPapParameterHandler {
42
43     @Test
44     public void testParameterHandlerNoParameterFile() throws PolicyPapException, CommandLineException {
45         final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
46
47         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
48         noArguments.parse(noArgumentString);
49
50         assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments))
51             .hasCauseInstanceOf(CoderException.class)
52             .hasRootCauseInstanceOf(FileNotFoundException.class);
53     }
54
55     @Test
56     public void testParameterHandlerEmptyParameters() throws PolicyPapException, CommandLineException {
57         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
58
59         final PapCommandLineArguments emptyArguments = new PapCommandLineArguments();
60         emptyArguments.parse(emptyArgumentString);
61
62         assertThatThrownBy(() -> new PapParameterHandler().getParameters(emptyArguments))
63             .hasMessageContaining("no parameters found in \"parameters/EmptyParameters.json\"");
64     }
65
66     @Test
67     public void testParameterHandlerInvalidParameters() throws PolicyPapException, CommandLineException {
68         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
69
70         final PapCommandLineArguments invalidArguments = new PapCommandLineArguments();
71         invalidArguments.parse(invalidArgumentString);
72
73         assertThatThrownBy(() -> new PapParameterHandler().getParameters(invalidArguments))
74             .hasMessageStartingWith("error reading parameters from \"parameters/InvalidParameters.json\"")
75             .hasCauseInstanceOf(CoderException.class);
76     }
77
78     @Test
79     public void testParameterHandlerNoParameters() throws PolicyPapException, CommandLineException {
80         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
81
82         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
83         noArguments.parse(noArgumentString);
84
85         assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments)).hasMessageContaining("is null");
86     }
87
88     @Test
89     public void testParameterHandlerMinumumParameters() throws PolicyPapException, CommandLineException {
90         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
91
92         final PapCommandLineArguments minArguments = new PapCommandLineArguments();
93         minArguments.parse(minArgumentString);
94
95         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(minArguments);
96         assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
97     }
98
99     @Test
100     public void testPapParameterGroup() throws PolicyPapException, CommandLineException {
101         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
102
103         final PapCommandLineArguments arguments = new PapCommandLineArguments();
104         arguments.parse(papConfigParameters);
105
106         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
107         assertTrue(arguments.checkSetConfigurationFilePath());
108         assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
109     }
110
111     @Test
112     public void testPapParameterGroup_InvalidName() throws PolicyPapException, CommandLineException {
113         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" };
114
115         final PapCommandLineArguments arguments = new PapCommandLineArguments();
116         arguments.parse(papConfigParameters);
117
118         assertThatThrownBy(() -> new PapParameterHandler().getParameters(arguments))
119             .hasMessageContaining("\"name\" value \" \" INVALID, is blank");
120     }
121
122     @Test
123     public void testPapVersion() throws PolicyPapException, CommandLineException {
124         final String[] papConfigParameters = { "-v" };
125         final PapCommandLineArguments arguments = new PapCommandLineArguments();
126         final String version = arguments.parse(papConfigParameters);
127         assertTrue(version.startsWith("ONAP Policy Framework PAP Service"));
128     }
129
130     @Test
131     public void testPapHelp() throws PolicyPapException, CommandLineException {
132         final String[] papConfigParameters = { "-h" };
133         final PapCommandLineArguments arguments = new PapCommandLineArguments();
134         final String help = arguments.parse(papConfigParameters);
135         assertTrue(help.startsWith("usage:"));
136     }
137
138     @Test
139     public void testPapInvalidOption() throws PolicyPapException, CommandLineException {
140         final String[] papConfigParameters = { "-d" };
141         final PapCommandLineArguments arguments = new PapCommandLineArguments();
142         assertThatThrownBy(() -> arguments.parse(papConfigParameters))
143             .hasMessageStartingWith("invalid command line arguments specified");
144     }
145 }