Changes for Checkstyle 8.32
[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 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.coder.CoderException;
32 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
33 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
34
35 /**
36  * Class to perform unit test of {@link PdpSimulatorParameterHandler}.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class TestPdpSimulatorParameterHandler {
41
42     @Test
43     public void testParameterHandlerNoParameterFile() throws PdpSimulatorException {
44         final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
45
46         final PdpSimulatorCommandLineArguments emptyArguments = new PdpSimulatorCommandLineArguments();
47         emptyArguments.parse(emptyArgumentString);
48
49         try {
50             new PdpSimulatorParameterHandler().getParameters(emptyArguments);
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 PdpSimulatorException {
60         final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
61
62         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
63         noArguments.parse(noArgumentString);
64
65         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
66                         .hasMessageContaining("no parameters found");
67     }
68
69     @Test
70     public void testParameterHandlerInvalidParameters() throws PdpSimulatorException {
71         final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
72
73         final PdpSimulatorCommandLineArguments invalidArguments = new PdpSimulatorCommandLineArguments();
74         invalidArguments.parse(invalidArgumentString);
75
76         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(invalidArguments))
77                         .hasMessageStartingWith("error reading parameters from")
78                         .hasCauseInstanceOf(CoderException.class);
79     }
80
81     @Test
82     public void testParameterHandlerNoParameters() throws PdpSimulatorException {
83         final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
84
85         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
86         noArguments.parse(noArgumentString);
87
88         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
89                         .hasMessageContaining("is null");
90     }
91
92     @Test
93     public void testPdpSimulatorParameterGroup() throws PdpSimulatorException {
94         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
95
96         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
97         arguments.parse(pdpSimulatorConfigParameters);
98
99         final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
100         assertTrue(arguments.checkSetConfigurationFilePath());
101         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, parGroup.getName());
102     }
103
104     @Test
105     public void testPdpSimulatorParameterGroup_InvalidName() throws PdpSimulatorException {
106         final String[] pdpSimulatorConfigParameters = {"-c",
107             "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json"};
108
109         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
110         arguments.parse(pdpSimulatorConfigParameters);
111
112         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(arguments)).hasMessageContaining(
113                         "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
114     }
115
116     @Test
117     public void testPdpSimulatorVersion() throws PdpSimulatorException {
118         final String[] pdpSimulatorConfigParameters = { "-v" };
119         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
120         final String version = arguments.parse(pdpSimulatorConfigParameters);
121         assertTrue(version.startsWith("ONAP Policy-PDP simulator Service"));
122     }
123
124     @Test
125     public void testPdpSimulatorHelp() throws PdpSimulatorException {
126         final String[] pdpSimulatorConfigParameters = { "-h" };
127         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
128         final String help = arguments.parse(pdpSimulatorConfigParameters);
129         assertTrue(help.startsWith("usage:"));
130     }
131
132     @Test
133     public void testPdpSimulatorInvalidOption() {
134         final String[] pdpSimulatorConfigParameters = { "-d" };
135         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
136
137         assertThatThrownBy(() -> arguments.parse(pdpSimulatorConfigParameters))
138                         .hasMessageStartingWith("invalid command line arguments specified");
139     }
140 }