Null data from XML
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / FromXmlToCompositeNodeTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.*;
8 import java.net.URISyntaxException;
9
10 import javax.ws.rs.WebApplicationException;
11
12 import org.junit.*;
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;
17 import org.slf4j.*;
18
19 public class FromXmlToCompositeNodeTest {
20     private static final Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNodeTest.class);
21
22     /**
23      * top level element represents container. second level element is list with
24      * two elements.
25      */
26     @Test
27     public void testXmlDataContainer() {
28         CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-container.xml", false);
29         assertNotNull(compNode);
30         DataSchemaNode dataSchemaNode = null;
31         try {
32             dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-container-yang");
33         } catch (FileNotFoundException e) {
34             LOG.error(e.getMessage());
35             assertTrue(false);
36         }
37
38         assertNotNull(dataSchemaNode);
39         TestUtils.supplementNamespace(dataSchemaNode, compNode);
40
41         String nameSpace = "data:container:yang";
42         assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
43
44         verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
45         verifyCommonPartAOfXml(compNode, "", nameSpace);
46     }
47
48     private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
49         assertEquals("cont", compNode.getNodeType().getLocalName());
50
51         SimpleNode<?> lf2 = null;
52         SimpleNode<?> lf3 = null;
53         int found = 0;
54         for (Node<?> child : compNode.getChildren()) {
55             if (found == 0x3)
56                 break;
57             if (child instanceof SimpleNode<?>) {
58                 SimpleNode<?> childSimple = (SimpleNode<?>) child;
59                 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
60                     lf3 = childSimple;
61                     found = found | (1 << 0);
62                 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
63                     lf2 = childSimple;
64                     found = found | (1 << 1);
65                 }
66             }
67             assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
68         }
69
70         assertEquals("", lf2.getValue());
71         assertEquals(null, lf3.getValue());
72     }
73
74     @Test
75     public void testXmlDataList() {
76         CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false);
77         assertNotNull(compNode);
78
79         DataSchemaNode dataSchemaNode = null;
80         try {
81             dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-list-yang",
82                     "data-container-yang");
83         } catch (FileNotFoundException e) {
84             LOG.error(e.getMessage());
85         }
86         assertNotNull(dataSchemaNode);
87         TestUtils.supplementNamespace(dataSchemaNode, compNode);
88
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;
96         int loopCount = 0;
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());
102             } else {
103                 assertTrue(node instanceof CompositeNode);
104                 switch (loopCount++) {
105                 case 0:
106                     lst1_1 = (CompositeNode) node;
107                     break;
108                 case 1:
109                     lst1_2 = (CompositeNode) node;
110                     break;
111                 }
112                 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
113             }
114         }
115         // lst1_1
116         verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
117         // :lst1_1
118
119         // lst1_2
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;
127
128             } else if (nodeName.equals("cont11")) {
129                 assertTrue(node instanceof CompositeNode);
130                 cont11 = (CompositeNode) node;
131             }
132             assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
133         }
134         assertEquals("221", lflst11.getValue());
135
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());
142         // :lst1_2
143
144     }
145
146     @Test
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;
154         int lflst1Count = 0;
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<?>);
161
162                 switch (lflst1Count++) {
163                 case 0:
164                     lflst1_1 = (SimpleNode<?>) node;
165                     break;
166                 case 1:
167                     lflst1_2 = (SimpleNode<?>) node;
168                     break;
169                 }
170             } else if (node.getNodeType().getLocalName().equals("lst1")) {
171                 assertTrue(node instanceof CompositeNode);
172                 lst1 = (CompositeNode) node;
173             }
174         }
175
176         assertNotNull(lf1);
177         assertNotNull(lflst1_1);
178         assertNotNull(lflst1_2);
179         assertNotNull(lst1);
180
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());
186
187         assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
188         assertEquals("", lst1.getChildren().get(0).getValue());
189
190     }
191
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;
199
200         int lflstCount = 0;
201
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++) {
210                 case 0:
211                     lflst1suf_1 = (SimpleNode<?>) node;
212                     break;
213                 case 1:
214                     lflst1suf_2 = (SimpleNode<?>) node;
215                     break;
216                 case 2:
217                     lflst1suf_3 = (SimpleNode<?>) node;
218                     break;
219                 }
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;
226             }
227             assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
228         }
229
230         assertNotNull(lf1suf);
231         assertNotNull(lflst1suf_1);
232         assertNotNull(lflst1suf_2);
233         assertNotNull(lflst1suf_3);
234         assertNotNull(lst1suf);
235         assertNotNull(cont1suf);
236
237         assertEquals("str0", lf1suf.getValue());
238         assertEquals("121", lflst1suf_1.getValue());
239         assertEquals("131", lflst1suf_2.getValue());
240         assertEquals("str1", lflst1suf_3.getValue());
241
242         assertEquals(1, lst1suf.getChildren().size());
243
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());
249
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());
255     }
256
257     private CompositeNode compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) {
258         XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
259         try {
260             InputStream xmlStream = FromXmlToCompositeNodeTest.class.getResourceAsStream(xmlPath);
261             CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
262             if (dummyNamespaces) {
263                 try {
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);
269                 }
270             }
271             return compositeNode;
272
273         } catch (WebApplicationException | IOException e) {
274             LOG.error(e.getMessage());
275             assertTrue(false);
276         }
277         return null;
278     }
279
280 }