2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.util;
27 import static org.junit.Assert.*;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.openecomp.appc.util.StructuredPropertyHelper;
36 import org.openecomp.appc.util.StructuredPropertyHelper.Node;
39 * This class is used to test the structured property helper class.
41 * A structured property is one where the name is constructed from a compound set of elements, concatenated by a period,
42 * and optionally being enumerated using a sequence number suffix. A java package name is an example of a structured
43 * name, where each element of the name represents a directory or name in the namespace hierarchy. Property names may
44 * also be structured. This class constructs a graph of the structured properties and this test case is used to verify
49 public class TestStructuredPropertyHelper {
52 * The properties to be parsed
54 private Properties properties;
57 * The result of parsing the properties
59 private List<Node> nodes = new ArrayList<>();
62 * Initialize the test environment
64 @SuppressWarnings("nls")
69 properties = new Properties();
71 properties.setProperty("provider1.name", "provider1Name");
72 properties.setProperty("provider1.type", "provider1type");
73 properties.setProperty("provider1.URL", "provider1URL");
74 properties.setProperty("provider2.name", "provider2Name");
75 properties.setProperty("provider2.type", "provider2type");
76 properties.setProperty("provider2.URL", "provider2URL");
77 properties.setProperty("provider003.name", "provider3Name");
78 properties.setProperty("provider003.type", "provider3type");
79 properties.setProperty("provider003.URL", "provider3URL");
81 properties.setProperty("node1.level1.value1.key", "1.1.1");
82 properties.setProperty("node1.level1.value2.key", "1.1.2");
83 properties.setProperty("node1.level1.value3.key", "1.1.3");
84 properties.setProperty("node1.level2.value1.key", "1.2.1");
85 properties.setProperty("node1.level2.value2.key", "1.2.2");
86 properties.setProperty("node1.level2.value3.key", "1.2.3");
87 properties.setProperty("node1.level3.value1.key", "1.3.1");
88 properties.setProperty("node1.level3.value2.key", "1.3.2");
89 properties.setProperty("node1.level3.value3.key", "1.3.3");
90 properties.setProperty("node2.level1.value1.key", "2.1.1");
91 properties.setProperty("node2.level1.value2.key", "2.1.2");
92 properties.setProperty("node2.level1.value3.key", "2.1.3");
93 properties.setProperty("node2.level2.value1.key", "2.2.1");
94 properties.setProperty("node2.level2.value2.key", "2.2.2");
95 properties.setProperty("node2.level2.value3.key", "2.2.3");
96 properties.setProperty("node2.level3.value1.key", "2.3.1");
97 properties.setProperty("node2.level3.value2.key", "2.3.2");
98 properties.setProperty("node2.level3.value3.key", "2.3.3");
99 properties.setProperty("node3.level1.value1.key", "3.1.1");
100 properties.setProperty("node3.level1.value2.key", "3.1.2");
101 properties.setProperty("node3.level1.value3.key", "3.1.3");
102 properties.setProperty("node3.level2.value1.key", "3.2.1");
103 properties.setProperty("node3.level2.value2.key", "3.2.2");
104 properties.setProperty("node3.level2.value3.key", "3.2.3");
105 properties.setProperty("node3.level3.value1.key", "3.3.1");
106 properties.setProperty("node3.level3.value2.key", "3.3.2");
107 properties.setProperty("node3.level3.value3.key", "3.3.3");
109 properties.setProperty("other.property", "bogus");
110 properties.setProperty("yet.another.property", "bogus");
111 properties.setProperty("simpleProperty", "bogus");
116 * Test that a simple namespace works
118 @SuppressWarnings("nls")
120 public void testSimpleNamespace() {
121 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
123 assertNotNull(nodes);
124 assertFalse(nodes.isEmpty());
126 assertEquals(3, nodes.size());
129 for (Node node : nodes) {
130 switch (node.getName()) {
132 assertNull(node.getValue());
133 children = node.getChildren();
134 assertNotNull(children);
135 assertEquals(3, children.size());
136 for (Node child : children) {
137 switch (child.getName()) {
139 assertEquals("provider1URL", child.getValue());
142 assertEquals("provider1type", child.getValue());
145 assertEquals("provider1Name", child.getValue());
148 fail("Unknown child of " + node.getName() + " with value " + child.toString());
153 assertNull(node.getValue());
154 children = node.getChildren();
155 assertNotNull(children);
156 assertEquals(3, children.size());
157 for (Node child : children) {
158 switch (child.getName()) {
160 assertEquals("provider2URL", child.getValue());
163 assertEquals("provider2type", child.getValue());
166 assertEquals("provider2Name", child.getValue());
169 fail("Unknown child of " + node.getName() + " with value " + child.toString());
175 * Note that the helper normalizes any ordinal suffixes (003 became 3)
177 assertNull(node.getValue());
178 children = node.getChildren();
179 assertNotNull(children);
180 assertEquals(3, children.size());
181 for (Node child : children) {
182 switch (child.getName()) {
184 assertEquals("provider3URL", child.getValue());
187 assertEquals("provider3type", child.getValue());
190 assertEquals("provider3Name", child.getValue());
193 fail("Unknown child of " + node.getName() + " with value " + child.toString());
198 fail("Unknown provider " + node.toString());
201 // System.out.println(nodes);
205 * Test a multi-dimensional namespace (3X3X3)
207 @SuppressWarnings("nls")
209 public void testMultiLevelNamespace() {
210 nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
212 assertNotNull(nodes);
213 assertFalse(nodes.isEmpty());
215 assertEquals(3, nodes.size());
216 for (Node node : nodes) {
217 assertNull(node.getValue());
218 List<Node> children = node.getChildren();
219 assertNotNull(children);
220 assertEquals(3, children.size());
221 for (Node child : children) {
222 assertNull(child.getValue());
223 List<Node> grandChildren = child.getChildren();
224 assertNotNull(grandChildren);
225 assertEquals(3, grandChildren.size());
226 for (Node greatGrandChild : grandChildren) {
227 assertNull(greatGrandChild.getValue());
228 List<Node> greatGrandChildren = greatGrandChild.getChildren();
229 assertNotNull(greatGrandChildren);
230 assertEquals(1, greatGrandChildren.size());
234 // System.out.println(nodes);