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