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.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.pap.main.PolicyPapException;
33 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
36 * Class to perform unit test of {@link PapParameterHandler}.
38 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40 public class TestPapParameterHandler {
43 public void testParameterHandlerNoParameterFile() throws PolicyPapException {
44 final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
46 final PapCommandLineArguments noArguments = new PapCommandLineArguments();
47 noArguments.parse(noArgumentString);
50 new PapParameterHandler().getParameters(noArguments);
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 PolicyPapException {
60 final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
62 final PapCommandLineArguments emptyArguments = new PapCommandLineArguments();
63 emptyArguments.parse(emptyArgumentString);
66 new PapParameterHandler().getParameters(emptyArguments);
67 fail("test should throw an exception here");
68 } catch (final Exception e) {
69 assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
74 public void testParameterHandlerInvalidParameters() throws PolicyPapException {
75 final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
77 final PapCommandLineArguments invalidArguments = new PapCommandLineArguments();
78 invalidArguments.parse(invalidArgumentString);
81 new PapParameterHandler().getParameters(invalidArguments);
82 fail("test should throw an exception here");
83 } catch (final Exception e) {
84 assertTrue(e.getMessage().startsWith(
85 "error reading parameters from \"parameters/InvalidParameters.json\""));
86 assertTrue(e.getCause() instanceof CoderException);
91 public void testParameterHandlerNoParameters() throws PolicyPapException {
92 final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
94 final PapCommandLineArguments noArguments = new PapCommandLineArguments();
95 noArguments.parse(noArgumentString);
97 assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments)).hasMessageContaining("is null");
101 public void testParameterHandlerMinumumParameters() throws PolicyPapException {
102 final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
104 final PapCommandLineArguments minArguments = new PapCommandLineArguments();
105 minArguments.parse(minArgumentString);
107 final PapParameterGroup parGroup = new PapParameterHandler().getParameters(minArguments);
108 assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
112 public void testPapParameterGroup() throws PolicyPapException {
113 final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
115 final PapCommandLineArguments arguments = new PapCommandLineArguments();
116 arguments.parse(papConfigParameters);
118 final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
119 assertTrue(arguments.checkSetConfigurationFilePath());
120 assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName());
124 public void testPapParameterGroup_InvalidName() throws PolicyPapException {
125 final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" };
127 final PapCommandLineArguments arguments = new PapCommandLineArguments();
128 arguments.parse(papConfigParameters);
131 new PapParameterHandler().getParameters(arguments);
132 fail("test should throw an exception here");
133 } catch (final Exception e) {
134 assertTrue(e.getMessage().contains(
135 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
140 public void testPapVersion() throws PolicyPapException {
141 final String[] papConfigParameters = { "-v" };
142 final PapCommandLineArguments arguments = new PapCommandLineArguments();
143 final String version = arguments.parse(papConfigParameters);
144 assertTrue(version.startsWith("ONAP Policy Framework PAP Service"));
148 public void testPapHelp() throws PolicyPapException {
149 final String[] papConfigParameters = { "-h" };
150 final PapCommandLineArguments arguments = new PapCommandLineArguments();
151 final String help = arguments.parse(papConfigParameters);
152 assertTrue(help.startsWith("usage:"));
156 public void testPapInvalidOption() throws PolicyPapException {
157 final String[] papConfigParameters = { "-d" };
158 final PapCommandLineArguments arguments = new PapCommandLineArguments();
160 arguments.parse(papConfigParameters);
161 } catch (final Exception exp) {
162 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));