90c2ef74346becd1a8a82f8c02535a8b43cb07d1
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.services.onappf.parameters;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.FileNotFoundException;
29 import org.junit.Test;
30 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
31 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
32 import org.onap.policy.common.utils.cmd.CommandLineException;
33 import org.onap.policy.common.utils.coder.CoderException;
34
35 /**
36  * Class to perform unit test of {@link ApexStarterParameterHandler}.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class TestApexStarterParameterHandler {
41
42     @Test
43     public void testParameterHandlerNoParameterFile() throws ApexStarterException, CommandLineException {
44         final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
45
46         final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments();
47         emptyArguments.parse(emptyArgumentString);
48
49         assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(emptyArguments))
50             .hasCauseInstanceOf(CoderException.class)
51             .hasRootCauseInstanceOf(FileNotFoundException.class);
52     }
53
54     @Test
55     public void testParameterHandlerEmptyParameters() throws ApexStarterException, CommandLineException {
56         final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
57
58         final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
59         noArguments.parse(noArgumentString);
60
61         assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(noArguments))
62             .hasMessageContaining("no parameters found");
63     }
64
65     @Test
66     public void testParameterHandlerInvalidParameters() throws ApexStarterException, CommandLineException {
67         final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
68
69         final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments();
70         invalidArguments.parse(invalidArgumentString);
71
72         assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(invalidArguments))
73             .hasMessageStartingWith("error reading parameters from")
74             .hasCauseInstanceOf(CoderException.class);
75     }
76
77     @Test
78     public void testParameterHandlerNoParameters() throws ApexStarterException, CommandLineException {
79         final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
80
81         final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
82         noArguments.parse(noArgumentString);
83
84         assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(noArguments))
85             .hasMessageContaining("is null");
86     }
87
88     @Test
89     public void testApexStarterParameterGroup() throws ApexStarterException, CommandLineException {
90         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
91
92         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
93         arguments.parse(apexStarterConfigParameters);
94
95         final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
96         assertTrue(arguments.checkSetConfigurationFilePath());
97         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName());
98     }
99
100     @Test
101     public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException, CommandLineException {
102         final String[] apexStarterConfigParameters =
103         { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
104
105         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
106         arguments.parse(apexStarterConfigParameters);
107
108         assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(arguments))
109             .hasMessageContaining("\"name\" value \" \" INVALID, is blank");
110     }
111
112     @Test
113     public void testApexStarterVersion() throws ApexStarterException, CommandLineException {
114         final String[] apexStarterConfigParameters = { "-v" };
115         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
116         final String version = arguments.parse(apexStarterConfigParameters);
117         assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service"));
118     }
119
120     @Test
121     public void testApexStarterHelp() throws ApexStarterException, CommandLineException {
122         final String[] apexStarterConfigParameters = { "-h" };
123         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
124         final String help = arguments.parse(apexStarterConfigParameters);
125         assertTrue(help.startsWith("usage:"));
126     }
127
128     @Test
129     public void testApexStarterInvalidOption() throws ApexStarterException {
130         final String[] apexStarterConfigParameters = { "-d" };
131         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
132         assertThatThrownBy(() -> arguments.parse(apexStarterConfigParameters))
133             .hasMessageStartingWith("invalid command line arguments specified");
134     }
135 }