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