Add PDP Group Deploy and Delete REST API stubs
[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 AT&T Intellectual Property.
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.pap.main.parameters;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.io.FileNotFoundException;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.pap.main.PolicyPapException;
33 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
34
35 /**
36  * Class to perform unit test of {@link PapParameterHandler}.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class TestPapParameterHandler {
41
42     @Test
43     public void testParameterHandlerNoParameterFile() throws PolicyPapException {
44         final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
45
46         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
47         noArguments.parse(noArgumentString);
48
49         try {
50             new PapParameterHandler().getParameters(noArguments);
51             fail("test should throw an exception here");
52         } catch (final Exception e) {
53             assertTrue(e.getCause() instanceof CoderException);
54             assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
55         }
56     }
57
58     @Test
59     public void testParameterHandlerEmptyParameters() throws PolicyPapException {
60         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
61
62         final PapCommandLineArguments emptyArguments = new PapCommandLineArguments();
63         emptyArguments.parse(emptyArgumentString);
64
65         try {
66             new PapParameterHandler().getParameters(emptyArguments);
67             fail("test should throw an exception here");
68         } catch (final Exception e) {
69             assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
70         }
71     }
72
73     @Test
74     public void testParameterHandlerInvalidParameters() throws PolicyPapException {
75         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
76
77         final PapCommandLineArguments invalidArguments = new PapCommandLineArguments();
78         invalidArguments.parse(invalidArgumentString);
79
80         try {
81             new PapParameterHandler().getParameters(invalidArguments);
82             fail("test should throw an exception here");
83         } catch (final Exception e) {
84             assertTrue(e.getMessage().startsWith(
85                             "error reading parameters from \"parameters/InvalidParameters.json\""));
86             assertTrue(e.getCause() instanceof CoderException);
87         }
88     }
89
90     @Test
91     public void testParameterHandlerNoParameters() throws PolicyPapException {
92         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
93
94         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
95         noArguments.parse(noArgumentString);
96
97         assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments)).hasMessageContaining("is null");
98     }
99
100     @Test
101     public void testParameterHandlerMinumumParameters() throws PolicyPapException {
102         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
103
104         final PapCommandLineArguments minArguments = new PapCommandLineArguments();
105         minArguments.parse(minArgumentString);
106
107         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(minArguments);
108         assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
109     }
110
111     @Test
112     public void testPapParameterGroup() throws PolicyPapException {
113         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
114
115         final PapCommandLineArguments arguments = new PapCommandLineArguments();
116         arguments.parse(papConfigParameters);
117
118         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
119         assertTrue(arguments.checkSetConfigurationFilePath());
120         assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
121     }
122
123     @Test
124     public void testPapParameterGroup_InvalidName() throws PolicyPapException {
125         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" };
126
127         final PapCommandLineArguments arguments = new PapCommandLineArguments();
128         arguments.parse(papConfigParameters);
129
130         try {
131             new PapParameterHandler().getParameters(arguments);
132             fail("test should throw an exception here");
133         } catch (final Exception e) {
134             assertTrue(e.getMessage().contains(
135                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
136         }
137     }
138
139     @Test
140     public void testPapVersion() throws PolicyPapException {
141         final String[] papConfigParameters = { "-v" };
142         final PapCommandLineArguments arguments = new PapCommandLineArguments();
143         final String version = arguments.parse(papConfigParameters);
144         assertTrue(version.startsWith("ONAP Policy Framework PAP Service"));
145     }
146
147     @Test
148     public void testPapHelp() throws PolicyPapException {
149         final String[] papConfigParameters = { "-h" };
150         final PapCommandLineArguments arguments = new PapCommandLineArguments();
151         final String help = arguments.parse(papConfigParameters);
152         assertTrue(help.startsWith("usage:"));
153     }
154
155     @Test
156     public void testPapInvalidOption() throws PolicyPapException {
157         final String[] papConfigParameters = { "-d" };
158         final PapCommandLineArguments arguments = new PapCommandLineArguments();
159         try {
160             arguments.parse(papConfigParameters);
161         } catch (final Exception exp) {
162             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
163         }
164     }
165 }