More test for improving of code coverage + test refactoring
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / xml / to / cnsn / test / XmlLeafrefToCnSnTest.java
1 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
2
3 import static org.junit.Assert.*;
4
5 import java.io.*;
6 import java.net.URISyntaxException;
7 import java.util.List;
8 import java.util.Set;
9
10 import javax.ws.rs.WebApplicationException;
11
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;
22
23 public class XmlLeafrefToCnSnTest {
24     private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class);
25
26     /**
27      * top level element represents container. second level element is list with
28      * two elements.
29      */
30     @Test
31     public void testXmlDataContainer() {
32         CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-container.xml", false);
33         assertNotNull(compNode);
34         Set<Module> modules = null;
35         try {
36             modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-container-yang").getPath());
37         } catch (FileNotFoundException e) {
38             LOG.error(e.getMessage());
39             assertTrue(false);
40         }
41
42         assertNotNull(modules);
43         TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
44
45         String nameSpace = "data:container:yang";
46         assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
47
48         verifyNullAndEmptyStringSingleNode(compNode, nameSpace);
49         verifyCommonPartAOfXml(compNode, "", nameSpace);
50     }
51
52     private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
53         assertEquals("cont", compNode.getNodeType().getLocalName());
54
55         SimpleNode<?> lf2 = null;
56         SimpleNode<?> lf3 = null;
57         int found = 0;
58         for (Node<?> child : compNode.getChildren()) {
59             if (found == 0x3)
60                 break;
61             if (child instanceof SimpleNode<?>) {
62                 SimpleNode<?> childSimple = (SimpleNode<?>) child;
63                 if (childSimple.getNodeType().getLocalName().equals("lf3")) {
64                     lf3 = childSimple;
65                     found = found | (1 << 0);
66                 } else if (childSimple.getNodeType().getLocalName().equals("lf2")) {
67                     lf2 = childSimple;
68                     found = found | (1 << 1);
69                 }
70             }
71             assertEquals(nameSpace, child.getNodeType().getNamespace().toString());
72         }
73
74         assertEquals("", lf2.getValue());
75         assertEquals(null, lf3.getValue());
76     }
77
78     @Test
79     public void testXmlDataList() {
80         CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-list.xml", false);
81         assertNotNull(compNode);
82
83         Set<Module> modules = null;
84         try {
85             modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-list-yang").getPath());
86         } catch (FileNotFoundException e) {
87         }
88         assertNotNull(modules);
89
90         TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
91
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;
99         int loopCount = 0;
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());
105             } else {
106                 assertTrue(node instanceof CompositeNode);
107                 switch (loopCount++) {
108                 case 0:
109                     lst1_1 = (CompositeNode) node;
110                     break;
111                 case 1:
112                     lst1_2 = (CompositeNode) node;
113                     break;
114                 }
115                 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
116             }
117         }
118         // lst1_1
119         verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
120         // :lst1_1
121
122         // lst1_2
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;
130
131             } else if (nodeName.equals("cont11")) {
132                 assertTrue(node instanceof CompositeNode);
133                 cont11 = (CompositeNode) node;
134             }
135             assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
136         }
137         assertEquals("221", lflst11.getValue());
138
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());
145         // :lst1_2
146
147     }
148
149     @Test
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;
157         int lflst1Count = 0;
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<?>);
164
165                 switch (lflst1Count++) {
166                 case 0:
167                     lflst1_1 = (SimpleNode<?>) node;
168                     break;
169                 case 1:
170                     lflst1_2 = (SimpleNode<?>) node;
171                     break;
172                 }
173             } else if (node.getNodeType().getLocalName().equals("lst1")) {
174                 assertTrue(node instanceof CompositeNode);
175                 lst1 = (CompositeNode) node;
176             }
177         }
178
179         assertNotNull(lf1);
180         assertNotNull(lflst1_1);
181         assertNotNull(lflst1_2);
182         assertNotNull(lst1);
183
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());
189
190         assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
191         assertEquals("", lst1.getChildren().get(0).getValue());
192
193     }
194
195     /**
196      * Test case like this <lf11 xmlns:x="namespace">x:identity</lf11>
197      */
198     @Test
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");
202     }
203
204     /**
205      * 
206      * Test case like <lf11 xmlns="namespace1"
207      * xmlns:x="namespace">identity</lf11>
208      */
209
210     @Test
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");
214     }
215
216     /**
217      * 
218      * Test case like <cont1 xmlns="namespace1"> <lf11
219      * xmlns:x="namespace">identity</lf11> </cont1>
220      */
221     @Test
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");
225     }
226
227     /**
228      * 
229      * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace">
230      * <lf11>x:identity</lf11> </cont1>
231      */
232     @Test
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");
236
237     }
238
239     /**
240      * 
241      * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11>
242      * </cont1>
243      */
244     @Test
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");
248     }
249
250     /**
251      * 
252      * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11>
253      * </cont1>
254      */
255     @Test
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");
259     }
260
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;
268
269         int lflstCount = 0;
270
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++) {
279                 case 0:
280                     lflst1suf_1 = (SimpleNode<?>) node;
281                     break;
282                 case 1:
283                     lflst1suf_2 = (SimpleNode<?>) node;
284                     break;
285                 case 2:
286                     lflst1suf_3 = (SimpleNode<?>) node;
287                     break;
288                 }
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;
295             }
296             assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
297         }
298
299         assertNotNull(lf1suf);
300         assertNotNull(lflst1suf_1);
301         assertNotNull(lflst1suf_2);
302         assertNotNull(lflst1suf_3);
303         assertNotNull(lst1suf);
304         assertNotNull(cont1suf);
305
306         assertEquals("str0", lf1suf.getValue());
307         assertEquals("121", lflst1suf_1.getValue());
308         assertEquals("131", lflst1suf_2.getValue());
309         assertEquals("str1", lflst1suf_3.getValue());
310
311         assertEquals(1, lst1suf.getChildren().size());
312
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());
318
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());
324     }
325
326     private CompositeNode compositeNodeFromXml(String xmlPath, boolean dummyNamespaces) {
327         XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
328         try {
329             InputStream xmlStream = XmlLeafrefToCnSnTest.class.getResourceAsStream(xmlPath);
330             CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
331             if (dummyNamespaces) {
332                 try {
333                     TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
334                     return ((CompositeNodeWrapper) compositeNode).unwrap();
335                 } catch (URISyntaxException e) {
336                     LOG.error(e.getMessage());
337                     assertTrue(e.getMessage(), false);
338                 }
339             }
340             return compositeNode;
341
342         } catch (WebApplicationException | IOException e) {
343             LOG.error(e.getMessage());
344             assertTrue(false);
345         }
346         return null;
347     }
348
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);
353
354         Set<Module> modules = TestUtils.resolveModulesFrom(yangPath);
355         assertEquals(moduleCount, modules.size());
356         Module module = TestUtils.resolveModule(moduleName, modules);
357         assertNotNull(module);
358         DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(null, module);
359         assertNotNull(dataSchemaNode);
360
361         TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
362
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());
368
369     }
370
371     private SimpleNode<?> getLf11(CompositeNode compositeNode) {
372         assertEquals("cont", compositeNode.getNodeType().getLocalName());
373
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());
379
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;
386             }
387         }
388         assertNotNull(lf11);
389         return lf11;
390     }
391
392 }