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.*;
15 import org.opendaylight.yangtools.yang.data.api.*;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 public class FromXmlToCompositeNodeTest {
20 private static final Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNodeTest.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 verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
45 verifyCommonPartAOfXml(compNode, "", nameSpace);
48 private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
49 assertEquals("cont", compNode.getNodeType().getLocalName());
51 SimpleNode<?> lf2 = null;
52 SimpleNode<?> lf3 = null;
54 for (Node<?> child : compNode.getChildren()) {
57 if (child instanceof SimpleNode<?>) {
58 SimpleNode<?> childSimple = (SimpleNode<?>) child;
59 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
61 found = found | (1 << 0);
62 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
64 found = found | (1 << 1);
67 assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
70 assertEquals("", lf2.getValue());
71 assertEquals(null, lf3.getValue());
75 public void testXmlDataList() {
76 CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false);
77 assertNotNull(compNode);
79 DataSchemaNode dataSchemaNode = null;
81 dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-list-yang",
82 "data-container-yang");
83 } catch (FileNotFoundException e) {
84 LOG.error(e.getMessage());
86 assertNotNull(dataSchemaNode);
87 TestUtils.supplementNamespace(dataSchemaNode, compNode);
89 String nameSpaceList = "data:list:yang";
90 String nameSpaceCont = "data:container:yang";
91 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
92 assertEquals("cont", compNode.getNodeType().getLocalName());
93 assertEquals(3, compNode.getChildren().size());
94 CompositeNode lst1_1 = null;
95 CompositeNode lst1_2 = null;
97 for (Node<?> node : compNode.getChildren()) {
98 if (node.getNodeType().getLocalName().equals("lf1")) {
99 assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
100 assertTrue(node instanceof SimpleNode<?>);
101 assertEquals("lf1", node.getValue());
103 assertTrue(node instanceof CompositeNode);
104 switch (loopCount++) {
106 lst1_1 = (CompositeNode) node;
109 lst1_2 = (CompositeNode) node;
112 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
116 verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
120 SimpleNode<?> lflst11 = null;
121 CompositeNode cont11 = null;
122 for (Node<?> node : lst1_2.getChildren()) {
123 String nodeName = node.getNodeType().getLocalName();
124 if (nodeName.equals("lflst11")) {
125 assertTrue(node instanceof SimpleNode<?>);
126 lflst11 = (SimpleNode<?>) node;
128 } else if (nodeName.equals("cont11")) {
129 assertTrue(node instanceof CompositeNode);
130 cont11 = (CompositeNode) node;
132 assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
134 assertEquals("221", lflst11.getValue());
136 assertEquals(1, cont11.getChildren().size());
137 assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
138 SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
139 assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
140 assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
141 assertEquals("100", cont11_lf111.getValue());
147 public void testXmlEmptyData() {
148 CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/empty-data.xml", true);
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.getChildren()) {
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.getChildren().size());
185 assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName());
187 assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
188 assertEquals("", lst1.getChildren().get(0).getValue());
192 private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) {
193 SimpleNode<?> lf1suf = null;
194 SimpleNode<?> lflst1suf_1 = null;
195 SimpleNode<?> lflst1suf_2 = null;
196 SimpleNode<?> lflst1suf_3 = null;
197 CompositeNode cont1suf = null;
198 CompositeNode lst1suf = null;
202 for (Node<?> node : compNode.getChildren()) {
203 String localName = node.getNodeType().getLocalName();
204 if (localName.equals("lf1" + suf)) {
205 assertTrue(node instanceof SimpleNode<?>);
206 lf1suf = (SimpleNode<?>) node;
207 } else if (localName.equals("lflst1" + suf)) {
208 assertTrue(node instanceof SimpleNode<?>);
209 switch (lflstCount++) {
211 lflst1suf_1 = (SimpleNode<?>) node;
214 lflst1suf_2 = (SimpleNode<?>) node;
217 lflst1suf_3 = (SimpleNode<?>) node;
220 } else if (localName.equals("lst1" + suf)) {
221 assertTrue(node instanceof CompositeNode);
222 lst1suf = (CompositeNode) node;
223 } else if (localName.equals("cont1" + suf)) {
224 assertTrue(node instanceof CompositeNode);
225 cont1suf = (CompositeNode) node;
227 assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
230 assertNotNull(lf1suf);
231 assertNotNull(lflst1suf_1);
232 assertNotNull(lflst1suf_2);
233 assertNotNull(lflst1suf_3);
234 assertNotNull(lst1suf);
235 assertNotNull(cont1suf);
237 assertEquals("str0", lf1suf.getValue());
238 assertEquals("121", lflst1suf_1.getValue());
239 assertEquals("131", lflst1suf_2.getValue());
240 assertEquals("str1", lflst1suf_3.getValue());
242 assertEquals(1, lst1suf.getChildren().size());
244 assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
245 SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().get(0);
246 assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString());
247 assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName());
248 assertEquals("str2", lst11_lf11.getValue());
250 assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode<?>);
251 SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().get(0);
252 assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString());
253 assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName());
254 assertEquals("100", cont1_lf11.getValue());
257 private CompositeNode compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) {
258 XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
260 InputStream xmlStream = FromXmlToCompositeNodeTest.class.getResourceAsStream(xmlPath);
261 CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
262 if (dummyNamespaces) {
264 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
265 return ((CompositeNodeWrapper) compositeNode).unwrap(null);
266 } catch (URISyntaxException e) {
267 LOG.error(e.getMessage());
268 assertTrue(e.getMessage(), false);
271 return compositeNode;
273 } catch (WebApplicationException | IOException e) {
274 LOG.error(e.getMessage());