b5f3c535407c7335093dfbad76b2d36809e8e736
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Configuration Test
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.junit.Test;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class PdpdConfigurationTest {
36
37     private static final Logger logger = LoggerFactory.getLogger(PdpdConfigurationTest.class);
38
39     private static final String REQUEST_ID = UUID.randomUUID().toString();
40     private static final String REQUEST_ID2 = UUID.randomUUID().toString();
41
42     private static final String ENTITY = "entity1";
43     private static final String ENTITY2 = "entity2";
44
45     private static final String PROPERTY1 = "property1";
46     private static final String PROPERTY2 = "property2";
47
48     private static final String VALUE1 = "value1";
49     private static final String VALUE2 = "value2";
50
51     private static final String ARTIFACT = "org.onap.artifact";
52     private static final String GROUPID = "group";
53     private static final String VERSION = "1.0.0";
54
55     private static final String ARTIFACT2 = "org.onap.artifact2";
56     private static final String GROUPID2 = "group2";
57     private static final String VERSION2 = "1.0.1";
58
59     private static final String NAME = "name";
60     private static final String OPERATION = "operation";
61
62     private static final String NAME2 = "name2";
63     private static final String OPERATION2 = "operation2";
64
65     @Test
66     public void test() {
67         //
68         // Empty constructor test
69         //
70         DroolsConfiguration drools = new DroolsConfiguration();
71         drools.set("artifactId", ARTIFACT);
72         drools.set("groupId", GROUPID);
73         drools.set("version", VERSION);
74         drools.set(PROPERTY1, VALUE1);
75
76         assertTrue(drools.equals(drools));
77         assertFalse(drools.equals(new Object()));
78
79         logger.info("Drools HashCode {}", drools.hashCode());
80
81         //
82         // Constructor with values test get calls
83         //
84         DroolsConfiguration drools2 = new DroolsConfiguration(
85                 drools.get("artifactId"), 
86                 drools.get("groupId"), 
87                 drools.get("version"));
88
89         //
90         // get Property
91         //
92
93         drools2.set(PROPERTY1, drools.get(PROPERTY1));
94
95         assertTrue(drools.equals(drools2));
96
97         //
98         // with methods
99         //
100         drools2.withArtifactId(ARTIFACT2).withGroupId(GROUPID2).withVersion(VERSION2)
101             .withAdditionalProperty(PROPERTY2, VALUE2);
102
103         assertFalse(drools.equals(drools2));
104
105         //
106         // Test get additional properties
107         //
108         assertEquals(drools.getAdditionalProperties().size(), 1);
109
110         //
111         // Test Not found
112         //
113         assertEquals(DroolsConfiguration.NOT_FOUND_VALUE,
114                 drools.declaredPropertyOrNotFound(PROPERTY2, DroolsConfiguration.NOT_FOUND_VALUE));
115
116         logger.info("drools {}", drools);
117         logger.info("drools2 {}", drools2);
118
119         //
120         // Test Controller Default Constructor
121         //
122         ControllerConfiguration controller = new ControllerConfiguration();
123
124         //
125         // Test set
126         //
127
128         controller.set("name", NAME);
129         controller.set("operation", OPERATION);
130         controller.set("drools", drools);
131         controller.set(PROPERTY1, VALUE1);
132
133         assertTrue(controller.equals(controller));
134         assertFalse(controller.equals(new Object()));
135
136         logger.info("Controller HashCode {}", controller.hashCode());
137
138         //
139         // Controller Constructor gets
140         //
141         ControllerConfiguration controller2 = new ControllerConfiguration(
142                 controller.get("name"), 
143                 controller.get("operation"), 
144                 controller.get("drools"));
145
146         //
147         // Test get property
148         //
149
150         controller2.set(PROPERTY1, controller.get(PROPERTY1));
151
152         assertTrue(controller.equals(controller2));
153
154         //
155         // test with methods
156         //
157
158         controller2.withDrools(drools2).withName(NAME2)
159             .withOperation(OPERATION2).withAdditionalProperty(PROPERTY2, VALUE2);
160
161         assertFalse(controller.equals(controller2));
162
163         //
164         // Test additional properties
165         //
166         assertEquals(controller.getAdditionalProperties().size(), 1);
167
168         //
169         // Not found
170         //
171         assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
172                 controller.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
173
174         //
175         // toString
176         //
177         logger.info("Controller {}", controller);
178         logger.info("Controller2 {}", controller2);
179
180         //
181         // PDP Configuration empty constructor
182         //
183         PdpdConfiguration config = new PdpdConfiguration();
184
185         //
186         // Test set
187         //
188
189         config.set("requestID", REQUEST_ID);
190         config.set("entity", ENTITY);
191         List<ControllerConfiguration> controllers = new ArrayList<>();
192         controllers.add(controller);
193         config.set("controllers", controllers);
194         config.set(PROPERTY1, VALUE1);
195
196         assertTrue(config.equals(config));
197         assertFalse(config.equals(new Object()));
198
199         logger.info("Config HashCode {}", config.hashCode());
200
201         //
202         // Test constructor with values
203         //
204
205         PdpdConfiguration config2 = new PdpdConfiguration(
206                 config.get("requestID"),
207                 config.get("entity"),
208                 config.get("controllers"));
209
210         //
211         // Test set
212         //
213
214         config2.set(PROPERTY1, config.get(PROPERTY1));
215
216         assertTrue(config.equals(config2));
217
218         //
219         // Test with methods
220         //
221         List<ControllerConfiguration> controllers2 = new ArrayList<>();
222         controllers2.add(controller2);
223         config2.withRequestID(REQUEST_ID2).withEntity(ENTITY2).withController(controllers2);
224
225         assertFalse(config.equals(config2));
226
227         //
228         // Test additional properties
229         //
230
231         assertEquals(config.getAdditionalProperties().size(), 1);
232
233         //
234         // Test NOT FOUND
235         //
236         assertEquals(ControllerConfiguration.NOT_FOUND_VALUE,
237                 config.declaredPropertyOrNotFound(PROPERTY2, ControllerConfiguration.NOT_FOUND_VALUE));
238
239         //
240         // toString
241         //
242         logger.info("Config {}", config);
243         logger.info("Config2 {}", config2);
244
245     }
246
247     @Test
248     public void testConstructor() {
249         PdpdConfiguration config = new PdpdConfiguration(REQUEST_ID, ENTITY, null);
250         assertEquals(config.getRequestID(), REQUEST_ID);
251         assertEquals(config.getEntity(), ENTITY);
252     }
253
254 }