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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.pdp.parameters;
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;
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;
36 * Class to perform unit test of {@link PdpSimulatorParameterHandler}.
38 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40 public class TestPdpSimulatorParameterHandler {
43 public void testParameterHandlerNoParameterFile() throws PdpSimulatorException {
44 final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
46 final PdpSimulatorCommandLineArguments emptyArguments = new PdpSimulatorCommandLineArguments();
47 emptyArguments.parse(emptyArgumentString);
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);
59 public void testParameterHandlerEmptyParameters() throws PdpSimulatorException {
60 final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
62 final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
63 noArguments.parse(noArgumentString);
65 assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
66 .hasMessageContaining("no parameters found");
70 public void testParameterHandlerInvalidParameters() throws PdpSimulatorException {
71 final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
73 final PdpSimulatorCommandLineArguments invalidArguments = new PdpSimulatorCommandLineArguments();
74 invalidArguments.parse(invalidArgumentString);
76 assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(invalidArguments))
77 .hasMessageStartingWith("error reading parameters from")
78 .hasCauseInstanceOf(CoderException.class);
82 public void testParameterHandlerNoParameters() throws PdpSimulatorException {
83 final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
85 final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
86 noArguments.parse(noArgumentString);
88 assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
89 .hasMessageContaining("is null");
93 public void testPdpSimulatorParameterGroup() throws PdpSimulatorException {
94 final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
96 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
97 arguments.parse(pdpSimulatorConfigParameters);
99 final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
100 assertTrue(arguments.checkSetConfigurationFilePath());
101 assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, parGroup.getName());
105 public void testPdpSimulatorParameterGroup_InvalidName() throws PdpSimulatorException {
106 final String[] pdpSimulatorConfigParameters = {"-c",
107 "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json"};
109 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
110 arguments.parse(pdpSimulatorConfigParameters);
112 assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(arguments)).hasMessageContaining(
113 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
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"));
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:"));
133 public void testPdpSimulatorInvalidOption() {
134 final String[] pdpSimulatorConfigParameters = { "-d" };
135 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
137 assertThatThrownBy(() -> arguments.parse(pdpSimulatorConfigParameters))
138 .hasMessageStartingWith("invalid command line arguments specified");