2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import java.util.List;
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;
28 public class XmlLeafrefToCnSnTest {
29 private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class);
32 * top level element represents container. second level element is list with two elements.
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;
41 assertNotNull(compNode);
42 Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-container-yang");
44 assertNotNull(modules);
45 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
47 String nameSpace = "data:container:yang";
48 assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
50 verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
51 verifyCommonPartAOfXml(compNode, "", nameSpace);
54 private void verifyNullAndEmptyStringSingleNode(final CompositeNode compNode, final String nameSpace) {
55 assertEquals("cont", compNode.getNodeType().getLocalName());
57 SimpleNode<?> lf2 = null;
58 SimpleNode<?> lf3 = null;
60 for (Node<?> child : compNode.getValue()) {
63 if (child instanceof SimpleNode<?>) {
64 SimpleNode<?> childSimple = (SimpleNode<?>) child;
65 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
67 found = found | (1 << 0);
68 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
70 found = found | (1 << 1);
73 assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
76 assertEquals("", lf2.getValue());
77 assertEquals(null, lf3.getValue());
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;
88 Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang");
89 assertNotNull(modules);
91 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
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;
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());
107 assertTrue(nd instanceof CompositeNode);
108 switch (loopCount++) {
110 lst1_1 = (CompositeNode) nd;
113 lst1_2 = (CompositeNode) nd;
116 assertEquals(nameSpaceCont, nd.getNodeType().getNamespace().toString());
120 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
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;
132 } else if (nodeName.equals("cont11")) {
133 assertTrue(nd instanceof CompositeNode);
134 cont11 = (CompositeNode) nd;
136 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
138 assertEquals("221", lflst11.getValue());
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());
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;
157 assertEquals("cont", compNode.getNodeType().getLocalName());
158 SimpleNode<?> lf1 = null;
159 SimpleNode<?> lflst1_1 = null;
160 SimpleNode<?> lflst1_2 = null;
161 CompositeNode lst1 = null;
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<?>);
170 switch (lflst1Count++) {
172 lflst1_1 = (SimpleNode<?>) nd;
175 lflst1_2 = (SimpleNode<?>) nd;
178 } else if (nd.getNodeType().getLocalName().equals("lst1")) {
179 assertTrue(nd instanceof CompositeNode);
180 lst1 = (CompositeNode) nd;
185 assertNotNull(lflst1_1);
186 assertNotNull(lflst1_2);
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());
195 assertTrue(lst1.getValue().get(0) instanceof SimpleNode<?>);
196 assertEquals("", lst1.getValue().get(0).getValue());
201 * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
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");
211 * Test case like <lf11 xmlns="namespace1" xmlns:x="namespace">identity</lf11>
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");
222 * Test case like <cont1 xmlns="namespace1"> <lf11 xmlns:x="namespace">identity</lf11> </cont1>
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");
232 * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace"> <lf11>x:identity</lf11> </cont1>
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");
243 * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11> </cont1>
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");
253 * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11> </cont1>
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");
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;
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++) {
280 lflst1suf_1 = (SimpleNode<?>) node;
283 lflst1suf_2 = (SimpleNode<?>) node;
286 lflst1suf_3 = (SimpleNode<?>) node;
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;
296 assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
299 assertNotNull(lf1suf);
300 assertNotNull(lflst1suf_1);
301 assertNotNull(lflst1suf_2);
302 assertNotNull(lflst1suf_3);
303 assertNotNull(lst1suf);
304 assertNotNull(cont1suf);
306 assertEquals("str0", lf1suf.getValue());
307 assertEquals("121", lflst1suf_1.getValue());
308 assertEquals("131", lflst1suf_2.getValue());
309 assertEquals("str1", lflst1suf_3.getValue());
311 assertEquals(1, lst1suf.getValue().size());
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());
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());
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;
333 Set<Module> modules = TestUtils.loadModulesFrom(yangPath);
334 assertEquals(moduleCount, modules.size());
336 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
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());
345 private SimpleNode<?> getLf11(final CompositeNode compositeNode) {
346 assertEquals("cont", compositeNode.getNodeType().getLocalName());
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());
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;