Replace try/catch with assertj
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / main / ApexCommandLineArgumentsTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.engine.main;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
26
27 import org.junit.After;
28 import org.junit.Test;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30
31 /**
32  * Test Apex Command Line Arguments.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class ApexCommandLineArgumentsTest {
37     @After
38     public void clearRelativeFileRoot() {
39         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
40     }
41
42     @Test
43     public void testCommandLineArguments() throws ApexException {
44         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
45
46         final String[] args00 =
47             { "" };
48         assertThatThrownBy(() -> {
49             apexArguments.parse(args00);
50             apexArguments.validate();
51         }).hasMessage("Apex configuration file was not specified as an argument");
52         final String[] args01 =
53             { "-h" };
54
55         final String result = apexArguments.parse(args01);
56         assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
57
58         final String[] args02 =
59             { "-v" };
60         final String result02 = apexArguments.parse(args02);
61         assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));
62
63         final String[] args03 =
64             { "-v", "-h" };
65
66         final String result03 = apexArguments.parse(args03);
67         assertTrue(result03.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
68
69         final String[] args04 =
70             { "-h", "-v" };
71
72         final String result04 = apexArguments.parse(args04);
73         assertTrue(result04.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
74
75         final String[] args05 =
76             { "-a" };
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...]"));
83
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]");
88     }
89
90     @Test
91     public void testCommandLineFileParameters() throws ApexException {
92         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
93
94         final String[] args00 =
95             { "-c", "zooby" };
96         assertThatThrownBy(() -> {
97             apexArguments.parse(args00);
98             apexArguments.validate();
99         }).hasMessage("Apex configuration file \"zooby\" does not exist");
100         final String[] args01 =
101             { "-c" };
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();
110
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 =
118             { "-m" };
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();
134
135         final String[] args07 =
136             { "-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json" };
137
138         apexArguments.parse(args07);
139         apexArguments.validate();
140     }
141
142     @Test
143     public void testCommandLineRelativeRootParameters() throws ApexException {
144         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
145
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 =
153             { "-rfr" };
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" };
166
167         apexArguments.parse(args03);
168         apexArguments.validate();
169
170         final String[] args04 =
171             { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" };
172
173         apexArguments.parse(args04);
174         apexArguments.validate();
175
176     }
177 }