2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.util;
26 import static org.junit.Assert.*;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Properties;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.appc.util.StructuredPropertyHelper;
35 import org.onap.appc.util.StructuredPropertyHelper.Node;
38 * This class is used to test the structured property helper class.
40 * A structured property is one where the name is constructed from a compound set of elements, concatenated by a period,
41 * and optionally being enumerated using a sequence number suffix. A java package name is an example of a structured
42 * name, where each element of the name represents a directory or name in the namespace hierarchy. Property names may
43 * also be structured. This class constructs a graph of the structured properties and this test case is used to verify
48 public class TestStructuredPropertyHelper {
51 * The properties to be parsed
53 private Properties properties;
56 * The result of parsing the properties
58 private List<Node> nodes = new ArrayList<>();
61 * Initialize the test environment
63 @SuppressWarnings("nls")
68 properties = new Properties();
70 properties.setProperty("provider1.name", "provider1Name");
71 properties.setProperty("provider1.type", "provider1type");
72 properties.setProperty("provider1.URL", "provider1URL");
73 properties.setProperty("provider2.name", "provider2Name");
74 properties.setProperty("provider2.type", "provider2type");
75 properties.setProperty("provider2.URL", "provider2URL");
76 properties.setProperty("provider003.name", "provider3Name");
77 properties.setProperty("provider003.type", "provider3type");
78 properties.setProperty("provider003.URL", "provider3URL");
80 properties.setProperty("node1.level1.value1.key", "1.1.1");
81 properties.setProperty("node1.level1.value2.key", "1.1.2");
82 properties.setProperty("node1.level1.value3.key", "1.1.3");
83 properties.setProperty("node1.level2.value1.key", "1.2.1");
84 properties.setProperty("node1.level2.value2.key", "1.2.2");
85 properties.setProperty("node1.level2.value3.key", "1.2.3");
86 properties.setProperty("node1.level3.value1.key", "1.3.1");
87 properties.setProperty("node1.level3.value2.key", "1.3.2");
88 properties.setProperty("node1.level3.value3.key", "1.3.3");
89 properties.setProperty("node2.level1.value1.key", "2.1.1");
90 properties.setProperty("node2.level1.value2.key", "2.1.2");
91 properties.setProperty("node2.level1.value3.key", "2.1.3");
92 properties.setProperty("node2.level2.value1.key", "2.2.1");
93 properties.setProperty("node2.level2.value2.key", "2.2.2");
94 properties.setProperty("node2.level2.value3.key", "2.2.3");
95 properties.setProperty("node2.level3.value1.key", "2.3.1");
96 properties.setProperty("node2.level3.value2.key", "2.3.2");
97 properties.setProperty("node2.level3.value3.key", "2.3.3");
98 properties.setProperty("node3.level1.value1.key", "3.1.1");
99 properties.setProperty("node3.level1.value2.key", "3.1.2");
100 properties.setProperty("node3.level1.value3.key", "3.1.3");
101 properties.setProperty("node3.level2.value1.key", "3.2.1");
102 properties.setProperty("node3.level2.value2.key", "3.2.2");
103 properties.setProperty("node3.level2.value3.key", "3.2.3");
104 properties.setProperty("node3.level3.value1.key", "3.3.1");
105 properties.setProperty("node3.level3.value2.key", "3.3.2");
106 properties.setProperty("node3.level3.value3.key", "3.3.3");
108 properties.setProperty("other.property", "bogus");
109 properties.setProperty("yet.another.property", "bogus");
110 properties.setProperty("simpleProperty", "bogus");
115 * Test that a simple namespace works
117 @SuppressWarnings("nls")
119 public void testSimpleNamespace() {
120 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
122 assertNotNull(nodes);
123 assertFalse(nodes.isEmpty());
125 assertEquals(3, nodes.size());
128 for (Node node : nodes) {
129 switch (node.getName()) {
131 assertNull(node.getValue());
132 children = node.getChildren();
133 assertNotNull(children);
134 assertEquals(3, children.size());
135 for (Node child : children) {
136 switch (child.getName()) {
138 assertEquals("provider1URL", child.getValue());
141 assertEquals("provider1type", child.getValue());
144 assertEquals("provider1Name", child.getValue());
147 fail("Unknown child of " + node.getName() + " with value " + child.toString());
152 assertNull(node.getValue());
153 children = node.getChildren();
154 assertNotNull(children);
155 assertEquals(3, children.size());
156 for (Node child : children) {
157 switch (child.getName()) {
159 assertEquals("provider2URL", child.getValue());
162 assertEquals("provider2type", child.getValue());
165 assertEquals("provider2Name", child.getValue());
168 fail("Unknown child of " + node.getName() + " with value " + child.toString());
174 * Note that the helper normalizes any ordinal suffixes (003 became 3)
176 assertNull(node.getValue());
177 children = node.getChildren();
178 assertNotNull(children);
179 assertEquals(3, children.size());
180 for (Node child : children) {
181 switch (child.getName()) {
183 assertEquals("provider3URL", child.getValue());
186 assertEquals("provider3type", child.getValue());
189 assertEquals("provider3Name", child.getValue());
192 fail("Unknown child of " + node.getName() + " with value " + child.toString());
197 fail("Unknown provider " + node.toString());
200 // System.out.println(nodes);
204 * Test a multi-dimensional namespace (3X3X3)
206 @SuppressWarnings("nls")
208 public void testMultiLevelNamespace() {
209 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
211 assertNotNull(nodes);
212 assertFalse(nodes.isEmpty());
214 assertEquals(3, nodes.size());
215 for (Node node : nodes) {
216 assertNull(node.getValue());
217 List<Node> children = node.getChildren();
218 assertNotNull(children);
219 assertEquals(3, children.size());
220 for (Node child : children) {
221 assertNull(child.getValue());
222 List<Node> grandChildren = child.getChildren();
223 assertNotNull(grandChildren);
224 assertEquals(3, grandChildren.size());
225 for (Node greatGrandChild : grandChildren) {
226 assertNull(greatGrandChild.getValue());
227 List<Node> greatGrandChildren = greatGrandChild.getChildren();
228 assertNotNull(greatGrandChildren);
229 assertEquals(1, greatGrandChildren.size());
233 // System.out.println(nodes);