Change nexus values to properties
[appc.git] / appc-common / src / test / java / org / openecomp / appc / util / TestStructuredPropertyHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.util;
23
24 import static org.junit.Assert.*;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29
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;
34
35 /**
36  * This class is used to test the structured property helper class.
37  * <p>
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
42  * its operation.
43  * </p>
44  *
45  */
46 public class TestStructuredPropertyHelper {
47
48     /**
49      * The properties to be parsed
50      */
51     private Properties properties;
52
53     /**
54      * The result of parsing the properties
55      */
56     private List<Node> nodes = new ArrayList<>();
57
58     /**
59      * Initialize the test environment
60      */
61     @SuppressWarnings("nls")
62     @Before
63     public void setup() {
64         nodes.clear();
65
66         properties = new Properties();
67
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");
77
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");
105
106         properties.setProperty("other.property", "bogus");
107         properties.setProperty("yet.another.property", "bogus");
108         properties.setProperty("simpleProperty", "bogus");
109
110     }
111
112     /**
113      * Test that a simple namespace works
114      */
115     @SuppressWarnings("nls")
116     @Test
117     public void testSimpleNamespace() {
118         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "provider");
119
120         assertNotNull(nodes);
121         assertFalse(nodes.isEmpty());
122
123         assertEquals(3, nodes.size());
124
125         List<Node> children;
126         for (Node node : nodes) {
127             switch (node.getName()) {
128                 case "provider1":
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()) {
135                             case "URL":
136                                 assertEquals("provider1URL", child.getValue());
137                                 break;
138                             case "type":
139                                 assertEquals("provider1type", child.getValue());
140                                 break;
141                             case "name":
142                                 assertEquals("provider1Name", child.getValue());
143                                 break;
144                             default:
145                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
146                         }
147                     }
148                     break;
149                 case "provider2":
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()) {
156                             case "URL":
157                                 assertEquals("provider2URL", child.getValue());
158                                 break;
159                             case "type":
160                                 assertEquals("provider2type", child.getValue());
161                                 break;
162                             case "name":
163                                 assertEquals("provider2Name", child.getValue());
164                                 break;
165                             default:
166                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
167                         }
168                     }
169                     break;
170                 case "provider3":
171                     /*
172                      * Note that the helper normalizes any ordinal suffixes (003 became 3)
173                      */
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()) {
180                             case "URL":
181                                 assertEquals("provider3URL", child.getValue());
182                                 break;
183                             case "type":
184                                 assertEquals("provider3type", child.getValue());
185                                 break;
186                             case "name":
187                                 assertEquals("provider3Name", child.getValue());
188                                 break;
189                             default:
190                                 fail("Unknown child of " + node.getName() + " with value " + child.toString());
191                         }
192                     }
193                     break;
194                 default:
195                     fail("Unknown provider " + node.toString());
196             }
197         }
198         // System.out.println(nodes);
199     }
200
201     /**
202      * Test a multi-dimensional namespace (3X3X3)
203      */
204     @SuppressWarnings("nls")
205     @Test
206     public void testMultiLevelNamespace() {
207         nodes = StructuredPropertyHelper.getStructuredProperties(properties, "node");
208
209         assertNotNull(nodes);
210         assertFalse(nodes.isEmpty());
211
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());
228                 }
229             }
230         }
231         // System.out.println(nodes);
232     }
233 }