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