1 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
3 import static org.junit.Assert.*;
9 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
10 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.*;
13 import org.opendaylight.yangtools.yang.model.api.Module;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public class XmlLeafrefToCnSnTest {
18 private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class);
21 * top level element represents container. second level element is list with
25 public void testXmlDataContainer() {
26 CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-container.xml", false,
27 XmlToCompositeNodeProvider.INSTANCE);
28 assertNotNull(compNode);
29 Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-container-yang");
31 assertNotNull(modules);
32 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
34 String nameSpace = "data:container:yang";
35 assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
37 verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
38 verifyCommonPartAOfXml(compNode, "", nameSpace);
41 private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
42 assertEquals("cont", compNode.getNodeType().getLocalName());
44 SimpleNode<?> lf2 = null;
45 SimpleNode<?> lf3 = null;
47 for (Node<?> child : compNode.getChildren()) {
50 if (child instanceof SimpleNode<?>) {
51 SimpleNode<?> childSimple = (SimpleNode<?>) child;
52 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
54 found = found | (1 << 0);
55 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
57 found = found | (1 << 1);
60 assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
63 assertEquals("", lf2.getValue());
64 assertEquals(null, lf3.getValue());
68 public void testXmlDataList() {
69 CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-list.xml", false,
70 XmlToCompositeNodeProvider.INSTANCE);
71 assertNotNull(compNode);
73 Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang");
74 assertNotNull(modules);
76 TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
78 String nameSpaceList = "data:list:yang";
79 String nameSpaceCont = "data:container:yang";
80 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
81 assertEquals("cont", compNode.getNodeType().getLocalName());
82 assertEquals(3, compNode.getChildren().size());
83 CompositeNode lst1_1 = null;
84 CompositeNode lst1_2 = null;
86 for (Node<?> node : compNode.getChildren()) {
87 if (node.getNodeType().getLocalName().equals("lf1")) {
88 assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
89 assertTrue(node instanceof SimpleNode<?>);
90 assertEquals("lf1", node.getValue());
92 assertTrue(node instanceof CompositeNode);
93 switch (loopCount++) {
95 lst1_1 = (CompositeNode) node;
98 lst1_2 = (CompositeNode) node;
101 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
105 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
109 SimpleNode<?> lflst11 = null;
110 CompositeNode cont11 = null;
111 for (Node<?> node : lst1_2.getChildren()) {
112 String nodeName = node.getNodeType().getLocalName();
113 if (nodeName.equals("lflst11")) {
114 assertTrue(node instanceof SimpleNode<?>);
115 lflst11 = (SimpleNode<?>) node;
117 } else if (nodeName.equals("cont11")) {
118 assertTrue(node instanceof CompositeNode);
119 cont11 = (CompositeNode) node;
121 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
123 assertEquals("221", lflst11.getValue());
125 assertEquals(1, cont11.getChildren().size());
126 assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
127 SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
128 assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
129 assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
130 assertEquals((short) 100, cont11_lf111.getValue());
136 public void testXmlEmptyData() {
137 CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/empty-data.xml", true,
138 XmlToCompositeNodeProvider.INSTANCE);
139 assertEquals("cont", compNode.getNodeType().getLocalName());
140 SimpleNode<?> lf1 = null;
141 SimpleNode<?> lflst1_1 = null;
142 SimpleNode<?> lflst1_2 = null;
143 CompositeNode lst1 = null;
145 for (Node<?> node : compNode.getChildren()) {
146 if (node.getNodeType().getLocalName().equals("lf1")) {
147 assertTrue(node instanceof SimpleNode<?>);
148 lf1 = (SimpleNode<?>) node;
149 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
150 assertTrue(node instanceof SimpleNode<?>);
152 switch (lflst1Count++) {
154 lflst1_1 = (SimpleNode<?>) node;
157 lflst1_2 = (SimpleNode<?>) node;
160 } else if (node.getNodeType().getLocalName().equals("lst1")) {
161 assertTrue(node instanceof CompositeNode);
162 lst1 = (CompositeNode) node;
167 assertNotNull(lflst1_1);
168 assertNotNull(lflst1_2);
171 assertEquals("", lf1.getValue());
172 assertEquals("", lflst1_1.getValue());
173 assertEquals("", lflst1_2.getValue());
174 assertEquals(1, lst1.getChildren().size());
175 assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName());
177 assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
178 assertEquals("", lst1.getChildren().get(0).getValue());
183 * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
186 public void testIdentityrefNmspcInElement() {
187 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-element.xml", "/xml-to-cnsn/identityref",
188 "identityref-module", "cont", 2, "iden", "identity:module");
193 * Test case like <lf11 xmlns="namespace1"
194 * xmlns:x="namespace">identity</lf11>
198 public void testIdentityrefDefaultNmspcInElement() {
199 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-element.xml",
200 "/xml-to-cnsn/identityref/yang-augments", "general-module", "cont", 3, "iden", "identityref:module");
205 * Test case like <cont1 xmlns="namespace1"> <lf11
206 * xmlns:x="namespace">identity</lf11> </cont1>
209 public void testIdentityrefDefaultNmspcInParrentElement() {
210 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-parrent-element.xml",
211 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module");
216 * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace">
217 * <lf11>x:identity</lf11> </cont1>
220 public void testIdentityrefNmspcInParrentElement() {
221 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-parrent-element.xml",
222 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "z:namespace");
228 * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11>
232 public void testIdentityrefNoNmspcValueWithPrefix() {
233 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-with-prefix.xml",
234 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "x:iden", "identityref:module");
239 * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11>
243 public void testIdentityrefNoNmspcValueWithoutPrefix() {
244 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-without-prefix.xml",
245 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module");
248 private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) {
249 SimpleNode<?> lf1suf = null;
250 SimpleNode<?> lflst1suf_1 = null;
251 SimpleNode<?> lflst1suf_2 = null;
252 SimpleNode<?> lflst1suf_3 = null;
253 CompositeNode cont1suf = null;
254 CompositeNode lst1suf = null;
258 for (Node<?> node : compNode.getChildren()) {
259 String localName = node.getNodeType().getLocalName();
260 if (localName.equals("lf1" + suf)) {
261 assertTrue(node instanceof SimpleNode<?>);
262 lf1suf = (SimpleNode<?>) node;
263 } else if (localName.equals("lflst1" + suf)) {
264 assertTrue(node instanceof SimpleNode<?>);
265 switch (lflstCount++) {
267 lflst1suf_1 = (SimpleNode<?>) node;
270 lflst1suf_2 = (SimpleNode<?>) node;
273 lflst1suf_3 = (SimpleNode<?>) node;
276 } else if (localName.equals("lst1" + suf)) {
277 assertTrue(node instanceof CompositeNode);
278 lst1suf = (CompositeNode) node;
279 } else if (localName.equals("cont1" + suf)) {
280 assertTrue(node instanceof CompositeNode);
281 cont1suf = (CompositeNode) node;
283 assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
286 assertNotNull(lf1suf);
287 assertNotNull(lflst1suf_1);
288 assertNotNull(lflst1suf_2);
289 assertNotNull(lflst1suf_3);
290 assertNotNull(lst1suf);
291 assertNotNull(cont1suf);
293 assertEquals("str0", lf1suf.getValue());
294 assertEquals("121", lflst1suf_1.getValue());
295 assertEquals("131", lflst1suf_2.getValue());
296 assertEquals("str1", lflst1suf_3.getValue());
298 assertEquals(1, lst1suf.getChildren().size());
300 assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
301 SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().get(0);
302 assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString());
303 assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName());
304 assertEquals("str2", lst11_lf11.getValue());
306 assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode<?>);
307 SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().get(0);
308 assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString());
309 assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName());
310 assertEquals((short) 100, cont1_lf11.getValue());
313 private void testIdentityrefToCnSn(String xmlPath, String yangPath, String moduleName, String schemaName,
314 int moduleCount, String resultLocalName, String resultNamespace) {
315 CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlPath, false, XmlToCompositeNodeProvider.INSTANCE);
316 assertNotNull(compositeNode);
318 Set<Module> modules = TestUtils.loadModulesFrom(yangPath);
319 assertEquals(moduleCount, modules.size());
321 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
323 SimpleNode<?> lf11 = getLf11(compositeNode);
324 assertTrue(lf11.getValue() instanceof QName);
325 QName qName = (QName) lf11.getValue();
326 assertEquals(resultLocalName, qName.getLocalName());
327 assertEquals(resultNamespace, qName.getNamespace().toString());
330 private SimpleNode<?> getLf11(CompositeNode compositeNode) {
331 assertEquals("cont", compositeNode.getNodeType().getLocalName());
333 List<Node<?>> childs = compositeNode.getChildren();
334 assertEquals(1, childs.size());
335 Node<?> nd = childs.iterator().next();
336 assertTrue(nd instanceof CompositeNode);
337 assertEquals("cont1", nd.getNodeType().getLocalName());
339 childs = ((CompositeNode) nd).getChildren();
340 SimpleNode<?> lf11 = null;
341 for (Node<?> child : childs) {
342 assertTrue(child instanceof SimpleNode);
343 if (child.getNodeType().getLocalName().equals("lf11")) {
344 lf11 = (SimpleNode<?>) child;