Bulk-add copyright headers to java files
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / xml / to / cnsn / test / XmlLeafrefToCnSnTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.List;
15 import java.util.Set;
16
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
20 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.Node;
24 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class XmlLeafrefToCnSnTest {
30     private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class);
31
32     /**
33      * top level element represents container. second level element is list with
34      * two elements.
35      */
36     @Test
37     public void testXmlDataContainer() {
38         CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-container.xml", false,
39                 XmlToCompositeNodeProvider.INSTANCE);
40         assertNotNull(compNode);
41         Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-container-yang");
42
43         assertNotNull(modules);
44         TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
45
46         String nameSpace = "data:container:yang";
47         assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
48
49         verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
50         verifyCommonPartAOfXml(compNode, "", nameSpace);
51     }
52
53     private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
54         assertEquals("cont", compNode.getNodeType().getLocalName());
55
56         SimpleNode<?> lf2 = null;
57         SimpleNode<?> lf3 = null;
58         int found = 0;
59         for (Node<?> child : compNode.getChildren()) {
60             if (found == 0x3)
61                 break;
62             if (child instanceof SimpleNode<?>) {
63                 SimpleNode<?> childSimple = (SimpleNode<?>) child;
64                 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
65                     lf3 = childSimple;
66                     found = found | (1 << 0);
67                 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
68                     lf2 = childSimple;
69                     found = found | (1 << 1);
70                 }
71             }
72             assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
73         }
74
75         assertEquals("", lf2.getValue());
76         assertEquals(null, lf3.getValue());
77     }
78
79     @Test
80     public void testXmlDataList() {
81         CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-list.xml", false,
82                 XmlToCompositeNodeProvider.INSTANCE);
83         assertNotNull(compNode);
84
85         Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang");
86         assertNotNull(modules);
87
88         TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
89
90         String nameSpaceList = "data:list:yang";
91         String nameSpaceCont = "data:container:yang";
92         assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
93         assertEquals("cont", compNode.getNodeType().getLocalName());
94         assertEquals(3, compNode.getChildren().size());
95         CompositeNode lst1_1 = null;
96         CompositeNode lst1_2 = null;
97         int loopCount = 0;
98         for (Node<?> node : compNode.getChildren()) {
99             if (node.getNodeType().getLocalName().equals("lf1")) {
100                 assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
101                 assertTrue(node instanceof SimpleNode<?>);
102                 assertEquals("lf1", node.getValue());
103             } else {
104                 assertTrue(node instanceof CompositeNode);
105                 switch (loopCount++) {
106                 case 0:
107                     lst1_1 = (CompositeNode) node;
108                     break;
109                 case 1:
110                     lst1_2 = (CompositeNode) node;
111                     break;
112                 }
113                 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
114             }
115         }
116         // lst1_1
117         verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
118         // :lst1_1
119
120         // lst1_2
121         SimpleNode<?> lflst11 = null;
122         CompositeNode cont11 = null;
123         for (Node<?> node : lst1_2.getChildren()) {
124             String nodeName = node.getNodeType().getLocalName();
125             if (nodeName.equals("lflst11")) {
126                 assertTrue(node instanceof SimpleNode<?>);
127                 lflst11 = (SimpleNode<?>) node;
128
129             } else if (nodeName.equals("cont11")) {
130                 assertTrue(node instanceof CompositeNode);
131                 cont11 = (CompositeNode) node;
132             }
133             assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
134         }
135         assertEquals("221", lflst11.getValue());
136
137         assertEquals(1, cont11.getChildren().size());
138         assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
139         SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
140         assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
141         assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
142         assertEquals((short) 100, cont11_lf111.getValue());
143         // :lst1_2
144
145     }
146
147     @Test
148     public void testXmlEmptyData() {
149         CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/empty-data.xml", true,
150                 XmlToCompositeNodeProvider.INSTANCE);
151         assertEquals("cont", compNode.getNodeType().getLocalName());
152         SimpleNode<?> lf1 = null;
153         SimpleNode<?> lflst1_1 = null;
154         SimpleNode<?> lflst1_2 = null;
155         CompositeNode lst1 = null;
156         int lflst1Count = 0;
157         for (Node<?> node : compNode.getChildren()) {
158             if (node.getNodeType().getLocalName().equals("lf1")) {
159                 assertTrue(node instanceof SimpleNode<?>);
160                 lf1 = (SimpleNode<?>) node;
161             } else if (node.getNodeType().getLocalName().equals("lflst1")) {
162                 assertTrue(node instanceof SimpleNode<?>);
163
164                 switch (lflst1Count++) {
165                 case 0:
166                     lflst1_1 = (SimpleNode<?>) node;
167                     break;
168                 case 1:
169                     lflst1_2 = (SimpleNode<?>) node;
170                     break;
171                 }
172             } else if (node.getNodeType().getLocalName().equals("lst1")) {
173                 assertTrue(node instanceof CompositeNode);
174                 lst1 = (CompositeNode) node;
175             }
176         }
177
178         assertNotNull(lf1);
179         assertNotNull(lflst1_1);
180         assertNotNull(lflst1_2);
181         assertNotNull(lst1);
182
183         assertEquals("", lf1.getValue());
184         assertEquals("", lflst1_1.getValue());
185         assertEquals("", lflst1_2.getValue());
186         assertEquals(1, lst1.getChildren().size());
187         assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName());
188
189         assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
190         assertEquals("", lst1.getChildren().get(0).getValue());
191
192     }
193
194     /**
195      * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
196      */
197     @Test
198     public void testIdentityrefNmspcInElement() {
199         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-element.xml", "/xml-to-cnsn/identityref",
200                 "identityref-module", "cont", 2, "iden", "identity:module");
201     }
202
203     /**
204      * 
205      * Test case like <lf11 xmlns="namespace1"
206      * xmlns:x="namespace">identity</lf11>
207      */
208
209     @Test
210     public void testIdentityrefDefaultNmspcInElement() {
211         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-element.xml",
212                 "/xml-to-cnsn/identityref/yang-augments", "general-module", "cont", 3, "iden", "identityref:module");
213     }
214
215     /**
216      * 
217      * Test case like <cont1 xmlns="namespace1"> <lf11
218      * xmlns:x="namespace">identity</lf11> </cont1>
219      */
220     @Test
221     public void testIdentityrefDefaultNmspcInParrentElement() {
222         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-parrent-element.xml",
223                 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module");
224     }
225
226     /**
227      * 
228      * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace">
229      * <lf11>x:identity</lf11> </cont1>
230      */
231     @Ignore
232     @Test
233     public void testIdentityrefNmspcInParrentElement() {
234         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-parrent-element.xml",
235                 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "z:namespace");
236     }
237
238     /**
239      * 
240      * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11>
241      * </cont1>
242      */
243     @Test
244     public void testIdentityrefNoNmspcValueWithPrefix() {
245         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-with-prefix.xml",
246                 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "x:iden", "identityref:module");
247     }
248
249     /**
250      * 
251      * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11>
252      * </cont1>
253      */
254     @Test
255     public void testIdentityrefNoNmspcValueWithoutPrefix() {
256         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-without-prefix.xml",
257                 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module");
258     }
259
260     private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) {
261         SimpleNode<?> lf1suf = null;
262         SimpleNode<?> lflst1suf_1 = null;
263         SimpleNode<?> lflst1suf_2 = null;
264         SimpleNode<?> lflst1suf_3 = null;
265         CompositeNode cont1suf = null;
266         CompositeNode lst1suf = null;
267
268         int lflstCount = 0;
269
270         for (Node<?> node : compNode.getChildren()) {
271             String localName = node.getNodeType().getLocalName();
272             if (localName.equals("lf1" + suf)) {
273                 assertTrue(node instanceof SimpleNode<?>);
274                 lf1suf = (SimpleNode<?>) node;
275             } else if (localName.equals("lflst1" + suf)) {
276                 assertTrue(node instanceof SimpleNode<?>);
277                 switch (lflstCount++) {
278                 case 0:
279                     lflst1suf_1 = (SimpleNode<?>) node;
280                     break;
281                 case 1:
282                     lflst1suf_2 = (SimpleNode<?>) node;
283                     break;
284                 case 2:
285                     lflst1suf_3 = (SimpleNode<?>) node;
286                     break;
287                 }
288             } else if (localName.equals("lst1" + suf)) {
289                 assertTrue(node instanceof CompositeNode);
290                 lst1suf = (CompositeNode) node;
291             } else if (localName.equals("cont1" + suf)) {
292                 assertTrue(node instanceof CompositeNode);
293                 cont1suf = (CompositeNode) node;
294             }
295             assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
296         }
297
298         assertNotNull(lf1suf);
299         assertNotNull(lflst1suf_1);
300         assertNotNull(lflst1suf_2);
301         assertNotNull(lflst1suf_3);
302         assertNotNull(lst1suf);
303         assertNotNull(cont1suf);
304
305         assertEquals("str0", lf1suf.getValue());
306         assertEquals("121", lflst1suf_1.getValue());
307         assertEquals("131", lflst1suf_2.getValue());
308         assertEquals("str1", lflst1suf_3.getValue());
309
310         assertEquals(1, lst1suf.getChildren().size());
311
312         assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
313         SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().get(0);
314         assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString());
315         assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName());
316         assertEquals("str2", lst11_lf11.getValue());
317
318         assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode<?>);
319         SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().get(0);
320         assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString());
321         assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName());
322         assertEquals((short) 100, cont1_lf11.getValue());
323     }
324
325     private void testIdentityrefToCnSn(String xmlPath, String yangPath, String moduleName, String schemaName,
326             int moduleCount, String resultLocalName, String resultNamespace) {
327         CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlPath, false, XmlToCompositeNodeProvider.INSTANCE);
328         assertNotNull(compositeNode);
329
330         Set<Module> modules = TestUtils.loadModulesFrom(yangPath);
331         assertEquals(moduleCount, modules.size());
332
333         TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
334
335         SimpleNode<?> lf11 = getLf11(compositeNode);
336         assertTrue(lf11.getValue() instanceof QName);
337         QName qName = (QName) lf11.getValue();
338         assertEquals(resultLocalName, qName.getLocalName());
339         assertEquals(resultNamespace, qName.getNamespace().toString());
340     }
341
342     private SimpleNode<?> getLf11(CompositeNode compositeNode) {
343         assertEquals("cont", compositeNode.getNodeType().getLocalName());
344
345         List<Node<?>> childs = compositeNode.getChildren();
346         assertEquals(1, childs.size());
347         Node<?> nd = childs.iterator().next();
348         assertTrue(nd instanceof CompositeNode);
349         assertEquals("cont1", nd.getNodeType().getLocalName());
350
351         childs = ((CompositeNode) nd).getChildren();
352         SimpleNode<?> lf11 = null;
353         for (Node<?> child : childs) {
354             assertTrue(child instanceof SimpleNode);
355             if (child.getNodeType().getLocalName().equals("lf11")) {
356                 lf11 = (SimpleNode<?>) child;
357             }
358         }
359         assertNotNull(lf11);
360         return lf11;
361     }
362
363 }