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