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.starter.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.starter.ApexStarterCommandLineArguments;
31 import org.onap.policy.apex.starter.exception.ApexStarterException;
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 {
43 final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
45 final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments();
46 emptyArguments.parse(emptyArgumentString);
49 new ApexStarterParameterHandler().getParameters(emptyArguments);
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 ApexStarterException {
59 final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
61 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
62 noArguments.parse(noArgumentString);
65 new ApexStarterParameterHandler().getParameters(noArguments);
66 fail("test should throw an exception here");
67 } catch (final Exception e) {
68 assertTrue(e.getMessage().contains("no parameters found"));
73 public void testParameterHandlerInvalidParameters() throws ApexStarterException {
74 final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
76 final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments();
77 invalidArguments.parse(invalidArgumentString);
80 new ApexStarterParameterHandler().getParameters(invalidArguments);
81 fail("test should throw an exception here");
82 } catch (final Exception e) {
83 assertTrue(e.getMessage().startsWith("error reading parameters from"));
84 assertTrue(e.getCause() instanceof CoderException);
89 public void testParameterHandlerNoParameters() throws ApexStarterException {
90 final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
92 final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
93 noArguments.parse(noArgumentString);
96 new ApexStarterParameterHandler().getParameters(noArguments);
97 } catch (final Exception e) {
98 assertTrue(e.getMessage().contains("is null"));
103 public void testApexStarterParameterGroup() throws ApexStarterException {
104 final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
106 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
107 arguments.parse(apexStarterConfigParameters);
109 final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
110 assertTrue(arguments.checkSetConfigurationFilePath());
111 assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName());
112 assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, parGroup.getTimeInterval());
116 public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException {
117 final String[] apexStarterConfigParameters =
118 { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
120 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
121 arguments.parse(apexStarterConfigParameters);
124 new ApexStarterParameterHandler().getParameters(arguments);
125 fail("test should throw an exception here");
126 } catch (final Exception e) {
127 assertTrue(e.getMessage().contains(
128 "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
133 public void testApexStarterVersion() throws ApexStarterException {
134 final String[] apexStarterConfigParameters = { "-v" };
135 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
136 final String version = arguments.parse(apexStarterConfigParameters);
137 assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service"));
141 public void testApexStarterHelp() throws ApexStarterException {
142 final String[] apexStarterConfigParameters = { "-h" };
143 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
144 final String help = arguments.parse(apexStarterConfigParameters);
145 assertTrue(help.startsWith("usage:"));
149 public void testApexStarterInvalidOption() throws ApexStarterException {
150 final String[] apexStarterConfigParameters = { "-d" };
151 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
153 arguments.parse(apexStarterConfigParameters);
154 } catch (final Exception exp) {
155 assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));