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 IBM.
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.util;
28 import static org.junit.Assert.*;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.appc.util.StructuredPropertyHelper;
37 import org.onap.appc.util.StructuredPropertyHelper.Node;
40 * This class is used to test the structured property helper class.
42 * A structured property is one where the name is constructed from a compound set of elements, concatenated by a period,
43 * and optionally being enumerated using a sequence number suffix. A java package name is an example of a structured
44 * name, where each element of the name represents a directory or name in the namespace hierarchy. Property names may
45 * also be structured. This class constructs a graph of the structured properties and this test case is used to verify
50 public class TestStructuredPropertyHelper {
53 * The properties to be parsed
55 private Properties properties;
58 * The result of parsing the properties
60 private List<Node> nodes = new ArrayList<>();
63 * Initialize the test environment
65 @SuppressWarnings("nls")
70 properties = new Properties();
72 properties.setProperty("provider1.name", "provider1Name");
73 properties.setProperty("provider1.type", "provider1type");
74 properties.setProperty("provider1.URL", "provider1URL");
75 properties.setProperty("provider2.name", "provider2Name");
76 properties.setProperty("provider2.type", "provider2type");
77 properties.setProperty("provider2.URL", "provider2URL");
78 properties.setProperty("provider003.name", "provider3Name");
79 properties.setProperty("provider003.type", "provider3type");
80 properties.setProperty("provider003.URL", "provider3URL");
82 properties.setProperty("node1.level1.value1.key", "1.1.1");
83 properties.setProperty("node1.level1.value2.key", "1.1.2");
84 properties.setProperty("node1.level1.value3.key", "1.1.3");
85 properties.setProperty("node1.level2.value1.key", "1.2.1");
86 properties.setProperty("node1.level2.value2.key", "1.2.2");
87 properties.setProperty("node1.level2.value3.key", "1.2.3");
88 properties.setProperty("node1.level3.value1.key", "1.3.1");
89 properties.setProperty("node1.level3.value2.key", "1.3.2");
90 properties.setProperty("node1.level3.value3.key", "1.3.3");
91 properties.setProperty("node2.level1.value1.key", "2.1.1");
92 properties.setProperty("node2.level1.value2.key", "2.1.2");
93 properties.setProperty("node2.level1.value3.key", "2.1.3");
94 properties.setProperty("node2.level2.value1.key", "2.2.1");
95 properties.setProperty("node2.level2.value2.key", "2.2.2");
96 properties.setProperty("node2.level2.value3.key", "2.2.3");
97 properties.setProperty("node2.level3.value1.key", "2.3.1");
98 properties.setProperty("node2.level3.value2.key", "2.3.2");
99 properties.setProperty("node2.level3.value3.key", "2.3.3");
100 properties.setProperty("node3.level1.value1.key", "3.1.1");
101 properties.setProperty("node3.level1.value2.key", "3.1.2");
102 properties.setProperty("node3.level1.value3.key", "3.1.3");
103 properties.setProperty("node3.level2.value1.key", "3.2.1");
104 properties.setProperty("node3.level2.value2.key", "3.2.2");
105 properties.setProperty("node3.level2.value3.key", "3.2.3");
106 properties.setProperty("node3.level3.value1.key", "3.3.1");
107 properties.setProperty("node3.level3.value2.key", "3.3.2");
108 properties.setProperty("node3.level3.value3.key", "3.3.3");
110 properties.setProperty("other.property", "bogus");
111 properties.setProperty("yet.another.property", "bogus");
112 properties.setProperty("simpleProperty", "bogus");
117 * Test that a simple namespace works
119 @SuppressWarnings("nls")
121 public void testSimpleNamespace() {
122 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
124 assertNotNull(nodes);
125 assertFalse(nodes.isEmpty());
127 assertEquals(3, nodes.size());
130 for (Node node : nodes) {
131 switch (node.getName()) {
133 assertNull(node.getValue());
134 children = node.getChildren();
135 assertNotNull(children);
136 assertEquals(3, children.size());
137 for (Node child : children) {
138 switch (child.getName()) {
140 assertEquals("provider1URL", child.getValue());
143 assertEquals("provider1type", child.getValue());
146 assertEquals("provider1Name", child.getValue());
149 fail("Unknown child of " + node.getName() + " with value " + child.toString());
154 assertNull(node.getValue());
155 children = node.getChildren();
156 assertNotNull(children);
157 assertEquals(3, children.size());
158 for (Node child : children) {
159 switch (child.getName()) {
161 assertEquals("provider2URL", child.getValue());
164 assertEquals("provider2type", child.getValue());
167 assertEquals("provider2Name", child.getValue());
170 fail("Unknown child of " + node.getName() + " with value " + child.toString());
176 * Note that the helper normalizes any ordinal suffixes (003 became 3)
178 assertNull(node.getValue());
179 children = node.getChildren();
180 assertNotNull(children);
181 assertEquals(3, children.size());
182 for (Node child : children) {
183 switch (child.getName()) {
185 assertEquals("provider3URL", child.getValue());
188 assertEquals("provider3type", child.getValue());
191 assertEquals("provider3Name", child.getValue());
194 fail("Unknown child of " + node.getName() + " with value " + child.toString());
199 fail("Unknown provider " + node.toString());
202 // System.out.println(nodes);
206 * Test a multi-dimensional namespace (3X3X3)
208 @SuppressWarnings("nls")
210 public void testMultiLevelNamespace() {
211 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
213 assertNotNull(nodes);
214 assertFalse(nodes.isEmpty());
216 assertEquals(3, nodes.size());
217 for (Node node : nodes) {
218 assertNull(node.getValue());
219 List<Node> children = node.getChildren();
220 assertNotNull(children);
221 assertEquals(3, children.size());
222 for (Node child : children) {
223 assertNull(child.getValue());
224 List<Node> grandChildren = child.getChildren();
225 assertNotNull(grandChildren);
226 assertEquals(3, grandChildren.size());
227 for (Node greatGrandChild : grandChildren) {
228 assertNull(greatGrandChild.getValue());
229 List<Node> greatGrandChildren = greatGrandChild.getChildren();
230 assertNotNull(greatGrandChildren);
231 assertEquals(1, greatGrandChildren.size());
235 // System.out.println(nodes);
239 public void testToStringWithValue()
241 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
242 Node node = nodes.get(0);
243 node.setName("testName");
244 node.setValue("testValue");
245 String str= node.toString();
246 assertEquals("testName = testValue",str);
250 public void testEquals()
252 Node node0 = new Node();
253 node0.setName("testName");
254 node0.setValue("testValue");
255 Node node1 = new Node();
256 node1.setName("testName");
257 node1.setValue("testValue");
258 assertTrue(node0.equals(node1));
262 public void testEqualsWithSameNameAndDifferentValue()
264 Node node0 = new Node();
265 node0.setName("testName");
266 node0.setValue("testValue1");
267 Node node1 = new Node();
268 node1.setName("testName");
269 node1.setValue("testValue2");
270 assertFalse(node0.equals(node1));
274 public void testEqualsWithSameValueAndDifferentName()
276 Node node0 = new Node();
277 node0.setName("testName1");
278 node0.setValue("testValue");
279 Node node1 = new Node();
280 node1.setName("testName2");
281 node1.setValue("testValue");
282 assertFalse(node0.equals(node1));