Applying license changes to all files
[appc.git] / appc-common / src / test / java / org / openecomp / appc / util / TestStructuredPropertyHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.util;
26
27 import static org.junit.Assert.*;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32
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;
37
38 /**
39  * This class is used to test the structured property helper class.
40  * <p>
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
45  * its operation.
46  * </p>
47  *
48  */
49 public class TestStructuredPropertyHelper {
50
51     /**
52      * The properties to be parsed
53      */
54     private Properties properties;
55
56     /**
57      * The result of parsing the properties
58      */
59     private List<Node> nodes = new ArrayList<>();
60
61     /**
62      * Initialize the test environment
63      */
64     @SuppressWarnings("nls")
65     @Before
66     public void setup() {
67         nodes.clear();
68
69         properties = new Properties();
70
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");
80
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");
108
109         properties.setProperty("other.property", "bogus");
110         properties.setProperty("yet.another.property", "bogus");
111         properties.setProperty("simpleProperty", "bogus");
112
113     }
114
115     /**
116      * Test that a simple namespace works
117      */
118     @SuppressWarnings("nls")
119     @Test
120     public void testSimpleNamespace() {
121         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
122
123         assertNotNull(nodes);
124         assertFalse(nodes.isEmpty());
125
126         assertEquals(3, nodes.size());
127
128         List<Node> children;
129         for (Node node : nodes) {
130             switch (node.getName()) {
131                 case "provider1":
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()) {
138                             case "URL":
139                                 assertEquals("provider1URL", child.getValue());
140                                 break;
141                             case "type":
142                                 assertEquals("provider1type", child.getValue());
143                                 break;
144                             case "name":
145                                 assertEquals("provider1Name", child.getValue());
146                                 break;
147                             default:
148                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
149                         }
150                     }
151                     break;
152                 case "provider2":
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()) {
159                             case "URL":
160                                 assertEquals("provider2URL", child.getValue());
161                                 break;
162                             case "type":
163                                 assertEquals("provider2type", child.getValue());
164                                 break;
165                             case "name":
166                                 assertEquals("provider2Name", child.getValue());
167                                 break;
168                             default:
169                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
170                         }
171                     }
172                     break;
173                 case "provider3":
174                     /*
175                      * Note that the helper normalizes any ordinal suffixes (003 became 3)
176                      */
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()) {
183                             case "URL":
184                                 assertEquals("provider3URL", child.getValue());
185                                 break;
186                             case "type":
187                                 assertEquals("provider3type", child.getValue());
188                                 break;
189                             case "name":
190                                 assertEquals("provider3Name", child.getValue());
191                                 break;
192                             default:
193                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
194                         }
195                     }
196                     break;
197                 default:
198                     fail("Unknown provider " + node.toString());
199             }
200         }
201         // System.out.println(nodes);
202     }
203
204     /**
205      * Test a multi-dimensional namespace (3X3X3)
206      */
207     @SuppressWarnings("nls")
208     @Test
209     public void testMultiLevelNamespace() {
210         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
211
212         assertNotNull(nodes);
213         assertFalse(nodes.isEmpty());
214
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());
231                 }
232             }
233         }
234         // System.out.println(nodes);
235     }
236 }