b5b68c9fcd549a01a55a1a61ae9bebe01540d17c
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.service.engine.main;
24
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;
28
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;
33
34 /**
35  * Test Apex Command Line Arguments.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class ApexCommandLineArgumentsTest {
40     @After
41     public void clearRelativeFileRoot() {
42         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
43     }
44
45     @Test
46     public void testCommandLineArguments() throws ApexException, CommandLineException {
47         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
48
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...]"));
52
53         final String[] args02 = {"-v"};
54         final String result02 = apexArguments.parse(args02);
55         assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));
56
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...]"));
60
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...]"));
64
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...]"));
68     }
69
70     @Test
71     public void testCommandLineArgumentsExceptions() throws ApexException, CommandLineException {
72         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
73
74         final String[] args00 = {""};
75         apexArguments.parse(args00);
76         assertThatThrownBy(() -> apexArguments.validateInputFiles())
77                 .hasMessage("Tosca Policy file was not specified as an argument");
78
79         final String[] args05 = {"-a"};
80         assertThatThrownBy(() -> apexArguments.parse(args05)).hasMessage("invalid command line arguments specified")
81                 .hasRootCauseMessage("Unrecognized option: -a");
82
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]");
86     }
87
88     @Test
89     public void testCommandLineFileParameters() throws ApexException, CommandLineException {
90         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
91
92         final String[] args02 = {"-p", "src/test/resources/parameters/goodParams.json"};
93         apexArguments.parse(args02);
94         assertThatCode(() -> apexArguments.validateInputFiles()).doesNotThrowAnyException();
95     }
96
97     @Test
98     public void testCommandLineFileParametersExceptions() throws ApexException, CommandLineException {
99         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
100
101         final String[] args00 = {"-c", "zooby"};
102         assertThatThrownBy(() -> apexArguments.parse(args00)).hasMessage("invalid command line arguments specified")
103                 .hasRootCauseMessage("Unrecognized option: -c");
104
105         final String[] args01 = {"-p"};
106         assertThatThrownBy(() -> apexArguments.parse(args01)).hasMessage("invalid command line arguments specified")
107                 .hasRootCauseMessage("Missing argument for option: p");
108
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");
112     }
113
114     @Test
115     public void testCommandLineRelativeRootParameters() throws ApexException, CommandLineException {
116         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
117
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");
122
123         final String[] args01 = {"-rfr"};
124         assertThatThrownBy(() -> apexArguments.parse(args01)).hasMessage("invalid command line arguments specified")
125                 .hasRootCauseMessage("Missing argument for option: rfr");
126
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");
131
132         final String[] args03 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "target"};
133         apexArguments.parse(args03);
134         assertThatCode(() -> apexArguments.validateInputFiles()).doesNotThrowAnyException();
135
136         final String[] args04 = {"-p", "parameters/goodParamsRelative.json", "-rfr", "src/test/resources"};
137         apexArguments.parse(args04);
138         assertThatCode(() -> apexArguments.validateInputFiles()).doesNotThrowAnyException();
139
140     }
141 }