2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.main;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
27 import org.junit.After;
28 import org.junit.Test;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 * Test Apex Command Line Arguments.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
36 public class ApexCommandLineArgumentsTest {
38 public void clearRelativeFileRoot() {
39 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
43 public void testCommandLineArguments() throws ApexException {
44 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
46 final String[] args00 =
48 assertThatThrownBy(() -> {
49 apexArguments.parse(args00);
50 apexArguments.validate();
51 }).hasMessage("Apex configuration file was not specified as an argument");
52 final String[] args01 =
55 final String result = apexArguments.parse(args01);
56 assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
58 final String[] args02 =
60 final String result02 = apexArguments.parse(args02);
61 assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));
63 final String[] args03 =
66 final String result03 = apexArguments.parse(args03);
67 assertTrue(result03.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
69 final String[] args04 =
72 final String result04 = apexArguments.parse(args04);
73 assertTrue(result04.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
75 final String[] args05 =
77 assertThatThrownBy(() -> apexArguments.parse(args05))
78 .hasMessage("invalid command line arguments specified : Unrecognized option: -a");
79 final String[] args06 =
80 { "-c", "hello", "-m", "goodbye", "-h", "-v" };
81 final String result06 = apexArguments.parse(args06);
82 assertTrue(result06.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
84 final String[] args07 =
85 { "-c", "hello", "-m", "goodbye", "-h", "aaa" };
86 assertThatThrownBy(() -> apexArguments.parse(args07))
87 .hasMessage("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]");
91 public void testCommandLineFileParameters() throws ApexException {
92 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
94 final String[] args00 =
96 assertThatThrownBy(() -> {
97 apexArguments.parse(args00);
98 apexArguments.validate();
99 }).hasMessage("Apex configuration file \"zooby\" does not exist");
100 final String[] args01 =
102 assertThatThrownBy(() -> {
103 apexArguments.parse(args01);
104 apexArguments.validate();
105 }).hasMessage("invalid command line arguments specified : Missing argument for option: c");
106 final String[] args02 =
107 { "-c", "src/test/resources/parameters/goodParams.json" };
108 apexArguments.parse(args02);
109 apexArguments.validate();
111 final String[] args03 =
112 { "-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby" };
113 assertThatThrownBy(() -> {
114 apexArguments.parse(args03);
115 apexArguments.validate();
116 }).hasMessage("Apex model file \"zooby\" does not exist");
117 final String[] args04 =
119 assertThatThrownBy(() -> {
120 apexArguments.parse(args04);
121 apexArguments.validate();
122 }).hasMessage("invalid command line arguments specified : Missing argument for option: m");
123 final String[] args05 =
124 { "-c", "src/test/resources/parameters/goodParams.json", "-m" };
125 assertThatThrownBy(() -> {
126 apexArguments.parse(args05);
127 apexArguments.validate();
128 }).hasMessage("invalid command line arguments specified : Missing argument for option: m");
129 final String[] args06 =
130 { "-c", "src/test/resources/parameters/goodParams.json", "-m",
131 "src/test/resources/main/DummyModelFile.json" };
132 apexArguments.parse(args06);
133 apexArguments.validate();
135 final String[] args07 =
136 { "-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json" };
138 apexArguments.parse(args07);
139 apexArguments.validate();
143 public void testCommandLineRelativeRootParameters() throws ApexException {
144 final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
146 final String[] args00 =
147 { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" };
148 assertThatThrownBy(() -> {
149 apexArguments.parse(args00);
150 apexArguments.validate();
151 }).hasMessageContaining("zooby\" does not exist or is not a directory");
152 final String[] args01 =
154 assertThatThrownBy(() -> {
155 apexArguments.parse(args01);
156 apexArguments.validate();
157 }).hasMessage("invalid command line arguments specified : Missing argument for option: rfr");
158 final String[] args02 =
159 { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" };
160 assertThatThrownBy(() -> {
161 apexArguments.parse(args02);
162 apexArguments.validate();
163 }).hasMessageContaining("pom.xml\" does not exist or is not a directory");
164 final String[] args03 =
165 { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" };
167 apexArguments.parse(args03);
168 apexArguments.validate();
170 final String[] args04 =
171 { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" };
173 apexArguments.parse(args04);
174 apexArguments.validate();