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