Merge "MD-SAL Statistics Manager -Added group-id to group-statistics API"
[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.CompositeNodeWrapper;
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         verifyCommonPartAOfXml(compNode, "", nameSpace);
45     }
46
47     @Test
48     public void testXmlDataList() {
49         CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false);
50         assertNotNull(compNode);
51
52         DataSchemaNode dataSchemaNode = null;
53         try {
54             dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-list-yang",
55                     "data-container-yang");
56         } catch (FileNotFoundException e) {
57             LOG.error(e.getMessage());
58         }
59         assertNotNull(dataSchemaNode);
60         TestUtils.supplementNamespace(dataSchemaNode, compNode);
61
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;
69         int loopCount = 0;
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());
75             } else {
76                 assertTrue(node instanceof CompositeNode);
77                 switch (loopCount++) {
78                 case 0:
79                     lst1_1 = (CompositeNode) node;
80                     break;
81                 case 1:
82                     lst1_2 = (CompositeNode) node;
83                     break;
84                 }
85                 assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
86             }
87         }
88         // lst1_1
89         verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
90         // :lst1_1
91
92         // lst1_2
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;
100
101             } else if (nodeName.equals("cont11")) {
102                 assertTrue(node instanceof CompositeNode);
103                 cont11 = (CompositeNode) node;
104             }
105             assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
106         }
107         assertEquals("221", lflst11.getValue());
108
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());
115         // :lst1_2
116
117     }
118
119     @Test
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;
127         int lflst1Count = 0;
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<?>);
134
135                 switch (lflst1Count++) {
136                 case 0:
137                     lflst1_1 = (SimpleNode<?>) node;
138                     break;
139                 case 1:
140                     lflst1_2 = (SimpleNode<?>) node;
141                     break;
142                 }
143             } else if (node.getNodeType().getLocalName().equals("lst1")) {
144                 assertTrue(node instanceof CompositeNode);
145                 lst1 = (CompositeNode) node;
146             }
147         }
148
149         assertNotNull(lf1);
150         assertNotNull(lflst1_1);
151         assertNotNull(lflst1_2);
152         assertNotNull(lst1);
153
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());
159
160         assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
161         assertEquals("", lst1.getChildren().get(0).getValue());
162
163     }
164
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;
172
173         int lflstCount = 0;
174
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++) {
183                 case 0:
184                     lflst1suf_1 = (SimpleNode<?>) node;
185                     break;
186                 case 1:
187                     lflst1suf_2 = (SimpleNode<?>) node;
188                     break;
189                 case 2:
190                     lflst1suf_3 = (SimpleNode<?>) node;
191                     break;
192                 }
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;
199             }
200             assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
201         }
202
203         assertNotNull(lf1suf);
204         assertNotNull(lflst1suf_1);
205         assertNotNull(lflst1suf_2);
206         assertNotNull(lflst1suf_3);
207         assertNotNull(lst1suf);
208         assertNotNull(cont1suf);
209
210         assertEquals("str0", lf1suf.getValue());
211         assertEquals("121", lflst1suf_1.getValue());
212         assertEquals("131", lflst1suf_2.getValue());
213         assertEquals("str1", lflst1suf_3.getValue());
214
215         assertEquals(1, lst1suf.getChildren().size());
216
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());
222
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());
228     }
229
230     private CompositeNode compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) {
231         XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
232         try {
233             InputStream xmlStream = FromXmlToCompositeNodeTest.class.getResourceAsStream(xmlPath);
234             CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
235             if (dummyNamespaces) {
236                 try {
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);
242                 }
243             }
244             return compositeNode;
245
246         } catch (WebApplicationException | IOException e) {
247             LOG.error(e.getMessage());
248             assertTrue(false);
249         }
250         return null;
251     }
252
253 }