added test cases to TestStructuredPropertyHelper
[appc.git] / appc-core / appc-common-bundle / src / test / java / org / onap / appc / util / TestStructuredPropertyHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2018-2019 IBM.
10  * ================================================================================
11  * Modifications Copyright (C) 2019 Ericsson
12  * =============================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *============LICENSE_END=========================================================
25  */
26
27 package org.onap.appc.util;
28
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertNull;
34 import static org.junit.Assert.assertTrue;
35 import static org.junit.Assert.fail;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Properties;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.appc.util.StructuredPropertyHelper;
43 import org.onap.appc.util.StructuredPropertyHelper.Node;
44
45 /**
46  * This class is used to test the structured property helper class.
47  * <p>
48  * A structured property is one where the name is constructed from a compound set of elements, concatenated by a period,
49  * and optionally being enumerated using a sequence number suffix. A java package name is an example of a structured
50  * name, where each element of the name represents a directory or name in the namespace hierarchy. Property names may
51  * also be structured. This class constructs a graph of the structured properties and this test case is used to verify
52  * its operation.
53  * </p>
54  *
55  */
56 public class TestStructuredPropertyHelper {
57
58     /**
59      * The properties to be parsed
60      */
61     private Properties properties;
62
63     /**
64      * The result of parsing the properties
65      */
66     private List<Node> nodes = new ArrayList<>();
67
68     /**
69      * Initialize the test environment
70      */
71     @SuppressWarnings("nls")
72     @Before
73     public void setup() {
74         nodes.clear();
75
76         properties = new Properties();
77
78         properties.setProperty("provider1.name", "provider1Name");
79         properties.setProperty("provider1.type", "provider1type");
80         properties.setProperty("provider1.URL", "provider1URL");
81         properties.setProperty("provider2.name", "provider2Name");
82         properties.setProperty("provider2.type", "provider2type");
83         properties.setProperty("provider2.URL", "provider2URL");
84         properties.setProperty("provider003.name", "provider3Name");
85         properties.setProperty("provider003.type", "provider3type");
86         properties.setProperty("provider003.URL", "provider3URL");
87
88         properties.setProperty("node1.level1.value1.key", "1.1.1");
89         properties.setProperty("node1.level1.value2.key", "1.1.2");
90         properties.setProperty("node1.level1.value3.key", "1.1.3");
91         properties.setProperty("node1.level2.value1.key", "1.2.1");
92         properties.setProperty("node1.level2.value2.key", "1.2.2");
93         properties.setProperty("node1.level2.value3.key", "1.2.3");
94         properties.setProperty("node1.level3.value1.key", "1.3.1");
95         properties.setProperty("node1.level3.value2.key", "1.3.2");
96         properties.setProperty("node1.level3.value3.key", "1.3.3");
97         properties.setProperty("node2.level1.value1.key", "2.1.1");
98         properties.setProperty("node2.level1.value2.key", "2.1.2");
99         properties.setProperty("node2.level1.value3.key", "2.1.3");
100         properties.setProperty("node2.level2.value1.key", "2.2.1");
101         properties.setProperty("node2.level2.value2.key", "2.2.2");
102         properties.setProperty("node2.level2.value3.key", "2.2.3");
103         properties.setProperty("node2.level3.value1.key", "2.3.1");
104         properties.setProperty("node2.level3.value2.key", "2.3.2");
105         properties.setProperty("node2.level3.value3.key", "2.3.3");
106         properties.setProperty("node3.level1.value1.key", "3.1.1");
107         properties.setProperty("node3.level1.value2.key", "3.1.2");
108         properties.setProperty("node3.level1.value3.key", "3.1.3");
109         properties.setProperty("node3.level2.value1.key", "3.2.1");
110         properties.setProperty("node3.level2.value2.key", "3.2.2");
111         properties.setProperty("node3.level2.value3.key", "3.2.3");
112         properties.setProperty("node3.level3.value1.key", "3.3.1");
113         properties.setProperty("node3.level3.value2.key", "3.3.2");
114         properties.setProperty("node3.level3.value3.key", "3.3.3");
115
116         properties.setProperty("other.property", "bogus");
117         properties.setProperty("yet.another.property", "bogus");
118         properties.setProperty("simpleProperty", "bogus");
119
120     }
121
122     /**
123      * Test that a simple namespace works
124      */
125     @SuppressWarnings("nls")
126     @Test
127     public void testSimpleNamespace() {
128         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
129
130         assertNotNull(nodes);
131         assertFalse(nodes.isEmpty());
132
133         assertEquals(3, nodes.size());
134
135         List<Node> children;
136         for (Node node : nodes) {
137             switch (node.getName()) {
138                 case "provider1":
139                     assertNull(node.getValue());
140                     children = node.getChildren();
141                     assertNotNull(children);
142                     assertEquals(3, children.size());
143                     for (Node child : children) {
144                         switch (child.getName()) {
145                             case "URL":
146                                 assertEquals("provider1URL", child.getValue());
147                                 break;
148                             case "type":
149                                 assertEquals("provider1type", child.getValue());
150                                 break;
151                             case "name":
152                                 assertEquals("provider1Name", child.getValue());
153                                 break;
154                             default:
155                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
156                         }
157                     }
158                     break;
159                 case "provider2":
160                     assertNull(node.getValue());
161                     children = node.getChildren();
162                     assertNotNull(children);
163                     assertEquals(3, children.size());
164                     for (Node child : children) {
165                         switch (child.getName()) {
166                             case "URL":
167                                 assertEquals("provider2URL", child.getValue());
168                                 break;
169                             case "type":
170                                 assertEquals("provider2type", child.getValue());
171                                 break;
172                             case "name":
173                                 assertEquals("provider2Name", child.getValue());
174                                 break;
175                             default:
176                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
177                         }
178                     }
179                     break;
180                 case "provider3":
181                     /*
182                      * Note that the helper normalizes any ordinal suffixes (003 became 3)
183                      */
184                     assertNull(node.getValue());
185                     children = node.getChildren();
186                     assertNotNull(children);
187                     assertEquals(3, children.size());
188                     for (Node child : children) {
189                         switch (child.getName()) {
190                             case "URL":
191                                 assertEquals("provider3URL", child.getValue());
192                                 break;
193                             case "type":
194                                 assertEquals("provider3type", child.getValue());
195                                 break;
196                             case "name":
197                                 assertEquals("provider3Name", child.getValue());
198                                 break;
199                             default:
200                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
201                         }
202                     }
203                     break;
204                 default:
205                     fail("Unknown provider " + node.toString());
206             }
207         }
208     }
209
210     /**
211      * Test a multi-dimensional namespace (3X3X3)
212      */
213     @SuppressWarnings("nls")
214     @Test
215     public void testMultiLevelNamespace() {
216         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
217
218         assertNotNull(nodes);
219         assertFalse(nodes.isEmpty());
220
221         assertEquals(3, nodes.size());
222         for (Node node : nodes) {
223             assertNull(node.getValue());
224             List<Node> children = node.getChildren();
225             assertNotNull(children);
226             assertEquals(3, children.size());
227             for (Node child : children) {
228                 assertNull(child.getValue());
229                 List<Node> grandChildren = child.getChildren();
230                 assertNotNull(grandChildren);
231                 assertEquals(3, grandChildren.size());
232                 for (Node greatGrandChild : grandChildren) {
233                     assertNull(greatGrandChild.getValue());
234                     List<Node> greatGrandChildren = greatGrandChild.getChildren();
235                     assertNotNull(greatGrandChildren);
236                     assertEquals(1, greatGrandChildren.size());
237                 }
238             }
239         }
240     }
241
242     @Test
243     public void testToStringWithValue()
244     {
245         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
246         Node node = nodes.get(0);
247         node.setName("testName");
248         node.setValue("testValue");
249         String str = node.toString();
250         assertEquals("testName = testValue",str);
251     }
252
253     @Test
254     public void testEquals()
255     {
256         Node node0 = new Node();
257         node0.setName("testName");
258         node0.setValue("testValue");
259         Node node1 = new Node();
260         node1.setName("testName");
261         node1.setValue("testValue");
262         assertTrue(node0.equals(node1));
263         assertFalse(node0.equals(null));
264         node0.setValue(null);
265         assertFalse(node0.equals(node1));
266      }
267
268    @Test
269     public void testEqualsWithSameNameAndDifferentValue()
270     {
271         Node node0 = new Node();
272         node0.setName("testName");
273         node0.setValue("testValue1");
274         Node node1 = new Node();
275         node1.setName("testName");
276         node1.setValue("testValue2");
277         assertFalse(node0.equals(node1));    
278     }
279
280     @Test
281     public void testEqualsWithSameValueAndDifferentName()
282     {
283         Node node0 = new Node();
284         node0.setName("testName1");
285         node0.setValue("testValue");
286         Node node1 = new Node();
287         node1.setName("testName2");
288         node1.setValue("testValue");
289         assertFalse(node0.equals(node1));
290     }
291
292     @Test
293     public void testCompareTo()
294     {
295         Node node0 = new Node();
296         node0.setName("testName1");
297         Node node1 = new Node();
298         node1.setName("testName2");
299         assertEquals(-1, node0.compareTo(node1));
300         assertEquals(0, node0.compareTo(node0));
301     }
302
303     @Test
304     public void testHashCode()
305     {
306         Node node0 = new Node();
307         node0.setName("testName");
308         node0.setValue("testValue");
309         Node node1 = new Node();
310         node1.setName("testName");
311         node1.setValue("testValue");
312         assertTrue(node0.hashCode() == node1.hashCode());
313     }
314
315 }