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
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.pap.main.parameters;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.io.FileNotFoundException;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.pap.main.PolicyPapException;
32 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
35 * Class to perform unit test of {@link PapParameterHandler}.
37 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39 public class TestPapParameterHandler {
42 public void testParameterHandlerNoParameterFile() throws PolicyPapException {
43 final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
45 final PapCommandLineArguments noArguments = new PapCommandLineArguments();
46 noArguments.parse(noArgumentString);
49 new PapParameterHandler().getParameters(noArguments);
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);
58 public void testParameterHandlerEmptyParameters() throws PolicyPapException {
59 final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
61 final PapCommandLineArguments emptyArguments = new PapCommandLineArguments();
62 emptyArguments.parse(emptyArgumentString);
65 new PapParameterHandler().getParameters(emptyArguments);
66 fail("test should throw an exception here");
67 } catch (final Exception e) {
68 assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
73 public void testParameterHandlerInvalidParameters() throws PolicyPapException {
74 final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
76 final PapCommandLineArguments invalidArguments = new PapCommandLineArguments();
77 invalidArguments.parse(invalidArgumentString);
80 new PapParameterHandler().getParameters(invalidArguments);
81 fail("test should throw an exception here");
82 } catch (final Exception e) {
83 assertTrue(e.getMessage().startsWith(
84 "error reading parameters from \"parameters/InvalidParameters.json\""));
85 assertTrue(e.getCause() instanceof CoderException);
90 public void testParameterHandlerNoParameters() throws PolicyPapException {
91 final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
93 final PapCommandLineArguments noArguments = new PapCommandLineArguments();
94 noArguments.parse(noArgumentString);
97 new PapParameterHandler().getParameters(noArguments);
98 fail("test should throw an exception here");
99 } catch (final Exception e) {
100 assertTrue(e.getMessage().contains(
101 "field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string"));
106 public void testParameterHandlerMinumumParameters() throws PolicyPapException {
107 final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
109 final PapCommandLineArguments minArguments = new PapCommandLineArguments();
110 minArguments.parse(minArgumentString);
112 final PapParameterGroup parGroup = new PapParameterHandler().getParameters(minArguments);
113 assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
117 public void testPapParameterGroup() throws PolicyPapException {
118 final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
120 final PapCommandLineArguments arguments = new PapCommandLineArguments();
121 arguments.parse(papConfigParameters);
123 final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
124 assertTrue(arguments.checkSetConfigurationFilePath());
125 assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
129 public void testPapParameterGroup_InvalidName() throws PolicyPapException {
130 final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" };
132 final PapCommandLineArguments arguments = new PapCommandLineArguments();
133 arguments.parse(papConfigParameters);
136 new PapParameterHandler().getParameters(arguments);
137 fail("test should throw an exception here");
138 } catch (final Exception e) {
139 assertTrue(e.getMessage().contains(
140 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
145 public void testPapVersion() throws PolicyPapException {
146 final String[] papConfigParameters = { "-v" };
147 final PapCommandLineArguments arguments = new PapCommandLineArguments();
148 final String version = arguments.parse(papConfigParameters);
149 assertTrue(version.startsWith("ONAP Policy Framework PAP Service"));
153 public void testPapHelp() throws PolicyPapException {
154 final String[] papConfigParameters = { "-h" };
155 final PapCommandLineArguments arguments = new PapCommandLineArguments();
156 final String help = arguments.parse(papConfigParameters);
157 assertTrue(help.startsWith("usage:"));
161 public void testPapInvalidOption() throws PolicyPapException {
162 final String[] papConfigParameters = { "-d" };
163 final PapCommandLineArguments arguments = new PapCommandLineArguments();
165 arguments.parse(papConfigParameters);
166 } catch (final Exception exp) {
167 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));