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