ff97eaec2e40cff14d1697beda4be90edab9517e
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / parameters / ApexParametersTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
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.service.engine.parameters;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
33 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
34 import org.onap.policy.apex.service.parameters.ApexParameters;
35 import org.onap.policy.common.parameters.ParameterException;
36
37 /**
38  * Test the ApexParameters class.
39  */
40 public class ApexParametersTest {
41
42     @Test
43     public void testJavaPropertiesOk() throws ParameterException {
44         final String[] args = {"-c", "src/test/resources/parameters/javaPropertiesOK.json"};
45         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
46
47         try {
48             ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
49             assertTrue(parameters.checkJavaPropertiesSet());
50             assertEquals("property0", parameters.getJavaProperties()[0][0]);
51             assertEquals("property0Value", parameters.getJavaProperties()[0][1]);
52             assertEquals("property1", parameters.getJavaProperties()[1][0]);
53             assertEquals("property1Value", parameters.getJavaProperties()[1][1]);
54         } catch (final ParameterException e) {
55             fail("This test should not throw an exception");
56         }
57     }
58
59     @Test
60     public void testJavaPropertiesEmpty() throws ParameterException {
61         final String[] args = {"-c", "src/test/resources/parameters/javaPropertiesEmpty.json"};
62         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
63
64         try {
65             ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
66             assertFalse(parameters.checkJavaPropertiesSet());
67         } catch (final ParameterException pe) {
68             fail("This test should not throw an exception");
69         }
70     }
71
72     @Test
73     public void testJavaPropertiesBad() throws ParameterException {
74         final String[] args = {"-c", "src/test/resources/parameters/javaPropertiesBad.json"};
75         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
76
77         try {
78             new ApexParameterHandler().getParameters(arguments);
79             fail("This test should throw an exception");
80         } catch (final ParameterException pe) {
81             assertTrue(pe.getMessage().contains("java properties array entries must have one key and one value"));
82             assertTrue(pe.getMessage().contains("java properties key is null or blank"));
83             assertTrue(pe.getMessage().contains("java properties value is null or blank"));
84             assertTrue(pe.getMessage().contains("java properties array entry is null"));
85         }
86     }
87
88     @Test
89     public void testGettersSetters() {
90         ApexParameters pars = new ApexParameters();
91         assertNotNull(pars);
92
93         pars.setEngineServiceParameters(null);
94         assertNull(pars.getEngineServiceParameters());
95
96         pars.setEventInputParameters(null);
97         assertNull(pars.getEventInputParameters());
98
99         pars.setEventOutputParameters(null);
100         assertNull(pars.getEventOutputParameters());
101
102         assertFalse(pars.checkJavaPropertiesSet());
103
104         pars.setName("parName");
105         assertEquals("parName", pars.getName());
106     }
107 }