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;
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.coder.CoderException;
34 * Class to perform unit test of {@link ApexStarterParameterHandler}.
36 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
38 public class TestApexStarterParameterHandler {
41 public void testParameterHandlerNoParameterFile() throws ApexStarterException {
42 final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
44 final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments();
45 emptyArguments.parse(emptyArgumentString);
48 new ApexStarterParameterHandler().getParameters(emptyArguments);
49 fail("test should throw an exception here");
50 } catch (final Exception e) {
51 assertTrue(e.getCause() instanceof CoderException);
52 assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
57 public void testParameterHandlerEmptyParameters() throws ApexStarterException {
58 final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
60 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
61 noArguments.parse(noArgumentString);
64 new ApexStarterParameterHandler().getParameters(noArguments);
65 fail("test should throw an exception here");
66 } catch (final Exception e) {
67 assertTrue(e.getMessage().contains("no parameters found"));
72 public void testParameterHandlerInvalidParameters() throws ApexStarterException {
73 final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
75 final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments();
76 invalidArguments.parse(invalidArgumentString);
79 new ApexStarterParameterHandler().getParameters(invalidArguments);
80 fail("test should throw an exception here");
81 } catch (final Exception e) {
82 assertTrue(e.getMessage().startsWith("error reading parameters from"));
83 assertTrue(e.getCause() instanceof CoderException);
88 public void testParameterHandlerNoParameters() throws ApexStarterException {
89 final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
91 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
92 noArguments.parse(noArgumentString);
95 new ApexStarterParameterHandler().getParameters(noArguments);
96 } catch (final Exception e) {
97 assertTrue(e.getMessage().contains("is null"));
102 public void testApexStarterParameterGroup() throws ApexStarterException {
103 final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
105 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
106 arguments.parse(apexStarterConfigParameters);
108 final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
109 assertTrue(arguments.checkSetConfigurationFilePath());
110 assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName());
114 public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException {
115 final String[] apexStarterConfigParameters =
116 { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
118 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
119 arguments.parse(apexStarterConfigParameters);
122 new ApexStarterParameterHandler().getParameters(arguments);
123 fail("test should throw an exception here");
124 } catch (final Exception e) {
125 assertTrue(e.getMessage().contains(
126 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
131 public void testApexStarterVersion() throws ApexStarterException {
132 final String[] apexStarterConfigParameters = { "-v" };
133 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
134 final String version = arguments.parse(apexStarterConfigParameters);
135 assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service"));
139 public void testApexStarterHelp() throws ApexStarterException {
140 final String[] apexStarterConfigParameters = { "-h" };
141 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
142 final String help = arguments.parse(apexStarterConfigParameters);
143 assertTrue(help.startsWith("usage:"));
147 public void testApexStarterInvalidOption() throws ApexStarterException {
148 final String[] apexStarterConfigParameters = { "-d" };
149 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
151 arguments.parse(apexStarterConfigParameters);
152 } catch (final Exception exp) {
153 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));