2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.main;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertTrue;
29 import org.junit.After;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.common.utils.cmd.CommandLineException;
35 * Test Apex Command Line Arguments.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class ApexCommandLineArgumentsTest {
41 public void clearRelativeFileRoot() {
42 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
46 public void testCommandLineArguments() throws ApexException, CommandLineException {
47 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
49 final String[] args01 = {"-h"};
50 final String result = apexArguments.parse(args01);
51 assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
53 final String[] args02 = {"-v"};
54 final String result02 = apexArguments.parse(args02);
55 assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));
57 final String[] args03 = {"-v", "-h"};
58 final String result03 = apexArguments.parse(args03);
59 assertTrue(result03.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
61 final String[] args04 = {"-h", "-v"};
62 final String result04 = apexArguments.parse(args04);
63 assertTrue(result04.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
65 final String[] args06 = {"-p", "goodbye", "-h", "-v"};
66 final String result06 = apexArguments.parse(args06);
67 assertTrue(result06.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
71 public void testCommandLineArgumentsExceptions() throws ApexException, CommandLineException {
72 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
74 final String[] args00 = {""};
75 apexArguments.parse(args00);
76 assertThatThrownBy(() -> apexArguments.validateInputFiles())
77 .hasMessage("Tosca Policy file was not specified as an argument");
79 final String[] args05 = {"-a"};
80 assertThatThrownBy(() -> apexArguments.parse(args05)).hasMessage("invalid command line arguments specified")
81 .hasRootCauseMessage("Unrecognized option: -a");
83 final String[] args07 = {"-p", "goodbye", "-h", "aaa"};
84 assertThatThrownBy(() -> apexArguments.parse(args07))
85 .hasMessage("too many command line arguments specified: [-p, goodbye, -h, aaa]");
89 public void testCommandLineFileParameters() throws ApexException, CommandLineException {
90 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
92 final String[] args02 = {"-p", "src/test/resources/parameters/goodParams.json"};
93 apexArguments.parse(args02);
94 assertThatCode(() -> apexArguments.validateInputFiles()).doesNotThrowAnyException();
98 public void testCommandLineFileParametersExceptions() throws ApexException, CommandLineException {
99 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
101 final String[] args00 = {"-c", "zooby"};
102 assertThatThrownBy(() -> apexArguments.parse(args00)).hasMessage("invalid command line arguments specified")
103 .hasRootCauseMessage("Unrecognized option: -c");
105 final String[] args01 = {"-p"};
106 assertThatThrownBy(() -> apexArguments.parse(args01)).hasMessage("invalid command line arguments specified")
107 .hasRootCauseMessage("Missing argument for option: p");
109 final String[] args03 = {"-p", "src/test/resources/parameters/goodParams.json", "-m", "zooby"};
110 assertThatThrownBy(() -> apexArguments.parse(args03)).hasMessage("invalid command line arguments specified")
111 .hasRootCauseMessage("Unrecognized option: -m");
115 public void testCommandLineRelativeRootParameters() throws ApexException, CommandLineException {
116 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
118 final String[] args00 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby"};
119 apexArguments.parse(args00);
120 assertThatThrownBy(() -> apexArguments.validateInputFiles())
121 .hasMessageContaining("zooby\" does not exist or is not a directory");
123 final String[] args01 = {"-rfr"};
124 assertThatThrownBy(() -> apexArguments.parse(args01)).hasMessage("invalid command line arguments specified")
125 .hasRootCauseMessage("Missing argument for option: rfr");
127 final String[] args02 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml"};
128 apexArguments.parse(args02);
129 assertThatThrownBy(() -> apexArguments.validateInputFiles())
130 .hasMessageContaining("pom.xml\" does not exist or is not a directory");
132 final String[] args03 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "target"};
133 apexArguments.parse(args03);
134 assertThatCode(() -> apexArguments.validateInputFiles()).doesNotThrowAnyException();
136 final String[] args04 = {"-p", "parameters/goodParamsRelative.json", "-rfr", "src/test/resources"};
137 apexArguments.parse(args04);
138 assertThatCode(() -> apexArguments.validateInputFiles()).doesNotThrowAnyException();