2b8bd6771272c5fd3cb0495a83589afc6fa65874
[policy/apex-pdp.git] /
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.Test;
28 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
29
30 /**
31  * Test Apex Command Line Arguments.
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  */
34 public class TestApexCommandLineArguments {
35
36     @Test
37     public void testCommandLineArguments() {
38         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
39
40         final String[] args00 = {""};
41         try {
42             apexArguments.parse(args00);
43             apexArguments.validate();
44             fail("Test should throw an exception here");
45         } catch (final ApexException e) {
46             assertEquals("Apex configuration file was not specified as an argument", e.getMessage());
47         }
48
49         final String[] args01 = {"-h"};
50         try {
51             final String result = apexArguments.parse(args01);
52             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
53         } catch (final ApexException e) {
54             e.printStackTrace();
55             fail("Test should not throw an exception");
56         }
57
58         final String[] args02 = {"-v"};
59         try {
60             final String result = apexArguments.parse(args02);
61             assertTrue(result.startsWith("Apex Adaptive Policy Engine"));
62         } catch (final ApexException e) {
63             e.printStackTrace();
64             fail("Test should not throw an exception");
65         }
66
67         final String[] args03 = {"-v", "-h"};
68         try {
69             final String result = apexArguments.parse(args03);
70             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
71         } catch (final ApexException e) {
72             e.printStackTrace();
73             fail("Test should not throw an exception");
74         }
75
76         final String[] args04 = {"-h", "-v"};
77         try {
78             final String result = apexArguments.parse(args04);
79             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
80         } catch (final ApexException e) {
81             e.printStackTrace();
82             fail("Test should not throw an exception");
83         }
84
85         final String[] args05 = {"-a"};
86         try {
87             apexArguments.parse(args05);
88         } catch (final ApexException e) {
89             assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage());
90         }
91
92         final String[] args06 = {"-c", "hello", "-m", "goodbye", "-h", "-v"};
93         try {
94             final String result = apexArguments.parse(args06);
95             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
96         } catch (final ApexException e) {
97             assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage());
98         }
99
100         final String[] args07 = {"-c", "hello", "-m", "goodbye", "-h", "aaa"};
101         try {
102             final String result = apexArguments.parse(args07);
103             assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
104         } catch (final ApexException e) {
105             assertEquals("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]",
106                     e.getMessage());
107         }
108     }
109
110     @Test
111     public void testCommandLineFileParameters() {
112         final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();
113
114         final String[] args00 = {"-c", "zooby"};
115         try {
116             apexArguments.parse(args00);
117             apexArguments.validate();
118             fail("Test should throw an exception here");
119         } catch (final ApexException e) {
120             assertEquals("Apex configuration file \"zooby\" does not exist", e.getMessage());
121         }
122
123         final String[] args01 = {"-c"};
124         try {
125             apexArguments.parse(args01);
126             apexArguments.validate();
127             fail("Test should throw an exception here");
128         } catch (final ApexException e) {
129             assertEquals("invalid command line arguments specified : Missing argument for option: c", e.getMessage());
130         }
131
132         final String[] args02 = {"-c", "src/test/resources/parameters/goodParams.json"};
133         try {
134             apexArguments.parse(args02);
135             apexArguments.validate();
136         } catch (final ApexException e) {
137             e.printStackTrace();
138             fail("Test should not throw an exception");
139         }
140
141         final String[] args03 = {"-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby"};
142         try {
143             apexArguments.parse(args03);
144             apexArguments.validate();
145             fail("Test should throw an exception here");
146         } catch (final ApexException e) {
147             assertEquals("Apex model file \"zooby\" does not exist", e.getMessage());
148         }
149
150         final String[] args04 = {"-m"};
151         try {
152             apexArguments.parse(args04);
153             apexArguments.validate();
154             fail("Test should throw an exception here");
155         } catch (final ApexException e) {
156             assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage());
157         }
158
159         final String[] args05 = {"-c", "src/test/resources/parameters/goodParams.json", "-m"};
160         try {
161             apexArguments.parse(args05);
162             apexArguments.validate();
163             fail("Test should throw an exception here");
164         } catch (final ApexException e) {
165             assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage());
166         }
167
168         final String[] args06 = {"-c", "src/test/resources/parameters/goodParams.json", "-m",
169                                  "src/test/resources/main/DummyModelFile.json"};
170         try {
171             apexArguments.parse(args06);
172             apexArguments.validate();
173         } catch (final ApexException e) {
174             e.printStackTrace();
175             fail("Test should not throw an exception");
176         }
177
178         final String[] args07 = {"-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json"};
179         try {
180             apexArguments.parse(args07);
181             apexArguments.validate();
182         } catch (final ApexException e) {
183             e.printStackTrace();
184             fail("Test should not throw an exception");
185         }
186     }
187 }