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