1 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
3 import static org.junit.Assert.*;
6 import java.net.URISyntaxException;
10 import javax.ws.rs.WebApplicationException;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
14 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
15 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.*;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class XmlToCnSnTest {
24 private static final Logger LOG = LoggerFactory.getLogger(XmlToCnSnTest.class);
27 * top level element represents container. second level element is list with
31 public void testXmlDataContainer() {
32 CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-container.xml", false);
33 assertNotNull(compNode);
34 DataSchemaNode dataSchemaNode = null;
36 dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-cnsn/data-container-yang");
37 } catch (FileNotFoundException e) {
38 LOG.error(e.getMessage());
42 assertNotNull(dataSchemaNode);
43 TestUtils.supplementNamespace(dataSchemaNode, compNode);
45 String nameSpace = "data:container:yang";
46 assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
48 verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
49 verifyCommonPartAOfXml(compNode, "", nameSpace);
52 private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
53 assertEquals("cont", compNode.getNodeType().getLocalName());
55 SimpleNode<?> lf2 = null;
56 SimpleNode<?> lf3 = null;
58 for (Node<?> child : compNode.getChildren()) {
61 if (child instanceof SimpleNode<?>) {
62 SimpleNode<?> childSimple = (SimpleNode<?>) child;
63 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
65 found = found | (1 << 0);
66 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
68 found = found | (1 << 1);
71 assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
74 assertEquals("", lf2.getValue());
75 assertEquals(null, lf3.getValue());
79 public void testXmlDataList() {
80 CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-list.xml", false);
81 assertNotNull(compNode);
83 DataSchemaNode dataSchemaNode = null;
85 dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-cnsn/data-list-yang", "data-container-yang");
86 } catch (FileNotFoundException e) {
87 LOG.error(e.getMessage());
89 assertNotNull(dataSchemaNode);
90 TestUtils.supplementNamespace(dataSchemaNode, compNode);
92 String nameSpaceList = "data:list:yang";
93 String nameSpaceCont = "data:container:yang";
94 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
95 assertEquals("cont", compNode.getNodeType().getLocalName());
96 assertEquals(3, compNode.getChildren().size());
97 CompositeNode lst1_1 = null;
98 CompositeNode lst1_2 = null;
100 for (Node<?> node : compNode.getChildren()) {
101 if (node.getNodeType().getLocalName().equals("lf1")) {
102 assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
103 assertTrue(node instanceof SimpleNode<?>);
104 assertEquals("lf1", node.getValue());
106 assertTrue(node instanceof CompositeNode);
107 switch (loopCount++) {
109 lst1_1 = (CompositeNode) node;
112 lst1_2 = (CompositeNode) node;
115 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
119 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
123 SimpleNode<?> lflst11 = null;
124 CompositeNode cont11 = null;
125 for (Node<?> node : lst1_2.getChildren()) {
126 String nodeName = node.getNodeType().getLocalName();
127 if (nodeName.equals("lflst11")) {
128 assertTrue(node instanceof SimpleNode<?>);
129 lflst11 = (SimpleNode<?>) node;
131 } else if (nodeName.equals("cont11")) {
132 assertTrue(node instanceof CompositeNode);
133 cont11 = (CompositeNode) node;
135 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
137 assertEquals("221", lflst11.getValue());
139 assertEquals(1, cont11.getChildren().size());
140 assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
141 SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
142 assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
143 assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
144 assertEquals((short) 100, cont11_lf111.getValue());
150 public void testXmlEmptyData() {
151 CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/empty-data.xml", true);
152 assertEquals("cont", compNode.getNodeType().getLocalName());
153 SimpleNode<?> lf1 = null;
154 SimpleNode<?> lflst1_1 = null;
155 SimpleNode<?> lflst1_2 = null;
156 CompositeNode lst1 = null;
158 for (Node<?> node : compNode.getChildren()) {
159 if (node.getNodeType().getLocalName().equals("lf1")) {
160 assertTrue(node instanceof SimpleNode<?>);
161 lf1 = (SimpleNode<?>) node;
162 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
163 assertTrue(node instanceof SimpleNode<?>);
165 switch (lflst1Count++) {
167 lflst1_1 = (SimpleNode<?>) node;
170 lflst1_2 = (SimpleNode<?>) node;
173 } else if (node.getNodeType().getLocalName().equals("lst1")) {
174 assertTrue(node instanceof CompositeNode);
175 lst1 = (CompositeNode) node;
180 assertNotNull(lflst1_1);
181 assertNotNull(lflst1_2);
184 assertEquals("", lf1.getValue());
185 assertEquals("", lflst1_1.getValue());
186 assertEquals("", lflst1_2.getValue());
187 assertEquals(1, lst1.getChildren().size());
188 assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName());
190 assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
191 assertEquals("", lst1.getChildren().get(0).getValue());
196 * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
199 public void testIdentityrefNmspcInElement() {
200 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-element.xml", "/xml-to-cnsn/identityref",
201 "identityref-module", "cont", 2, "iden", "identity:module");
206 * Test case like <lf11 xmlns="namespace1"
207 * xmlns:x="namespace">identity</lf11>
211 public void testIdentityrefDefaultNmspcInElement() {
212 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-element.xml",
213 "/xml-to-cnsn/identityref/yang-augments", "general-module", "cont", 3, "iden", "identityref:module");
218 * Test case like <cont1 xmlns="namespace1"> <lf11
219 * xmlns:x="namespace">identity</lf11> </cont1>
222 public void testIdentityrefDefaultNmspcInParrentElement() {
223 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-parrent-element.xml",
224 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module");
229 * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace">
230 * <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");
241 * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11>
245 public void testIdentityrefNoNmspcValueWithPrefix() {
246 testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-with-prefix.xml",
247 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "x:iden", "identityref:module");
252 * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11>
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(CompositeNode compNode, String suf, 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.getChildren()) {
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.getChildren().size());
313 assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
314 SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().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.getChildren().get(0) instanceof SimpleNode<?>);
320 SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().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 CompositeNode compositeNodeFromXml(String xmlPath, boolean dummyNamespaces) {
327 XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
329 InputStream xmlStream = XmlToCnSnTest.class.getResourceAsStream(xmlPath);
330 CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
331 if (dummyNamespaces) {
333 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
334 return ((CompositeNodeWrapper) compositeNode).unwrap();
335 } catch (URISyntaxException e) {
336 LOG.error(e.getMessage());
337 assertTrue(e.getMessage(), false);
340 return compositeNode;
342 } catch (WebApplicationException | IOException e) {
343 LOG.error(e.getMessage());
349 private void testIdentityrefToCnSn(String xmlPath, String yangPath, String moduleName, String schemaName,
350 int moduleCount, String resultLocalName, String resultNamespace) {
351 CompositeNode compositeNode = compositeNodeFromXml(xmlPath, false);
352 assertNotNull(compositeNode);
354 Set<Module> modules = TestUtils.resolveModules(yangPath);
355 assertEquals(moduleCount, modules.size());
356 Module module = TestUtils.resolveModule(moduleName, modules);
357 assertNotNull(module);
358 DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null);
359 assertNotNull(dataSchemaNode);
361 TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, moduleName + ":" + schemaName);
363 SimpleNode<?> lf11 = getLf11(compositeNode);
364 assertTrue(lf11.getValue() instanceof QName);
365 QName qName = (QName) lf11.getValue();
366 assertEquals(resultLocalName, qName.getLocalName());
367 assertEquals(resultNamespace, qName.getNamespace().toString());
371 private SimpleNode<?> getLf11(CompositeNode compositeNode) {
372 assertEquals("cont", compositeNode.getNodeType().getLocalName());
374 List<Node<?>> childs = compositeNode.getChildren();
375 assertEquals(1, childs.size());
376 Node<?> nd = childs.iterator().next();
377 assertTrue(nd instanceof CompositeNode);
378 assertEquals("cont1", nd.getNodeType().getLocalName());
380 childs = ((CompositeNode) nd).getChildren();
381 SimpleNode<?> lf11 = null;
382 for (Node<?> child : childs) {
383 assertTrue(child instanceof SimpleNode);
384 if (child.getNodeType().getLocalName().equals("lf11")) {
385 lf11 = (SimpleNode<?>) child;