2 * ============LICENSE_START=======================================================
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
27 package org.onap.appc.util;
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;
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;
46 * This class is used to test the structured property helper class.
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
56 public class TestStructuredPropertyHelper {
59 * The properties to be parsed
61 private Properties properties;
64 * The result of parsing the properties
66 private List<Node> nodes = new ArrayList<>();
69 * Initialize the test environment
71 @SuppressWarnings("nls")
76 properties = new Properties();
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");
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");
116 properties.setProperty("other.property", "bogus");
117 properties.setProperty("yet.another.property", "bogus");
118 properties.setProperty("simpleProperty", "bogus");
123 * Test that a simple namespace works
125 @SuppressWarnings("nls")
127 public void testSimpleNamespace() {
128 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
130 assertNotNull(nodes);
131 assertFalse(nodes.isEmpty());
133 assertEquals(3, nodes.size());
136 for (Node node : nodes) {
137 switch (node.getName()) {
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()) {
146 assertEquals("provider1URL", child.getValue());
149 assertEquals("provider1type", child.getValue());
152 assertEquals("provider1Name", child.getValue());
155 fail("Unknown child of " + node.getName() + " with value " + child.toString());
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()) {
167 assertEquals("provider2URL", child.getValue());
170 assertEquals("provider2type", child.getValue());
173 assertEquals("provider2Name", child.getValue());
176 fail("Unknown child of " + node.getName() + " with value " + child.toString());
182 * Note that the helper normalizes any ordinal suffixes (003 became 3)
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()) {
191 assertEquals("provider3URL", child.getValue());
194 assertEquals("provider3type", child.getValue());
197 assertEquals("provider3Name", child.getValue());
200 fail("Unknown child of " + node.getName() + " with value " + child.toString());
205 fail("Unknown provider " + node.toString());
211 * Test a multi-dimensional namespace (3X3X3)
213 @SuppressWarnings("nls")
215 public void testMultiLevelNamespace() {
216 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
218 assertNotNull(nodes);
219 assertFalse(nodes.isEmpty());
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());
243 public void testToStringWithValue()
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);
254 public void testEquals()
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));
269 public void testEqualsWithSameNameAndDifferentValue()
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));
281 public void testEqualsWithSameValueAndDifferentName()
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));
293 public void testCompareTo()
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));
304 public void testHashCode()
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());