03c73cceb9594d9bde315656094026e98318cc64
[policy/models.git] / models-sim / policy-models-sim-pdp / src / test / java / org / onap / policy / models / sim / pdp / parameters / TestPdpSimulatorParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
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.models.sim.pdp.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.cmd.CommandLineException;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
34 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
35
36 /**
37  * Class to perform unit test of {@link PdpSimulatorParameterHandler}.
38  *
39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40  */
41 public class TestPdpSimulatorParameterHandler {
42
43     @Test
44     public void testParameterHandlerNoParameterFile() throws PdpSimulatorException, CommandLineException {
45         final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
46
47         final PdpSimulatorCommandLineArguments emptyArguments = new PdpSimulatorCommandLineArguments();
48         emptyArguments.parse(emptyArgumentString);
49
50         try {
51             new PdpSimulatorParameterHandler().getParameters(emptyArguments);
52             fail("test should throw an exception here");
53         } catch (final Exception e) {
54             assertTrue(e.getCause() instanceof CoderException);
55             assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
56         }
57     }
58
59     @Test
60     public void testParameterHandlerEmptyParameters() throws PdpSimulatorException, CommandLineException {
61         final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
62
63         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
64         noArguments.parse(noArgumentString);
65
66         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
67                         .hasMessageContaining("no parameters found");
68     }
69
70     @Test
71     public void testParameterHandlerInvalidParameters() throws PdpSimulatorException, CommandLineException {
72         final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
73
74         final PdpSimulatorCommandLineArguments invalidArguments = new PdpSimulatorCommandLineArguments();
75         invalidArguments.parse(invalidArgumentString);
76
77         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(invalidArguments))
78                         .hasMessageStartingWith("error reading parameters from")
79                         .hasCauseInstanceOf(CoderException.class);
80     }
81
82     @Test
83     public void testParameterHandlerNoParameters() throws PdpSimulatorException, CommandLineException {
84         final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
85
86         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
87         noArguments.parse(noArgumentString);
88
89         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
90                         .hasMessageContaining("is null");
91     }
92
93     @Test
94     public void testPdpSimulatorParameterGroup() throws PdpSimulatorException, CommandLineException {
95         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
96
97         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
98         arguments.parse(pdpSimulatorConfigParameters);
99
100         final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
101         assertTrue(arguments.checkSetConfigurationFilePath());
102         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, parGroup.getName());
103     }
104
105     @Test
106     public void testPdpSimulatorParameterGroup_InvalidName() throws PdpSimulatorException, CommandLineException {
107         final String[] pdpSimulatorConfigParameters = {"-c",
108             "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json"};
109
110         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
111         arguments.parse(pdpSimulatorConfigParameters);
112
113         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(arguments)).hasMessageContaining(
114                         "\"name\" value \" \" INVALID, is blank");
115     }
116
117     @Test
118     public void testPdpSimulatorVersion() throws PdpSimulatorException, CommandLineException {
119         final String[] pdpSimulatorConfigParameters = { "-v" };
120         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
121         final String version = arguments.parse(pdpSimulatorConfigParameters);
122         assertTrue(version.startsWith("ONAP Policy-PDP simulator Service"));
123     }
124
125     @Test
126     public void testPdpSimulatorHelp() throws PdpSimulatorException, CommandLineException {
127         final String[] pdpSimulatorConfigParameters = { "-h" };
128         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
129         final String help = arguments.parse(pdpSimulatorConfigParameters);
130         assertTrue(help.startsWith("usage:"));
131     }
132
133     @Test
134     public void testPdpSimulatorInvalidOption() {
135         final String[] pdpSimulatorConfigParameters = { "-d" };
136         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
137
138         assertThatThrownBy(() -> arguments.parse(pdpSimulatorConfigParameters))
139                         .hasMessageStartingWith("invalid command line arguments specified");
140     }
141
142     @Test
143     public void testPdpSimulatorProperty() throws PdpSimulatorException, CommandLineException {
144         final String[] pdpSimulatorConfigParameters = { "-p", "dummyProperties.json" };
145         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
146         arguments.parse(pdpSimulatorConfigParameters);
147         assertTrue(arguments.checkSetPropertyFilePath());
148     }
149 }