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