Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-onappf / src / test / java / org / onap / policy / apex / services / onappf / parameters / TestApexStarterParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.services.onappf.parameters;
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 java.io.FileNotFoundException;
28 import org.junit.Test;
29 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
30 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
31 import org.onap.policy.common.utils.coder.CoderException;
32
33 /**
34  * Class to perform unit test of {@link ApexStarterParameterHandler}.
35  *
36  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
37  */
38 public class TestApexStarterParameterHandler {
39
40     @Test
41     public void testParameterHandlerNoParameterFile() throws ApexStarterException {
42         final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
43
44         final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments();
45         emptyArguments.parse(emptyArgumentString);
46
47         try {
48             new ApexStarterParameterHandler().getParameters(emptyArguments);
49             fail("test should throw an exception here");
50         } catch (final Exception e) {
51             assertTrue(e.getCause() instanceof CoderException);
52             assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
53         }
54     }
55
56     @Test
57     public void testParameterHandlerEmptyParameters() throws ApexStarterException {
58         final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
59
60         final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
61         noArguments.parse(noArgumentString);
62
63         try {
64             new ApexStarterParameterHandler().getParameters(noArguments);
65             fail("test should throw an exception here");
66         } catch (final Exception e) {
67             assertTrue(e.getMessage().contains("no parameters found"));
68         }
69     }
70
71     @Test
72     public void testParameterHandlerInvalidParameters() throws ApexStarterException {
73         final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
74
75         final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments();
76         invalidArguments.parse(invalidArgumentString);
77
78         try {
79             new ApexStarterParameterHandler().getParameters(invalidArguments);
80             fail("test should throw an exception here");
81         } catch (final Exception e) {
82             assertTrue(e.getMessage().startsWith("error reading parameters from"));
83             assertTrue(e.getCause() instanceof CoderException);
84         }
85     }
86
87     @Test
88     public void testParameterHandlerNoParameters() throws ApexStarterException {
89         final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
90
91         final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments();
92         noArguments.parse(noArgumentString);
93
94         try {
95             new ApexStarterParameterHandler().getParameters(noArguments);
96         } catch (final Exception e) {
97             assertTrue(e.getMessage().contains("is null"));
98         }
99     }
100
101     @Test
102     public void testApexStarterParameterGroup() throws ApexStarterException {
103         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
104
105         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
106         arguments.parse(apexStarterConfigParameters);
107
108         final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments);
109         assertTrue(arguments.checkSetConfigurationFilePath());
110         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName());
111     }
112
113     @Test
114     public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException {
115         final String[] apexStarterConfigParameters =
116         { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" };
117
118         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
119         arguments.parse(apexStarterConfigParameters);
120
121         try {
122             new ApexStarterParameterHandler().getParameters(arguments);
123             fail("test should throw an exception here");
124         } catch (final Exception e) {
125             assertTrue(e.getMessage().contains(
126                     "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
127         }
128     }
129
130     @Test
131     public void testApexStarterVersion() throws ApexStarterException {
132         final String[] apexStarterConfigParameters = { "-v" };
133         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
134         final String version = arguments.parse(apexStarterConfigParameters);
135         assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service"));
136     }
137
138     @Test
139     public void testApexStarterHelp() throws ApexStarterException {
140         final String[] apexStarterConfigParameters = { "-h" };
141         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
142         final String help = arguments.parse(apexStarterConfigParameters);
143         assertTrue(help.startsWith("usage:"));
144     }
145
146     @Test
147     public void testApexStarterInvalidOption() throws ApexStarterException {
148         final String[] apexStarterConfigParameters = { "-d" };
149         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
150         try {
151             arguments.parse(apexStarterConfigParameters);
152         } catch (final Exception exp) {
153             assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
154         }
155     }
156 }