Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-management / src / test / java / org / onap / policy / drools / protocol / configuration / PdpdConfigurationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.protocol.configuration;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotEquals;
26
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;
35
36 class PdpdConfigurationTest {
37
38     private static final Logger logger = LoggerFactory.getLogger(PdpdConfigurationTest.class);
39
40     private static final String REQUEST_ID = UUID.randomUUID().toString();
41     private static final String REQUEST_ID2 = UUID.randomUUID().toString();
42
43     private static final String ENTITY = "entity1";
44     private static final String ENTITY2 = "entity2";
45
46     private static final String PROPERTY1 = "property1";
47     private static final String PROPERTY2 = "property2";
48
49     private static final String VALUE1 = "value1";
50     private static final String VALUE2 = "value2";
51
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";
55
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";
59
60     private static final String NAME = "name";
61     private static final String OPERATION = "operation";
62
63     private static final String NAME2 = "name2";
64     private static final String OPERATION2 = "operation2";
65
66     @Test
67     void test() {
68         //
69         // Empty constructor test
70         //
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);
76
77         assertEquals(drools, (Object) drools);
78         assertNotEquals(drools, new Object());
79
80         logger.info("Drools HashCode {}", drools.hashCode());
81
82         //
83         // Constructor with values test get calls
84         //
85         DroolsConfiguration drools2 = new DroolsConfiguration(
86                 drools.get("artifactId"),
87                 drools.get("groupId"),
88                 drools.get("version"));
89
90         //
91         // get Property
92         //
93
94         drools2.set(PROPERTY1, drools.get(PROPERTY1));
95
96         assertEquals(drools, drools2);
97
98         //
99         // with methods
100         //
101         drools2.withArtifactId(ARTIFACT2).withGroupId(GROUPID2).withVersion(VERSION2)
102             .withAdditionalProperty(PROPERTY2, VALUE2);
103
104         assertNotEquals(drools, drools2);
105
106         //
107         // Test get additional properties
108         //
109         assertEquals(1, drools.getAdditionalProperties().size());
110
111         //
112         // Test Not found
113         //
114         assertEquals(DroolsConfiguration.NOT_FOUND_VALUE,
115                 drools.declaredPropertyOrNotFound(PROPERTY2, DroolsConfiguration.NOT_FOUND_VALUE));
116
117         logger.info("drools {}", drools);
118         logger.info("drools2 {}", drools2);
119
120         //
121         // Test Controller Default Constructor
122         //
123         ControllerConfiguration controller = new ControllerConfiguration();
124
125         //
126         // Test set
127         //
128
129         controller.set("name", NAME);
130         controller.set("operation", OPERATION);
131         controller.set("drools", drools);
132         controller.set(PROPERTY1, VALUE1);
133
134         assertEquals(controller, (Object) controller);
135         assertNotEquals(controller, new Object());
136
137         logger.info("Controller HashCode {}", controller.hashCode());
138
139         //
140         // Controller Constructor gets
141         //
142         ControllerConfiguration controller2 = new ControllerConfiguration(
143                 controller.get("name"),
144                 controller.get("operation"),
145                 controller.get("drools"));
146
147         //
148         // Test get property
149         //
150
151         controller2.set(PROPERTY1, controller.get(PROPERTY1));
152
153         assertEquals(controller, controller2);
154
155         //
156         // test with methods
157         //
158
159         controller2.withDrools(drools2).withName(NAME2)
160             .withOperation(OPERATION2).withAdditionalProperty(PROPERTY2, VALUE2);
161
162         assertNotEquals(controller, controller2);
163
164         //
165         // Test additional properties
166         //
167         assertEquals(1, controller.getAdditionalProperties().size());
168
169         //
170         // Not found
171         //
172         assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
173                 controller.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
174
175         //
176         // toString
177         //
178         logger.info("Controller {}", controller);
179         logger.info("Controller2 {}", controller2);
180
181         //
182         // PDP Configuration empty constructor
183         //
184         PdpdConfiguration config = new PdpdConfiguration();
185
186         //
187         // Test set
188         //
189
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);
196
197         assertEquals(config, (Object) config);
198         assertNotEquals(config, new Object());
199
200         logger.info("Config HashCode {}", config.hashCode());
201
202         //
203         // Test constructor with values
204         //
205
206         PdpdConfiguration config2 = new PdpdConfiguration(
207                 config.get("requestID"),
208                 config.get("entity"),
209                 config.get("controllers"));
210
211         //
212         // Test set
213         //
214
215         config2.set(PROPERTY1, config.get(PROPERTY1));
216
217         assertEquals(config, config2);
218
219         //
220         // Test with methods
221         //
222         List<ControllerConfiguration> controllers2 = new ArrayList<>();
223         controllers2.add(controller2);
224         config2.withRequestId(REQUEST_ID2).withEntity(ENTITY2).withController(controllers2);
225
226         assertNotEquals(config, config2);
227
228         //
229         // Test additional properties
230         //
231
232         assertEquals(1, config.getAdditionalProperties().size());
233
234         //
235         // Test NOT FOUND
236         //
237         assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
238                 config.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
239
240         //
241         // toString
242         //
243         logger.info("Config {}", config);
244         logger.info("Config2 {}", config2);
245
246     }
247
248     @Test
249     void testConstructor() {
250         PdpdConfiguration config = new PdpdConfiguration(REQUEST_ID, ENTITY, null);
251         assertEquals(REQUEST_ID, config.getRequestId());
252         assertEquals(ENTITY, config.getEntity());
253     }
254
255     @Test
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);
260
261         GsonTestUtils gson = new GsonTestUtils();
262
263         // ensure jackson and gson give same result
264         gson.compareGson(pdpConfig, PdpdConfigurationTest.class);
265
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));
271     }
272
273     /**
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
276      * different runs.
277      *
278      * @param text text from which to strip the identifier
279      * @return the text, without the identifier
280      */
281     private String stripIdent(String text) {
282         return text.replaceAll("@\\w+", "@");
283     }
284 }