1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
8 import java.net.URISyntaxException;
10 import javax.ws.rs.WebApplicationException;
13 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
14 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
15 import org.opendaylight.yangtools.yang.data.api.*;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 public class FromXmlToCompositeNode {
20 private static Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNode.class);
23 * top level element represents container. second level element is list with
27 public void testXmlDataContainer() {
28 CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-container.xml", false);
29 assertNotNull(compNode);
30 DataSchemaNode dataSchemaNode = null;
32 dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-container-yang");
33 } catch (FileNotFoundException e) {
34 LOG.error(e.getMessage());
38 assertNotNull(dataSchemaNode);
39 TestUtils.supplementNamespace(dataSchemaNode, compNode);
41 String nameSpace = "data:container:yang";
42 assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
44 verifyCommonPartAOfXml(compNode, "", nameSpace);
48 public void testXmlDataList() {
49 CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false);
50 assertNotNull(compNode);
52 DataSchemaNode dataSchemaNode = null;
54 dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-list-yang",
55 "data-container-yang");
56 } catch (FileNotFoundException e) {
57 LOG.error(e.getMessage());
59 assertNotNull(dataSchemaNode);
60 TestUtils.supplementNamespace(dataSchemaNode, compNode);
62 String nameSpaceList = "data:list:yang";
63 String nameSpaceCont = "data:container:yang";
64 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
65 assertEquals("cont", compNode.getNodeType().getLocalName());
66 assertEquals(3, compNode.getChildren().size());
67 CompositeNode lst1_1 = null;
68 CompositeNode lst1_2 = null;
70 for (Node<?> node : compNode.getChildren()) {
71 if (node.getNodeType().getLocalName().equals("lf1")) {
72 assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
73 assertTrue(node instanceof SimpleNode<?>);
74 assertEquals("lf1", node.getValue());
76 assertTrue(node instanceof CompositeNode);
77 switch (loopCount++) {
79 lst1_1 = (CompositeNode) node;
82 lst1_2 = (CompositeNode) node;
85 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
89 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
93 SimpleNode<?> lflst11 = null;
94 CompositeNode cont11 = null;
95 for (Node<?> node : lst1_2.getChildren()) {
96 String nodeName = node.getNodeType().getLocalName();
97 if (nodeName.equals("lflst11")) {
98 assertTrue(node instanceof SimpleNode<?>);
99 lflst11 = (SimpleNode<?>) node;
101 } else if (nodeName.equals("cont11")) {
102 assertTrue(node instanceof CompositeNode);
103 cont11 = (CompositeNode) node;
105 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
107 assertEquals("221", lflst11.getValue());
109 assertEquals(1, cont11.getChildren().size());
110 assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
111 SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
112 assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
113 assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
114 assertEquals("100", cont11_lf111.getValue());
120 public void testXmlEmptyData() {
121 CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/empty-data.xml", true);
122 assertEquals("cont", compNode.getNodeType().getLocalName());
123 SimpleNode<?> lf1 = null;
124 SimpleNode<?> lflst1_1 = null;
125 SimpleNode<?> lflst1_2 = null;
126 CompositeNode lst1 = null;
128 for (Node<?> node : compNode.getChildren()) {
129 if (node.getNodeType().getLocalName().equals("lf1")) {
130 assertTrue(node instanceof SimpleNode<?>);
131 lf1 = (SimpleNode<?>) node;
132 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
133 assertTrue(node instanceof SimpleNode<?>);
135 switch (lflst1Count++) {
137 lflst1_1 = (SimpleNode<?>) node;
140 lflst1_2 = (SimpleNode<?>) node;
143 } else if (node.getNodeType().getLocalName().equals("lst1")) {
144 assertTrue(node instanceof CompositeNode);
145 lst1 = (CompositeNode) node;
150 assertNotNull(lflst1_1);
151 assertNotNull(lflst1_2);
154 assertEquals("", lf1.getValue());
155 assertEquals("", lflst1_1.getValue());
156 assertEquals("", lflst1_2.getValue());
157 assertEquals(1, lst1.getChildren().size());
158 assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName());
160 assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
161 assertEquals("", lst1.getChildren().get(0).getValue());
165 private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) {
166 SimpleNode<?> lf1suf = null;
167 SimpleNode<?> lflst1suf_1 = null;
168 SimpleNode<?> lflst1suf_2 = null;
169 SimpleNode<?> lflst1suf_3 = null;
170 CompositeNode cont1suf = null;
171 CompositeNode lst1suf = null;
175 for (Node<?> node : compNode.getChildren()) {
176 String localName = node.getNodeType().getLocalName();
177 if (localName.equals("lf1" + suf)) {
178 assertTrue(node instanceof SimpleNode<?>);
179 lf1suf = (SimpleNode<?>) node;
180 } else if (localName.equals("lflst1" + suf)) {
181 assertTrue(node instanceof SimpleNode<?>);
182 switch (lflstCount++) {
184 lflst1suf_1 = (SimpleNode<?>) node;
187 lflst1suf_2 = (SimpleNode<?>) node;
190 lflst1suf_3 = (SimpleNode<?>) node;
193 } else if (localName.equals("lst1" + suf)) {
194 assertTrue(node instanceof CompositeNode);
195 lst1suf = (CompositeNode) node;
196 } else if (localName.equals("cont1" + suf)) {
197 assertTrue(node instanceof CompositeNode);
198 cont1suf = (CompositeNode) node;
200 assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
203 assertNotNull(lf1suf);
204 assertNotNull(lflst1suf_1);
205 assertNotNull(lflst1suf_2);
206 assertNotNull(lflst1suf_3);
207 assertNotNull(lst1suf);
208 assertNotNull(cont1suf);
210 assertEquals("str0", lf1suf.getValue());
211 assertEquals("121", lflst1suf_1.getValue());
212 assertEquals("131", lflst1suf_2.getValue());
213 assertEquals("str1", lflst1suf_3.getValue());
215 assertEquals(1, lst1suf.getChildren().size());
217 assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
218 SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().get(0);
219 assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString());
220 assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName());
221 assertEquals("str2", lst11_lf11.getValue());
223 assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode<?>);
224 SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().get(0);
225 assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString());
226 assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName());
227 assertEquals("100", cont1_lf11.getValue());
230 private CompositeNode compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) {
231 XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
233 InputStream xmlStream = FromXmlToCompositeNode.class.getResourceAsStream(xmlPath);
234 CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
235 if (dummyNamespaces) {
237 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
238 return ((CompositeNodeWrapper) compositeNode).unwrap(null);
239 } catch (URISyntaxException e) {
240 LOG.error(e.getMessage());
241 assertTrue(e.getMessage(), false);
244 return compositeNode;
246 } catch (WebApplicationException | IOException e) {
247 LOG.error(e.getMessage());