2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.protocol.configuration;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotEquals;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.common.utils.gson.GsonTestUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 class PdpdConfigurationTest {
38 private static final Logger logger = LoggerFactory.getLogger(PdpdConfigurationTest.class);
40 private static final String REQUEST_ID = UUID.randomUUID().toString();
41 private static final String REQUEST_ID2 = UUID.randomUUID().toString();
43 private static final String ENTITY = "entity1";
44 private static final String ENTITY2 = "entity2";
46 private static final String PROPERTY1 = "property1";
47 private static final String PROPERTY2 = "property2";
49 private static final String VALUE1 = "value1";
50 private static final String VALUE2 = "value2";
52 private static final String ARTIFACT = "org.onap.artifact";
53 private static final String GROUPID = "group";
54 private static final String VERSION = "1.0.0";
56 private static final String ARTIFACT2 = "org.onap.artifact2";
57 private static final String GROUPID2 = "group2";
58 private static final String VERSION2 = "1.0.1";
60 private static final String NAME = "name";
61 private static final String OPERATION = "operation";
63 private static final String NAME2 = "name2";
64 private static final String OPERATION2 = "operation2";
69 // Empty constructor test
71 DroolsConfiguration drools = new DroolsConfiguration();
72 drools.set("artifactId", ARTIFACT);
73 drools.set("groupId", GROUPID);
74 drools.set("version", VERSION);
75 drools.set(PROPERTY1, VALUE1);
77 assertEquals(drools, (Object) drools);
78 assertNotEquals(drools, new Object());
80 logger.info("Drools HashCode {}", drools.hashCode());
83 // Constructor with values test get calls
85 DroolsConfiguration drools2 = new DroolsConfiguration(
86 drools.get("artifactId"),
87 drools.get("groupId"),
88 drools.get("version"));
94 drools2.set(PROPERTY1, drools.get(PROPERTY1));
96 assertEquals(drools, drools2);
101 drools2.withArtifactId(ARTIFACT2).withGroupId(GROUPID2).withVersion(VERSION2)
102 .withAdditionalProperty(PROPERTY2, VALUE2);
104 assertNotEquals(drools, drools2);
107 // Test get additional properties
109 assertEquals(1, drools.getAdditionalProperties().size());
114 assertEquals(DroolsConfiguration.NOT_FOUND_VALUE,
115 drools.declaredPropertyOrNotFound(PROPERTY2, DroolsConfiguration.NOT_FOUND_VALUE));
117 logger.info("drools {}", drools);
118 logger.info("drools2 {}", drools2);
121 // Test Controller Default Constructor
123 ControllerConfiguration controller = new ControllerConfiguration();
129 controller.set("name", NAME);
130 controller.set("operation", OPERATION);
131 controller.set("drools", drools);
132 controller.set(PROPERTY1, VALUE1);
134 assertEquals(controller, (Object) controller);
135 assertNotEquals(controller, new Object());
137 logger.info("Controller HashCode {}", controller.hashCode());
140 // Controller Constructor gets
142 ControllerConfiguration controller2 = new ControllerConfiguration(
143 controller.get("name"),
144 controller.get("operation"),
145 controller.get("drools"));
151 controller2.set(PROPERTY1, controller.get(PROPERTY1));
153 assertEquals(controller, controller2);
159 controller2.withDrools(drools2).withName(NAME2)
160 .withOperation(OPERATION2).withAdditionalProperty(PROPERTY2, VALUE2);
162 assertNotEquals(controller, controller2);
165 // Test additional properties
167 assertEquals(1, controller.getAdditionalProperties().size());
172 assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
173 controller.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
178 logger.info("Controller {}", controller);
179 logger.info("Controller2 {}", controller2);
182 // PDP Configuration empty constructor
184 PdpdConfiguration config = new PdpdConfiguration();
190 config.set("requestID", REQUEST_ID);
191 config.set("entity", ENTITY);
192 List<ControllerConfiguration> controllers = new ArrayList<>();
193 controllers.add(controller);
194 config.set("controllers", controllers);
195 config.set(PROPERTY1, VALUE1);
197 assertEquals(config, (Object) config);
198 assertNotEquals(config, new Object());
200 logger.info("Config HashCode {}", config.hashCode());
203 // Test constructor with values
206 PdpdConfiguration config2 = new PdpdConfiguration(
207 config.get("requestID"),
208 config.get("entity"),
209 config.get("controllers"));
215 config2.set(PROPERTY1, config.get(PROPERTY1));
217 assertEquals(config, config2);
222 List<ControllerConfiguration> controllers2 = new ArrayList<>();
223 controllers2.add(controller2);
224 config2.withRequestId(REQUEST_ID2).withEntity(ENTITY2).withController(controllers2);
226 assertNotEquals(config, config2);
229 // Test additional properties
232 assertEquals(1, config.getAdditionalProperties().size());
237 assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
238 config.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
243 logger.info("Config {}", config);
244 logger.info("Config2 {}", config2);
249 void testConstructor() {
250 PdpdConfiguration config = new PdpdConfiguration(REQUEST_ID, ENTITY, null);
251 assertEquals(REQUEST_ID, config.getRequestId());
252 assertEquals(ENTITY, config.getEntity());
256 void testSerialize() {
257 List<ControllerConfiguration> controllers = Arrays.asList(new ControllerConfiguration(NAME, OPERATION, null),
258 new ControllerConfiguration(NAME2, OPERATION2, null));
259 PdpdConfiguration pdpConfig = new PdpdConfiguration(REQUEST_ID, ENTITY, controllers);
261 GsonTestUtils gson = new GsonTestUtils();
263 // ensure jackson and gson give same result
264 gson.compareGson(pdpConfig, PdpdConfigurationTest.class);
266 // ensure we get the same value when decoding
267 PdpdConfiguration config2 = gson.gsonRoundTrip(pdpConfig, PdpdConfiguration.class);
268 assertEquals(stripIdent(pdpConfig.toString()), stripIdent(config2.toString()));
269 assertEquals(pdpConfig, config2);
270 assertEquals(gson.gsonEncode(pdpConfig), gson.gsonEncode(config2));
274 * Object identifiers may change with each execution, so this method is used to strip
275 * the identifier from the text string so that the strings will still match across
278 * @param text text from which to strip the identifier
279 * @return the text, without the identifier
281 private String stripIdent(String text) {
282 return text.replaceAll("@\\w+", "@");