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 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");
41 assertNotNull(modules);
42 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
44 String nameSpace = "data:container:yang";
45 assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
47 verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
48 verifyCommonPartAOfXml(compNode, "", nameSpace);
51 private void verifyNullAndEmptyStringSingleNode(final CompositeNode compNode, final String nameSpace) {
52 assertEquals("cont", compNode.getNodeType().getLocalName());
54 SimpleNode<?> lf2 = null;
55 SimpleNode<?> lf3 = null;
57 for (Node<?> child : compNode.getValue()) {
60 if (child instanceof SimpleNode<?>) {
61 SimpleNode<?> childSimple = (SimpleNode<?>) child;
62 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
64 found = found | (1 << 0);
65 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
67 found = found | (1 << 1);
70 assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
73 assertEquals("", lf2.getValue());
74 assertEquals(null, lf3.getValue());
78 public void testXmlDataList() {
79 CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-list.xml", false,
80 XmlToCompositeNodeProvider.INSTANCE);
81 assertNotNull(compNode);
83 Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang");
84 assertNotNull(modules);
86 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
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;
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());
102 assertTrue(node instanceof CompositeNode);
103 switch (loopCount++) {
105 lst1_1 = (CompositeNode) node;
108 lst1_2 = (CompositeNode) node;
111 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
115 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
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;
127 } else if (nodeName.equals("cont11")) {
128 assertTrue(node instanceof CompositeNode);
129 cont11 = (CompositeNode) node;
131 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
133 assertEquals("221", lflst11.getValue());
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());
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;
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<?>);
162 switch (lflst1Count++) {
164 lflst1_1 = (SimpleNode<?>) node;
167 lflst1_2 = (SimpleNode<?>) node;
170 } else if (node.getNodeType().getLocalName().equals("lst1")) {
171 assertTrue(node instanceof CompositeNode);
172 lst1 = (CompositeNode) node;
177 assertNotNull(lflst1_1);
178 assertNotNull(lflst1_2);
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());
187 assertTrue(lst1.getValue().get(0) instanceof SimpleNode<?>);
188 assertEquals("", lst1.getValue().get(0).getValue());
193 * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
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");
203 * Test case like <lf11 xmlns="namespace1" xmlns:x="namespace">identity</lf11>
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");
214 * Test case like <cont1 xmlns="namespace1"> <lf11 xmlns:x="namespace">identity</lf11> </cont1>
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");
224 * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace"> <lf11>x:identity</lf11> </cont1>
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");
235 * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11> </cont1>
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");
245 * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11> </cont1>
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");
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;
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++) {
272 lflst1suf_1 = (SimpleNode<?>) node;
275 lflst1suf_2 = (SimpleNode<?>) node;
278 lflst1suf_3 = (SimpleNode<?>) node;
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;
288 assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
291 assertNotNull(lf1suf);
292 assertNotNull(lflst1suf_1);
293 assertNotNull(lflst1suf_2);
294 assertNotNull(lflst1suf_3);
295 assertNotNull(lst1suf);
296 assertNotNull(cont1suf);
298 assertEquals("str0", lf1suf.getValue());
299 assertEquals("121", lflst1suf_1.getValue());
300 assertEquals("131", lflst1suf_2.getValue());
301 assertEquals("str1", lflst1suf_3.getValue());
303 assertEquals(1, lst1suf.getValue().size());
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());
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());
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);
323 Set<Module> modules = TestUtils.loadModulesFrom(yangPath);
324 assertEquals(moduleCount, modules.size());
326 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
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());
335 private SimpleNode<?> getLf11(final CompositeNode compositeNode) {
336 assertEquals("cont", compositeNode.getNodeType().getLocalName());
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());
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;