40375b1f6cd068415b6b53059dd780dd337c50ce
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.main;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
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() {
44         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
45
46         final String[] args00 =
47             { "" };
48         try {
49             apexArguments.parse(args00);
50             apexArguments.validate();
51             fail("Test should throw an exception here");
52         } catch (final ApexException e) {
53             assertEquals("Apex configuration file was not specified as an argument", e.getMessage());
54         }
55
56         final String[] args01 =
57             { "-h" };
58         try {
59             final String result = apexArguments.parse(args01);
60             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
61         } catch (final ApexException e) {
62             e.printStackTrace();
63             fail("Test should not throw an exception");
64         }
65
66         final String[] args02 =
67             { "-v" };
68         try {
69             final String result = apexArguments.parse(args02);
70             assertTrue(result.startsWith("Apex Adaptive Policy Engine"));
71         } catch (final ApexException e) {
72             e.printStackTrace();
73             fail("Test should not throw an exception");
74         }
75
76         final String[] args03 =
77             { "-v", "-h" };
78         try {
79             final String result = apexArguments.parse(args03);
80             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
81         } catch (final ApexException e) {
82             e.printStackTrace();
83             fail("Test should not throw an exception");
84         }
85
86         final String[] args04 =
87             { "-h", "-v" };
88         try {
89             final String result = apexArguments.parse(args04);
90             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
91         } catch (final ApexException e) {
92             e.printStackTrace();
93             fail("Test should not throw an exception");
94         }
95
96         final String[] args05 =
97             { "-a" };
98         try {
99             apexArguments.parse(args05);
100         } catch (final ApexException e) {
101             assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage());
102         }
103
104         final String[] args06 =
105             { "-c", "hello", "-m", "goodbye", "-h", "-v" };
106         try {
107             final String result = apexArguments.parse(args06);
108             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
109         } catch (final ApexException e) {
110             assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage());
111         }
112
113         final String[] args07 =
114             { "-c", "hello", "-m", "goodbye", "-h", "aaa" };
115         try {
116             final String result = apexArguments.parse(args07);
117             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
118         } catch (final ApexException e) {
119             assertEquals("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]",
120                             e.getMessage());
121         }
122     }
123
124     @Test
125     public void testCommandLineFileParameters() {
126         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
127
128         final String[] args00 =
129             { "-c", "zooby" };
130         try {
131             apexArguments.parse(args00);
132             apexArguments.validate();
133             fail("Test should throw an exception here");
134         } catch (final ApexException e) {
135             assertEquals("Apex configuration file \"zooby\" does not exist", e.getMessage());
136         }
137
138         final String[] args01 =
139             { "-c" };
140         try {
141             apexArguments.parse(args01);
142             apexArguments.validate();
143             fail("Test should throw an exception here");
144         } catch (final ApexException e) {
145             assertEquals("invalid command line arguments specified : Missing argument for option: c", e.getMessage());
146         }
147
148         final String[] args02 =
149             { "-c", "src/test/resources/parameters/goodParams.json" };
150         try {
151             apexArguments.parse(args02);
152             apexArguments.validate();
153         } catch (final ApexException e) {
154             e.printStackTrace();
155             fail("Test should not throw an exception");
156         }
157
158         final String[] args03 =
159             { "-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby" };
160         try {
161             apexArguments.parse(args03);
162             apexArguments.validate();
163             fail("Test should throw an exception here");
164         } catch (final ApexException e) {
165             assertEquals("Apex model file \"zooby\" does not exist", e.getMessage());
166         }
167
168         final String[] args04 =
169             { "-m" };
170         try {
171             apexArguments.parse(args04);
172             apexArguments.validate();
173             fail("Test should throw an exception here");
174         } catch (final ApexException e) {
175             assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage());
176         }
177
178         final String[] args05 =
179             { "-c", "src/test/resources/parameters/goodParams.json", "-m" };
180         try {
181             apexArguments.parse(args05);
182             apexArguments.validate();
183             fail("Test should throw an exception here");
184         } catch (final ApexException e) {
185             assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage());
186         }
187
188         final String[] args06 =
189             { "-c", "src/test/resources/parameters/goodParams.json", "-m",
190                 "src/test/resources/main/DummyModelFile.json" };
191         try {
192             apexArguments.parse(args06);
193             apexArguments.validate();
194         } catch (final ApexException e) {
195             e.printStackTrace();
196             fail("Test should not throw an exception");
197         }
198
199         final String[] args07 =
200             { "-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json" };
201         try {
202             apexArguments.parse(args07);
203             apexArguments.validate();
204         } catch (final ApexException e) {
205             e.printStackTrace();
206             fail("Test should not throw an exception");
207         }
208     }
209
210     @Test
211     public void testCommandLineRelativeRootParameters() {
212         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
213
214         final String[] args00 =
215             { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" };
216         try {
217             apexArguments.parse(args00);
218             apexArguments.validate();
219             fail("Test should throw an exception here");
220         } catch (final ApexException e) {
221             assertTrue(e.getMessage().contains("zooby\" does not exist or is not a directory"));
222         }
223
224         final String[] args01 =
225             { "-rfr" };
226         try {
227             apexArguments.parse(args01);
228             apexArguments.validate();
229             fail("Test should throw an exception here");
230         } catch (final ApexException e) {
231             assertEquals("invalid command line arguments specified : Missing argument for option: rfr", e.getMessage());
232         }
233
234         final String[] args02 =
235             { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" };
236         try {
237             apexArguments.parse(args02);
238             apexArguments.validate();
239             fail("Test should throw an exception here");
240         } catch (final ApexException e) {
241             assertTrue(e.getMessage().contains("pom.xml\" does not exist or is not a directory"));
242         }
243
244         final String[] args03 =
245             { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" };
246         try {
247             apexArguments.parse(args03);
248             apexArguments.validate();
249         } catch (final ApexException e) {
250             fail("Test should not throw an exception here");
251         }
252
253         final String[] args04 =
254             { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" };
255         try {
256             apexArguments.parse(args04);
257             apexArguments.validate();
258         } catch (final ApexException e) {
259             fail("Test should not throw an exception here");
260         }
261     }
262 }