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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.service.engine.parameters;
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;
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;
41 * Test the ApexParameters class.
43 public class ApexParametersTest {
46 public void testJavaPropertiesOk() throws ParameterException {
47 final String[] args = {"-p", "src/test/resources/parameters/javaPropertiesOK.json"};
48 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
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]);
60 public void testJavaPropertiesEmpty() throws ParameterException {
61 final String[] args = {"-p", "src/test/resources/parameters/javaPropertiesEmpty.json"};
62 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
64 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
65 assertFalse(parameters.checkJavaPropertiesSet());
70 public void testJavaPropertiesBad() throws ParameterException {
71 final String[] args = {"-p", "src/test/resources/parameters/javaPropertiesBad.json"};
72 final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
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");
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);
88 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
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\"}");
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);
103 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
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\"}");
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);
118 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
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\"}");
128 public void testGettersSetters() {
129 ApexParameters pars = new ApexParameters();
132 pars.setEngineServiceParameters(null);
133 assertNull(pars.getEngineServiceParameters());
135 pars.setEventInputParameters(null);
136 assertNull(pars.getEventInputParameters());
138 pars.setEventOutputParameters(null);
139 assertNull(pars.getEventOutputParameters());
141 assertFalse(pars.checkJavaPropertiesSet());
143 pars.setName("parName");
144 assertEquals("parName", pars.getName());