2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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.assertThatThrownBy;
26 import static org.junit.Assert.assertTrue;
28 import org.junit.After;
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 * Test Apex Command Line Arguments.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class ApexCommandLineArgumentsTest {
39 public void clearRelativeFileRoot() {
40 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
44 public void testCommandLineArguments() throws ApexException {
45 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
47 final String[] args00 = {""};
48 apexArguments.parse(args00);
49 assertThatThrownBy(() -> apexArguments.validate())
50 .hasMessage("Tosca Policy file was not specified as an argument");
52 final String[] args01 = {"-h"};
53 final String result = apexArguments.parse(args01);
54 assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
56 final String[] args02 = {"-v"};
57 final String result02 = apexArguments.parse(args02);
58 assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));
60 final String[] args03 = {"-v", "-h"};
61 final String result03 = apexArguments.parse(args03);
62 assertTrue(result03.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
64 final String[] args04 = {"-h", "-v"};
65 final String result04 = apexArguments.parse(args04);
66 assertTrue(result04.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
68 final String[] args05 = {"-a"};
69 assertThatThrownBy(() -> apexArguments.parse(args05))
70 .hasMessage("invalid command line arguments specified : Unrecognized option: -a");
72 final String[] args06 = {"-p", "goodbye", "-h", "-v"};
73 final String result06 = apexArguments.parse(args06);
74 assertTrue(result06.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
76 final String[] args07 = {"-p", "goodbye", "-h", "aaa"};
77 assertThatThrownBy(() -> apexArguments.parse(args07))
78 .hasMessage("too many command line arguments specified : [-p, goodbye, -h, aaa]");
82 public void testCommandLineFileParameters() throws ApexException {
83 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
85 final String[] args00 = {"-c", "zooby"};
86 assertThatThrownBy(() -> apexArguments.parse(args00))
87 .hasMessage("invalid command line arguments specified : Unrecognized option: -c");
89 final String[] args01 = {"-p"};
90 assertThatThrownBy(() -> apexArguments.parse(args01))
91 .hasMessage("invalid command line arguments specified : Missing argument for option: p");
93 final String[] args02 = {"-p", "src/test/resources/parameters/goodParams.json"};
94 apexArguments.parse(args02);
95 apexArguments.validate();
97 final String[] args03 = {"-p", "src/test/resources/parameters/goodParams.json", "-m", "zooby"};
98 assertThatThrownBy(() -> apexArguments.parse(args03))
99 .hasMessage("invalid command line arguments specified : Unrecognized option: -m");
101 final String[] args06 = {"-p", "src/test/resources/parameters/goodParams.json"};
102 apexArguments.parse(args06);
103 apexArguments.validate();
107 public void testCommandLineRelativeRootParameters() throws ApexException {
108 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
110 final String[] args00 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby"};
111 apexArguments.parse(args00);
112 assertThatThrownBy(() -> apexArguments.validate())
113 .hasMessageContaining("zooby\" does not exist or is not a directory");
115 final String[] args01 = {"-rfr"};
116 assertThatThrownBy(() -> apexArguments.parse(args01))
117 .hasMessage("invalid command line arguments specified : Missing argument for option: rfr");
119 final String[] args02 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml"};
120 apexArguments.parse(args02);
121 assertThatThrownBy(() -> apexArguments.validate())
122 .hasMessageContaining("pom.xml\" does not exist or is not a directory");
124 final String[] args03 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "target"};
125 apexArguments.parse(args03);
126 apexArguments.validate();
128 final String[] args04 =
129 {"-p", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources"};
130 apexArguments.parse(args04);
131 apexArguments.validate();