2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.services.onappf.parameters;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.io.FileNotFoundException;
28 import org.junit.Test;
29 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
30 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
31 import org.onap.policy.common.utils.cmd.CommandLineException;
32 import org.onap.policy.common.utils.coder.CoderException;
35 * Class to perform unit test of {@link ApexStarterParameterHandler}.
37 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39 public class TestApexStarterParameterHandler {
42 public void testParameterHandlerNoParameterFile() throws ApexStarterException, CommandLineException {
43 final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
45 final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments();
46 emptyArguments.parse(emptyArgumentString);
48 assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(emptyArguments))
49 .hasCauseInstanceOf(CoderException.class)
50 .hasRootCauseInstanceOf(FileNotFoundException.class);
54 public void testParameterHandlerEmptyParameters() throws ApexStarterException, CommandLineException {
55 final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
57 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
58 noArguments.parse(noArgumentString);
60 assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(noArguments))
61 .hasMessageContaining("no parameters found");
65 public void testParameterHandlerInvalidParameters() throws ApexStarterException, CommandLineException {
66 final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
68 final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments();
69 invalidArguments.parse(invalidArgumentString);
71 assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(invalidArguments))
72 .hasMessageStartingWith("error reading parameters from")
73 .hasCauseInstanceOf(CoderException.class);
77 public void testParameterHandlerNoParameters() throws ApexStarterException, CommandLineException {
78 final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
80 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
81 noArguments.parse(noArgumentString);
83 assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(noArguments))
84 .hasMessageContaining("is null");
88 public void testApexStarterParameterGroup() throws ApexStarterException, CommandLineException {
89 final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
91 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
92 arguments.parse(apexStarterConfigParameters);
94 final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
95 assertTrue(arguments.checkSetConfigurationFilePath());
96 assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName());
100 public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException, CommandLineException {
101 final String[] apexStarterConfigParameters =
102 { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
104 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
105 arguments.parse(apexStarterConfigParameters);
107 assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(arguments))
108 .hasMessageContaining("field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a "
109 + "non-blank string");
113 public void testApexStarterVersion() throws ApexStarterException, CommandLineException {
114 final String[] apexStarterConfigParameters = { "-v" };
115 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
116 final String version = arguments.parse(apexStarterConfigParameters);
117 assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service"));
121 public void testApexStarterHelp() throws ApexStarterException, CommandLineException {
122 final String[] apexStarterConfigParameters = { "-h" };
123 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
124 final String help = arguments.parse(apexStarterConfigParameters);
125 assertTrue(help.startsWith("usage:"));
129 public void testApexStarterInvalidOption() throws ApexStarterException {
130 final String[] apexStarterConfigParameters = { "-d" };
131 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
132 assertThatThrownBy(() -> arguments.parse(apexStarterConfigParameters))
133 .hasMessageStartingWith("invalid command line arguments specified");