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;
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;
29 public class XmlLeafrefToCnSnTest {
30 private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class);
33 * top level element represents container. second level element is list with
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");
43 assertNotNull(modules);
44 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
46 String nameSpace = "data:container:yang";
47 assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
49 verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
50 verifyCommonPartAOfXml(compNode, "", nameSpace);
53 private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
54 assertEquals("cont", compNode.getNodeType().getLocalName());
56 SimpleNode<?> lf2 = null;
57 SimpleNode<?> lf3 = null;
59 for (Node<?> child : compNode.getChildren()) {
62 if (child instanceof SimpleNode<?>) {
63 SimpleNode<?> childSimple = (SimpleNode<?>) child;
64 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
66 found = found | (1 << 0);
67 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
69 found = found | (1 << 1);
72 assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
75 assertEquals("", lf2.getValue());
76 assertEquals(null, lf3.getValue());
80 public void testXmlDataList() {
81 CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-list.xml", false,
82 XmlToCompositeNodeProvider.INSTANCE);
83 assertNotNull(compNode);
85 Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang");
86 assertNotNull(modules);
88 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
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;
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());
104 assertTrue(node instanceof CompositeNode);
105 switch (loopCount++) {
107 lst1_1 = (CompositeNode) node;
110 lst1_2 = (CompositeNode) node;
113 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
117 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
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;
129 } else if (nodeName.equals("cont11")) {
130 assertTrue(node instanceof CompositeNode);
131 cont11 = (CompositeNode) node;
133 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
135 assertEquals("221", lflst11.getValue());
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());
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;
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<?>);
164 switch (lflst1Count++) {
166 lflst1_1 = (SimpleNode<?>) node;
169 lflst1_2 = (SimpleNode<?>) node;
172 } else if (node.getNodeType().getLocalName().equals("lst1")) {
173 assertTrue(node instanceof CompositeNode);
174 lst1 = (CompositeNode) node;
179 assertNotNull(lflst1_1);
180 assertNotNull(lflst1_2);
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());
189 assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
190 assertEquals("", lst1.getChildren().get(0).getValue());
195 * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
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");
205 * Test case like <lf11 xmlns="namespace1"
206 * xmlns:x="namespace">identity</lf11>
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");
217 * Test case like <cont1 xmlns="namespace1"> <lf11
218 * xmlns:x="namespace">identity</lf11> </cont1>
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");
228 * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace">
229 * <lf11>x:identity</lf11> </cont1>
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");
240 * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11>
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");
251 * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11>
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");
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;
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++) {
279 lflst1suf_1 = (SimpleNode<?>) node;
282 lflst1suf_2 = (SimpleNode<?>) node;
285 lflst1suf_3 = (SimpleNode<?>) node;
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;
295 assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
298 assertNotNull(lf1suf);
299 assertNotNull(lflst1suf_1);
300 assertNotNull(lflst1suf_2);
301 assertNotNull(lflst1suf_3);
302 assertNotNull(lst1suf);
303 assertNotNull(cont1suf);
305 assertEquals("str0", lf1suf.getValue());
306 assertEquals("121", lflst1suf_1.getValue());
307 assertEquals("131", lflst1suf_2.getValue());
308 assertEquals("str1", lflst1suf_3.getValue());
310 assertEquals(1, lst1suf.getChildren().size());
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());
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());
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);
330 Set<Module> modules = TestUtils.loadModulesFrom(yangPath);
331 assertEquals(moduleCount, modules.size());
333 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
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());
342 private SimpleNode<?> getLf11(CompositeNode compositeNode) {
343 assertEquals("cont", compositeNode.getNodeType().getLocalName());
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());
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;