Adding pdp simulator for testing purposes
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.sim.pdp.parameters;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.FileNotFoundException;
28
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
32 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
33
34 /**
35  * Class to perform unit test of {@link PdpSimulatorParameterHandler}.
36  *
37  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
38  */
39 public class TestPdpSimulatorParameterHandler {
40
41     @Test
42     public void testParameterHandlerNoParameterFile() throws PdpSimulatorException {
43         final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
44
45         final PdpSimulatorCommandLineArguments emptyArguments = new PdpSimulatorCommandLineArguments();
46         emptyArguments.parse(emptyArgumentString);
47
48         try {
49             new PdpSimulatorParameterHandler().getParameters(emptyArguments);
50             fail("test should throw an exception here");
51         } catch (final Exception e) {
52             assertTrue(e.getCause() instanceof CoderException);
53             assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
54         }
55     }
56
57     @Test
58     public void testParameterHandlerEmptyParameters() throws PdpSimulatorException {
59         final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
60
61         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
62         noArguments.parse(noArgumentString);
63
64         try {
65             new PdpSimulatorParameterHandler().getParameters(noArguments);
66             fail("test should throw an exception here");
67         } catch (final Exception e) {
68             assertTrue(e.getMessage().contains("no parameters found"));
69         }
70     }
71
72     @Test
73     public void testParameterHandlerInvalidParameters() throws PdpSimulatorException {
74         final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
75
76         final PdpSimulatorCommandLineArguments invalidArguments = new PdpSimulatorCommandLineArguments();
77         invalidArguments.parse(invalidArgumentString);
78
79         try {
80             new PdpSimulatorParameterHandler().getParameters(invalidArguments);
81             fail("test should throw an exception here");
82         } catch (final Exception e) {
83             assertTrue(e.getMessage().startsWith("error reading parameters from"));
84             assertTrue(e.getCause() instanceof CoderException);
85         }
86     }
87
88     @Test
89     public void testParameterHandlerNoParameters() throws PdpSimulatorException {
90         final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
91
92         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
93         noArguments.parse(noArgumentString);
94
95         try {
96             new PdpSimulatorParameterHandler().getParameters(noArguments);
97         } catch (final Exception e) {
98             assertTrue(e.getMessage().contains("is null"));
99         }
100     }
101
102     @Test
103     public void testPdpSimulatorParameterGroup() throws PdpSimulatorException {
104         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
105
106         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
107         arguments.parse(pdpSimulatorConfigParameters);
108
109         final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
110         assertTrue(arguments.checkSetConfigurationFilePath());
111         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, parGroup.getName());
112     }
113
114     @Test
115     public void testPdpSimulatorParameterGroup_InvalidName() throws PdpSimulatorException {
116         final String[] pdpSimulatorConfigParameters = {"-c", 
117             "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json"};
118
119         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
120         arguments.parse(pdpSimulatorConfigParameters);
121
122         try {
123             new PdpSimulatorParameterHandler().getParameters(arguments);
124             fail("test should throw an exception here");
125         } catch (final Exception e) {
126             assertTrue(e.getMessage().contains(
127                 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
128         }
129     }
130
131     @Test
132     public void testPdpSimulatorVersion() throws PdpSimulatorException {
133         final String[] pdpSimulatorConfigParameters = { "-v" };
134         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
135         final String version = arguments.parse(pdpSimulatorConfigParameters);
136         assertTrue(version.startsWith("ONAP Policy-PDP simulator Service"));
137     }
138
139     @Test
140     public void testPdpSimulatorHelp() throws PdpSimulatorException {
141         final String[] pdpSimulatorConfigParameters = { "-h" };
142         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
143         final String help = arguments.parse(pdpSimulatorConfigParameters);
144         assertTrue(help.startsWith("usage:"));
145     }
146
147     @Test
148     public void testPdpSimulatorInvalidOption() throws PdpSimulatorException {
149         final String[] pdpSimulatorConfigParameters = { "-d" };
150         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
151         try {
152             arguments.parse(pdpSimulatorConfigParameters);
153         } catch (final Exception exp) {
154             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
155         }
156     }
157 }