Fix intermittent time based test failures
[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 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  * 
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.util;
29
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertNotNull;
34 import static org.junit.Assert.assertNull;
35 import static org.junit.Assert.assertTrue;
36 import static org.junit.Assert.fail;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Properties;
40
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.appc.util.StructuredPropertyHelper;
44 import org.onap.appc.util.StructuredPropertyHelper.Node;
45
46 /**
47  * This class is used to test the structured property helper class.
48  * <p>
49  * A structured property is one where the name is constructed from a compound set of elements, concatenated by a period,
50  * and optionally being enumerated using a sequence number suffix. A java package name is an example of a structured
51  * name, where each element of the name represents a directory or name in the namespace hierarchy. Property names may
52  * also be structured. This class constructs a graph of the structured properties and this test case is used to verify
53  * its operation.
54  * </p>
55  *
56  */
57 public class TestStructuredPropertyHelper {
58
59     /**
60      * The properties to be parsed
61      */
62     private Properties properties;
63
64     /**
65      * The result of parsing the properties
66      */
67     private List<Node> nodes = new ArrayList<>();
68
69     /**
70      * Initialize the test environment
71      */
72     @SuppressWarnings("nls")
73     @Before
74     public void setup() {
75         nodes.clear();
76
77         properties = new Properties();
78
79         properties.setProperty("provider1.name", "provider1Name");
80         properties.setProperty("provider1.type", "provider1type");
81         properties.setProperty("provider1.URL", "provider1URL");
82         properties.setProperty("provider2.name", "provider2Name");
83         properties.setProperty("provider2.type", "provider2type");
84         properties.setProperty("provider2.URL", "provider2URL");
85         properties.setProperty("provider003.name", "provider3Name");
86         properties.setProperty("provider003.type", "provider3type");
87         properties.setProperty("provider003.URL", "provider3URL");
88
89         properties.setProperty("node1.level1.value1.key", "1.1.1");
90         properties.setProperty("node1.level1.value2.key", "1.1.2");
91         properties.setProperty("node1.level1.value3.key", "1.1.3");
92         properties.setProperty("node1.level2.value1.key", "1.2.1");
93         properties.setProperty("node1.level2.value2.key", "1.2.2");
94         properties.setProperty("node1.level2.value3.key", "1.2.3");
95         properties.setProperty("node1.level3.value1.key", "1.3.1");
96         properties.setProperty("node1.level3.value2.key", "1.3.2");
97         properties.setProperty("node1.level3.value3.key", "1.3.3");
98         properties.setProperty("node2.level1.value1.key", "2.1.1");
99         properties.setProperty("node2.level1.value2.key", "2.1.2");
100         properties.setProperty("node2.level1.value3.key", "2.1.3");
101         properties.setProperty("node2.level2.value1.key", "2.2.1");
102         properties.setProperty("node2.level2.value2.key", "2.2.2");
103         properties.setProperty("node2.level2.value3.key", "2.2.3");
104         properties.setProperty("node2.level3.value1.key", "2.3.1");
105         properties.setProperty("node2.level3.value2.key", "2.3.2");
106         properties.setProperty("node2.level3.value3.key", "2.3.3");
107         properties.setProperty("node3.level1.value1.key", "3.1.1");
108         properties.setProperty("node3.level1.value2.key", "3.1.2");
109         properties.setProperty("node3.level1.value3.key", "3.1.3");
110         properties.setProperty("node3.level2.value1.key", "3.2.1");
111         properties.setProperty("node3.level2.value2.key", "3.2.2");
112         properties.setProperty("node3.level2.value3.key", "3.2.3");
113         properties.setProperty("node3.level3.value1.key", "3.3.1");
114         properties.setProperty("node3.level3.value2.key", "3.3.2");
115         properties.setProperty("node3.level3.value3.key", "3.3.3");
116
117         properties.setProperty("other.property", "bogus");
118         properties.setProperty("yet.another.property", "bogus");
119         properties.setProperty("simpleProperty", "bogus");
120
121     }
122
123     /**
124      * Test that a simple namespace works
125      */
126     @SuppressWarnings("nls")
127     @Test
128     public void testSimpleNamespace() {
129         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
130
131         assertNotNull(nodes);
132         assertFalse(nodes.isEmpty());
133
134         assertEquals(3, nodes.size());
135
136         List<Node> children;
137         for (Node node : nodes) {
138             switch (node.getName()) {
139                 case "provider1":
140                     assertNull(node.getValue());
141                     children = node.getChildren();
142                     assertNotNull(children);
143                     assertEquals(3, children.size());
144                     for (Node child : children) {
145                         switch (child.getName()) {
146                             case "URL":
147                                 assertEquals("provider1URL", child.getValue());
148                                 break;
149                             case "type":
150                                 assertEquals("provider1type", child.getValue());
151                                 break;
152                             case "name":
153                                 assertEquals("provider1Name", child.getValue());
154                                 break;
155                             default:
156                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
157                         }
158                     }
159                     break;
160                 case "provider2":
161                     assertNull(node.getValue());
162                     children = node.getChildren();
163                     assertNotNull(children);
164                     assertEquals(3, children.size());
165                     for (Node child : children) {
166                         switch (child.getName()) {
167                             case "URL":
168                                 assertEquals("provider2URL", child.getValue());
169                                 break;
170                             case "type":
171                                 assertEquals("provider2type", child.getValue());
172                                 break;
173                             case "name":
174                                 assertEquals("provider2Name", child.getValue());
175                                 break;
176                             default:
177                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
178                         }
179                     }
180                     break;
181                 case "provider3":
182                     /*
183                      * Note that the helper normalizes any ordinal suffixes (003 became 3)
184                      */
185                     assertNull(node.getValue());
186                     children = node.getChildren();
187                     assertNotNull(children);
188                     assertEquals(3, children.size());
189                     for (Node child : children) {
190                         switch (child.getName()) {
191                             case "URL":
192                                 assertEquals("provider3URL", child.getValue());
193                                 break;
194                             case "type":
195                                 assertEquals("provider3type", child.getValue());
196                                 break;
197                             case "name":
198                                 assertEquals("provider3Name", child.getValue());
199                                 break;
200                             default:
201                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
202                         }
203                     }
204                     break;
205                 default:
206                     fail("Unknown provider " + node.toString());
207             }
208         }
209     }
210
211     /**
212      * Test a multi-dimensional namespace (3X3X3)
213      */
214     @SuppressWarnings("nls")
215     @Test
216     public void testMultiLevelNamespace() {
217         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
218
219         assertNotNull(nodes);
220         assertFalse(nodes.isEmpty());
221
222         assertEquals(3, nodes.size());
223         for (Node node : nodes) {
224             assertNull(node.getValue());
225             List<Node> children = node.getChildren();
226             assertNotNull(children);
227             assertEquals(3, children.size());
228             for (Node child : children) {
229                 assertNull(child.getValue());
230                 List<Node> grandChildren = child.getChildren();
231                 assertNotNull(grandChildren);
232                 assertEquals(3, grandChildren.size());
233                 for (Node greatGrandChild : grandChildren) {
234                     assertNull(greatGrandChild.getValue());
235                     List<Node> greatGrandChildren = greatGrandChild.getChildren();
236                     assertNotNull(greatGrandChildren);
237                     assertEquals(1, greatGrandChildren.size());
238                 }
239             }
240         }
241     }
242
243     @Test
244     public void testToStringWithValue()
245     {
246         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
247         Node node = nodes.get(0);
248         node.setName("testName");
249         node.setValue("testValue");
250         String str = node.toString();
251         assertEquals("testName = testValue",str);
252     }
253
254     @Test
255     public void testEquals()
256     {
257         Node node0 = new Node();
258         node0.setName("testName");
259         node0.setValue("testValue");
260         Node node1 = new Node();
261         node1.setName("testName");
262         node1.setValue("testValue");
263         assertTrue(node0.equals(node1));    
264     }
265
266    @Test
267     public void testEqualsWithSameNameAndDifferentValue()
268     {
269         Node node0 = new Node();
270         node0.setName("testName");
271         node0.setValue("testValue1");
272         Node node1 = new Node();
273         node1.setName("testName");
274         node1.setValue("testValue2");
275         assertFalse(node0.equals(node1));    
276     }
277
278     @Test
279     public void testEqualsWithSameValueAndDifferentName()
280     {
281         Node node0 = new Node();
282         node0.setName("testName1");
283         node0.setValue("testValue");
284         Node node1 = new Node();
285         node1.setName("testName2");
286         node1.setValue("testValue");
287         assertFalse(node0.equals(node1));    
288     }
289 }