summaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java
blob: 09fbe91fe16ad6487aced1aabb0603d8b13d99f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.service.engine.main;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;

/**
 * Test Apex Command Line Arguments.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ApexCommandLineArgumentsTest {
    @After
    public void clearRelativeFileRoot() {
        System.clearProperty("APEX_RELATIVE_FILE_ROOT");
    }

    @Test
    public void testCommandLineArguments() throws ApexException {
        final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();

        final String[] args00 =
            { "" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args00);
            apexArguments.validate();
        }).hasMessage("Apex configuration file was not specified as an argument");
        final String[] args01 =
            { "-h" };

        final String result = apexArguments.parse(args01);
        assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));

        final String[] args02 =
            { "-v" };
        final String result02 = apexArguments.parse(args02);
        assertTrue(result02.startsWith("Apex Adaptive Policy Engine"));

        final String[] args03 =
            { "-v", "-h" };

        final String result03 = apexArguments.parse(args03);
        assertTrue(result03.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));

        final String[] args04 =
            { "-h", "-v" };

        final String result04 = apexArguments.parse(args04);
        assertTrue(result04.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));

        final String[] args05 =
            { "-a" };
        assertThatThrownBy(() -> apexArguments.parse(args05))
            .hasMessage("invalid command line arguments specified : Unrecognized option: -a");
        final String[] args06 =
            { "-c", "hello", "-m", "goodbye", "-h", "-v" };
        final String result06 = apexArguments.parse(args06);
        assertTrue(result06.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));

        final String[] args07 =
            { "-c", "hello", "-m", "goodbye", "-h", "aaa" };
        assertThatThrownBy(() -> apexArguments.parse(args07))
            .hasMessage("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]");
    }

    @Test
    public void testCommandLineFileParameters() throws ApexException {
        final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();

        final String[] args00 =
            { "-c", "zooby" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args00);
            apexArguments.validate();
        }).hasMessage("Apex configuration file \"zooby\" does not exist");
        final String[] args01 =
            { "-c" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args01);
            apexArguments.validate();
        }).hasMessage("invalid command line arguments specified : Missing argument for option: c");
        final String[] args02 =
            { "-c", "src/test/resources/parameters/goodParams.json" };
        apexArguments.parse(args02);
        apexArguments.validate();

        final String[] args03 =
            { "-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args03);
            apexArguments.validate();
        }).hasMessage("Apex model file \"zooby\" does not exist");
        final String[] args04 =
            { "-m" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args04);
            apexArguments.validate();
        }).hasMessage("invalid command line arguments specified : Missing argument for option: m");
        final String[] args05 =
            { "-c", "src/test/resources/parameters/goodParams.json", "-m" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args05);
            apexArguments.validate();
        }).hasMessage("invalid command line arguments specified : Missing argument for option: m");
        final String[] args06 =
            { "-c", "src/test/resources/parameters/goodParams.json", "-m",
                "src/test/resources/main/DummyModelFile.json" };
        apexArguments.parse(args06);
        apexArguments.validate();

        final String[] args07 =
            { "-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json" };

        apexArguments.parse(args07);
        apexArguments.validate();
    }

    @Test
    public void testCommandLineRelativeRootParameters() throws ApexException {
        final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments();

        final String[] args00 =
            { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args00);
            apexArguments.validate();
        }).hasMessageContaining("zooby\" does not exist or is not a directory");
        final String[] args01 =
            { "-rfr" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args01);
            apexArguments.validate();
        }).hasMessage("invalid command line arguments specified : Missing argument for option: rfr");
        final String[] args02 =
            { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" };
        assertThatThrownBy(() -> {
            apexArguments.parse(args02);
            apexArguments.validate();
        }).hasMessageContaining("pom.xml\" does not exist or is not a directory");
        final String[] args03 =
            { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" };

        apexArguments.parse(args03);
        apexArguments.validate();

        final String[] args04 =
            { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" };

        apexArguments.parse(args04);
        apexArguments.validate();

    }
}