APEX standalone support for ToscaPolicy format
[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  *  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.assertThatThrownBy;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.After;
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31
32 /**
33  * Test Apex Command Line Arguments.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public class ApexCommandLineArgumentsTest {
38     @After
39     public void clearRelativeFileRoot() {
40         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
41     }
42
43     @Test
44     public void testCommandLineArguments() throws ApexException {
45         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
46
47         final String[] args00 = {""};
48         apexArguments.parse(args00);
49         assertThatThrownBy(() -> apexArguments.validate())
50             .hasMessage("Tosca Policy file was not specified as an argument");
51
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...]"));
55
56         final String[] args02 = {"-v"};
57         final String result02 = apexArguments.parse(args02);
58         assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));
59
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...]"));
63
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...]"));
67
68         final String[] args05 = {"-a"};
69         assertThatThrownBy(() -> apexArguments.parse(args05))
70             .hasMessage("invalid command line arguments specified : Unrecognized option: -a");
71
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...]"));
75
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]");
79     }
80
81     @Test
82     public void testCommandLineFileParameters() throws ApexException {
83         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
84
85         final String[] args00 = {"-c", "zooby"};
86         assertThatThrownBy(() -> apexArguments.parse(args00))
87             .hasMessage("invalid command line arguments specified : Unrecognized option: -c");
88
89         final String[] args01 = {"-p"};
90         assertThatThrownBy(() -> apexArguments.parse(args01))
91             .hasMessage("invalid command line arguments specified : Missing argument for option: p");
92
93         final String[] args02 = {"-p", "src/test/resources/parameters/goodParams.json"};
94         apexArguments.parse(args02);
95         apexArguments.validate();
96
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");
100
101         final String[] args06 = {"-p", "src/test/resources/parameters/goodParams.json"};
102         apexArguments.parse(args06);
103         apexArguments.validate();
104     }
105
106     @Test
107     public void testCommandLineRelativeRootParameters() throws ApexException {
108         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
109
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");
114
115         final String[] args01 = {"-rfr"};
116         assertThatThrownBy(() -> apexArguments.parse(args01))
117             .hasMessage("invalid command line arguments specified : Missing argument for option: rfr");
118
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");
123
124         final String[] args03 = {"-p", "src/test/resources/parameters/goodParams.json", "-rfr", "target"};
125         apexArguments.parse(args03);
126         apexArguments.validate();
127
128         final String[] args04 =
129             {"-p", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources"};
130         apexArguments.parse(args04);
131         apexArguments.validate();
132
133     }
134 }