2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.protocol.configuration;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.UUID;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.gson.GsonTestUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class PdpdConfigurationTest {
39 private static final Logger logger = LoggerFactory.getLogger(PdpdConfigurationTest.class);
41 private static final String REQUEST_ID = UUID.randomUUID().toString();
42 private static final String REQUEST_ID2 = UUID.randomUUID().toString();
44 private static final String ENTITY = "entity1";
45 private static final String ENTITY2 = "entity2";
47 private static final String PROPERTY1 = "property1";
48 private static final String PROPERTY2 = "property2";
50 private static final String VALUE1 = "value1";
51 private static final String VALUE2 = "value2";
53 private static final String ARTIFACT = "org.onap.artifact";
54 private static final String GROUPID = "group";
55 private static final String VERSION = "1.0.0";
57 private static final String ARTIFACT2 = "org.onap.artifact2";
58 private static final String GROUPID2 = "group2";
59 private static final String VERSION2 = "1.0.1";
61 private static final String NAME = "name";
62 private static final String OPERATION = "operation";
64 private static final String NAME2 = "name2";
65 private static final String OPERATION2 = "operation2";
70 // Empty constructor test
72 DroolsConfiguration drools = new DroolsConfiguration();
73 drools.set("artifactId", ARTIFACT);
74 drools.set("groupId", GROUPID);
75 drools.set("version", VERSION);
76 drools.set(PROPERTY1, VALUE1);
78 assertTrue(drools.equals(drools));
79 assertFalse(drools.equals(new Object()));
81 logger.info("Drools HashCode {}", drools.hashCode());
84 // Constructor with values test get calls
86 DroolsConfiguration drools2 = new DroolsConfiguration(
87 drools.get("artifactId"),
88 drools.get("groupId"),
89 drools.get("version"));
95 drools2.set(PROPERTY1, drools.get(PROPERTY1));
97 assertTrue(drools.equals(drools2));
102 drools2.withArtifactId(ARTIFACT2).withGroupId(GROUPID2).withVersion(VERSION2)
103 .withAdditionalProperty(PROPERTY2, VALUE2);
105 assertFalse(drools.equals(drools2));
108 // Test get additional properties
110 assertEquals(1, drools.getAdditionalProperties().size());
115 assertEquals(DroolsConfiguration.NOT_FOUND_VALUE,
116 drools.declaredPropertyOrNotFound(PROPERTY2, DroolsConfiguration.NOT_FOUND_VALUE));
118 logger.info("drools {}", drools);
119 logger.info("drools2 {}", drools2);
122 // Test Controller Default Constructor
124 ControllerConfiguration controller = new ControllerConfiguration();
130 controller.set("name", NAME);
131 controller.set("operation", OPERATION);
132 controller.set("drools", drools);
133 controller.set(PROPERTY1, VALUE1);
135 assertTrue(controller.equals(controller));
136 assertFalse(controller.equals(new Object()));
138 logger.info("Controller HashCode {}", controller.hashCode());
141 // Controller Constructor gets
143 ControllerConfiguration controller2 = new ControllerConfiguration(
144 controller.get("name"),
145 controller.get("operation"),
146 controller.get("drools"));
152 controller2.set(PROPERTY1, controller.get(PROPERTY1));
154 assertTrue(controller.equals(controller2));
160 controller2.withDrools(drools2).withName(NAME2)
161 .withOperation(OPERATION2).withAdditionalProperty(PROPERTY2, VALUE2);
163 assertFalse(controller.equals(controller2));
166 // Test additional properties
168 assertEquals(1, controller.getAdditionalProperties().size());
173 assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
174 controller.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
179 logger.info("Controller {}", controller);
180 logger.info("Controller2 {}", controller2);
183 // PDP Configuration empty constructor
185 PdpdConfiguration config = new PdpdConfiguration();
191 config.set("requestID", REQUEST_ID);
192 config.set("entity", ENTITY);
193 List<ControllerConfiguration> controllers = new ArrayList<>();
194 controllers.add(controller);
195 config.set("controllers", controllers);
196 config.set(PROPERTY1, VALUE1);
198 assertTrue(config.equals(config));
199 assertFalse(config.equals(new Object()));
201 logger.info("Config HashCode {}", config.hashCode());
204 // Test constructor with values
207 PdpdConfiguration config2 = new PdpdConfiguration(
208 config.get("requestID"),
209 config.get("entity"),
210 config.get("controllers"));
216 config2.set(PROPERTY1, config.get(PROPERTY1));
218 assertTrue(config.equals(config2));
223 List<ControllerConfiguration> controllers2 = new ArrayList<>();
224 controllers2.add(controller2);
225 config2.withRequestId(REQUEST_ID2).withEntity(ENTITY2).withController(controllers2);
227 assertFalse(config.equals(config2));
230 // Test additional properties
233 assertEquals(1, config.getAdditionalProperties().size());
238 assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
239 config.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
244 logger.info("Config {}", config);
245 logger.info("Config2 {}", config2);
250 public void testConstructor() {
251 PdpdConfiguration config = new PdpdConfiguration(REQUEST_ID, ENTITY, null);
252 assertEquals(REQUEST_ID, config.getRequestId());
253 assertEquals(ENTITY, config.getEntity());
257 public void testSerialize() throws IOException {
258 List<ControllerConfiguration> controllers = Arrays.asList(new ControllerConfiguration(NAME, OPERATION, null),
259 new ControllerConfiguration(NAME2, OPERATION2, null));
260 PdpdConfiguration pdpConfig = new PdpdConfiguration(REQUEST_ID, ENTITY, controllers);
262 GsonTestUtils gson = new GsonTestUtils();
264 // ensure jackson and gson give same result
265 gson.compareGson(pdpConfig, PdpdConfigurationTest.class);
267 // ensure we get the same value when decoding
268 PdpdConfiguration config2 = gson.gsonRoundTrip(pdpConfig, PdpdConfiguration.class);
269 assertEquals(stripIdent(pdpConfig.toString()), stripIdent(config2.toString()));
270 assertEquals(pdpConfig, config2);
271 assertEquals(gson.gsonEncode(pdpConfig), gson.gsonEncode(config2));
275 * Object identifiers may change with each execution, so this method is used to strip
276 * the identifier from the text string so that the strings will still match across
279 * @param text text from which to strip the identifier
280 * @return the text, without the identifier
282 private String stripIdent(String text) {
283 return text.replaceAll("@\\w+", "@");