dd76374d8576d8703ee29a1529ac942bee20ddae
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020,2022 Nordix Foundation.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.apex.service.engine.parameters;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33
34 import org.junit.Test;
35 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
36 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
37 import org.onap.policy.apex.service.parameters.ApexParameters;
38 import org.onap.policy.common.parameters.ParameterException;
39
40 /**
41  * Test the ApexParameters class.
42  */
43 public class ApexParametersTest {
44
45     @Test
46     public void testJavaPropertiesOk() throws ParameterException {
47         final String[] args = {"-p", "src/test/resources/parameters/javaPropertiesOK.json"};
48         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
49
50         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
51         assertTrue(parameters.checkJavaPropertiesSet());
52         assertEquals("property0", parameters.getJavaProperties()[0][0]);
53         assertEquals("property0Value", parameters.getJavaProperties()[0][1]);
54         assertEquals("property1", parameters.getJavaProperties()[1][0]);
55         assertEquals("property1Value", parameters.getJavaProperties()[1][1]);
56
57     }
58
59     @Test
60     public void testJavaPropertiesEmpty() throws ParameterException {
61         final String[] args = {"-p", "src/test/resources/parameters/javaPropertiesEmpty.json"};
62         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
63
64         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
65         assertFalse(parameters.checkJavaPropertiesSet());
66
67     }
68
69     @Test
70     public void testJavaPropertiesBad() throws ParameterException {
71         final String[] args = {"-p", "src/test/resources/parameters/javaPropertiesBad.json"};
72         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
73
74         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
75             .hasMessageContaining("\"javaProperties\"")
76             .hasMessageContaining("entry 0", "entry 1", "entry 2", "entry 3", "entry 4", "entry 5")
77             .hasMessageContaining("must have one key and one value")
78             .hasMessageContaining("\"key\" value \"null\" INVALID, is blank")
79             .hasMessageContaining("\"value\" value \"null\" INVALID, is blank");
80     }
81
82     @Test
83     public void testPolicyModelFromMetadata() throws ParameterException {
84         // Policy Models provided only in metadata.
85         final String[] args = {"-p", "src/test/resources/parameters/policyModelFromMetadata.json"};
86         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
87
88         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
89
90         assertThat(parameters.getEngineServiceParameters().getPolicyModel()).isNotEmpty();
91         assertThat(parameters.getEngineServiceParameters().getPolicyModel())
92             .contains("{\"key\":{\"name\":\"dummy key1 provided in metadata\",\"version\":\"0.0.1\"},\"keyInformation\""
93                 + ":{\"key\":{\"name\":\"dummy key2 provided in metadata\",\"version\":\"0.0.1\"}},"
94                 + "\"threshold\":3.15,\"state\":\"passive\"}");
95     }
96
97     @Test
98     public void testPolicyModelFromProperties() throws ParameterException {
99         // Policy models provided in properties under EngineServiceParameters for backward compatibility
100         final String[] args = {"-p", "src/test/resources/parameters/policyModelFromProperties.json"};
101         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
102
103         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
104
105         assertThat(parameters.getEngineServiceParameters().getPolicyModel()).isNotEmpty();
106         assertThat(parameters.getEngineServiceParameters().getPolicyModel())
107             .contains("{\"key\":{\"name\":\"dummy key1 provided in properties\",\"version\":\"0.0.1\"},"
108                 + "\"keyInformation\":{\"key\":{\"name\":\"dummy key2 provided in properties\","
109                 + "\"version\":\"0.0.1\"}},\"threshold\":3.15,\"state\":\"passive\"}");
110     }
111
112     @Test
113     public void testPolicyModelFromPropertiesAndMetadata() throws ParameterException {
114         // Policy models provided in both properties and in metadata. policyModels in metadata takes precedence
115         final String[] args = {"-p", "src/test/resources/parameters/policyModelMultiple.json"};
116         final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
117
118         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
119
120         assertThat(parameters.getEngineServiceParameters().getPolicyModel()).isNotEmpty();
121         assertThat(parameters.getEngineServiceParameters().getPolicyModel())
122             .contains("{\"key\":{\"name\":\"dummy key1 provided in metadata\",\"version\":\"0.0.1\"},"
123                 + "\"keyInformation\":{\"key\":{\"name\":\"dummy key2 provided in metadata\","
124                 + "\"version\":\"0.0.1\"}},\"threshold\":3.15,\"state\":\"passive\"}");
125     }
126
127     @Test
128     public void testGettersSetters() {
129         ApexParameters pars = new ApexParameters();
130         assertNotNull(pars);
131
132         pars.setEngineServiceParameters(null);
133         assertNull(pars.getEngineServiceParameters());
134
135         pars.setEventInputParameters(null);
136         assertNull(pars.getEventInputParameters());
137
138         pars.setEventOutputParameters(null);
139         assertNull(pars.getEventOutputParameters());
140
141         assertFalse(pars.checkJavaPropertiesSet());
142
143         pars.setName("parName");
144         assertEquals("parName", pars.getName());
145     }
146 }