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 * 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.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.util;
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;
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;
47 * This class is used to test the structured property helper class.
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
57 public class TestStructuredPropertyHelper {
60 * The properties to be parsed
62 private Properties properties;
65 * The result of parsing the properties
67 private List<Node> nodes = new ArrayList<>();
70 * Initialize the test environment
72 @SuppressWarnings("nls")
77 properties = new Properties();
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");
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");
117 properties.setProperty("other.property", "bogus");
118 properties.setProperty("yet.another.property", "bogus");
119 properties.setProperty("simpleProperty", "bogus");
124 * Test that a simple namespace works
126 @SuppressWarnings("nls")
128 public void testSimpleNamespace() {
129 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
131 assertNotNull(nodes);
132 assertFalse(nodes.isEmpty());
134 assertEquals(3, nodes.size());
137 for (Node node : nodes) {
138 switch (node.getName()) {
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()) {
147 assertEquals("provider1URL", child.getValue());
150 assertEquals("provider1type", child.getValue());
153 assertEquals("provider1Name", child.getValue());
156 fail("Unknown child of " + node.getName() + " with value " + child.toString());
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()) {
168 assertEquals("provider2URL", child.getValue());
171 assertEquals("provider2type", child.getValue());
174 assertEquals("provider2Name", child.getValue());
177 fail("Unknown child of " + node.getName() + " with value " + child.toString());
183 * Note that the helper normalizes any ordinal suffixes (003 became 3)
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()) {
192 assertEquals("provider3URL", child.getValue());
195 assertEquals("provider3type", child.getValue());
198 assertEquals("provider3Name", child.getValue());
201 fail("Unknown child of " + node.getName() + " with value " + child.toString());
206 fail("Unknown provider " + node.toString());
212 * Test a multi-dimensional namespace (3X3X3)
214 @SuppressWarnings("nls")
216 public void testMultiLevelNamespace() {
217 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
219 assertNotNull(nodes);
220 assertFalse(nodes.isEmpty());
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());
244 public void testToStringWithValue()
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);
255 public void testEquals()
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));
267 public void testEqualsWithSameNameAndDifferentValue()
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));
279 public void testEqualsWithSameValueAndDifferentName()
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));