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
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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.FileNotFoundException;
29 import org.junit.Test;
30 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
31 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
32 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
33 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
34 import org.onap.policy.common.utils.coder.CoderException;
37 * Class to perform unit test of {@link ApexStarterParameterHandler}.
39 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
41 public class TestApexStarterParameterHandler {
44 public void testParameterHandlerNoParameterFile() throws ApexStarterException {
45 final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
47 final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments();
48 emptyArguments.parse(emptyArgumentString);
51 new ApexStarterParameterHandler().getParameters(emptyArguments);
52 fail("test should throw an exception here");
53 } catch (final Exception e) {
54 assertTrue(e.getCause() instanceof CoderException);
55 assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
60 public void testParameterHandlerEmptyParameters() throws ApexStarterException {
61 final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
63 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
64 noArguments.parse(noArgumentString);
67 new ApexStarterParameterHandler().getParameters(noArguments);
68 fail("test should throw an exception here");
69 } catch (final Exception e) {
70 assertTrue(e.getMessage().contains("no parameters found"));
75 public void testParameterHandlerInvalidParameters() throws ApexStarterException {
76 final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
78 final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments();
79 invalidArguments.parse(invalidArgumentString);
82 new ApexStarterParameterHandler().getParameters(invalidArguments);
83 fail("test should throw an exception here");
84 } catch (final Exception e) {
85 assertTrue(e.getMessage().startsWith("error reading parameters from"));
86 assertTrue(e.getCause() instanceof CoderException);
91 public void testParameterHandlerNoParameters() throws ApexStarterException {
92 final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
94 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
95 noArguments.parse(noArgumentString);
98 new ApexStarterParameterHandler().getParameters(noArguments);
99 } catch (final Exception e) {
100 assertTrue(e.getMessage().contains("is null"));
105 public void testApexStarterParameterGroup() throws ApexStarterException {
106 final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
108 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
109 arguments.parse(apexStarterConfigParameters);
111 final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
112 assertTrue(arguments.checkSetConfigurationFilePath());
113 assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName());
117 public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException {
118 final String[] apexStarterConfigParameters =
119 { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
121 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
122 arguments.parse(apexStarterConfigParameters);
125 new ApexStarterParameterHandler().getParameters(arguments);
126 fail("test should throw an exception here");
127 } catch (final Exception e) {
128 assertTrue(e.getMessage().contains(
129 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
134 public void testApexStarterVersion() throws ApexStarterException {
135 final String[] apexStarterConfigParameters = { "-v" };
136 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
137 final String version = arguments.parse(apexStarterConfigParameters);
138 assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service"));
142 public void testApexStarterHelp() throws ApexStarterException {
143 final String[] apexStarterConfigParameters = { "-h" };
144 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
145 final String help = arguments.parse(apexStarterConfigParameters);
146 assertTrue(help.startsWith("usage:"));
150 public void testApexStarterInvalidOption() throws ApexStarterException {
151 final String[] apexStarterConfigParameters = { "-d" };
152 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
154 arguments.parse(apexStarterConfigParameters);
155 } catch (final Exception exp) {
156 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));