Merge changes I50218f21,I6bc2631b
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / FromJsonToCompositeNodeTest.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 import static org.mockito.Matchers.any;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import java.io.*;
11 import java.net.*;
12 import java.util.*;
13 import java.util.concurrent.*;
14
15 import javax.ws.rs.WebApplicationException;
16
17 import org.junit.*;
18 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
19 import org.opendaylight.controller.sal.restconf.impl.*;
20 import org.opendaylight.yangtools.yang.data.api.*;
21 import org.slf4j.*;
22 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.model.api.*;
25
26 import com.google.gson.JsonSyntaxException;
27
28 public class FromJsonToCompositeNodeTest {
29
30     private static final Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNodeTest.class);
31
32     @Test
33     public void simpleListTest() {
34         simpleTest("/json-to-composite-node/simple-list.json", "/json-to-composite-node/simple-list-yang", "lst",
35                 "simple:list:yang1", "simple-list-yang1");
36     }
37
38     @Test
39     public void simpleContainerTest() {
40         simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-container-yang",
41                 "cont", "simple:container:yang", "simple-container-yang");
42     }
43
44     /**
45      * test if for every leaf list item is simple node instance created
46      */
47     @Test
48     public void multipleItemsInLeafList() {
49         CompositeNode compositeNode = compositeContainerFromJson(
50                 "/json-to-composite-node/multiple-leaflist-items.json", true);
51         assertNotNull(compositeNode);
52         assertEquals(3, compositeNode.getChildren().size());
53
54         boolean lflst1_1 = false;
55         boolean lflst1_2 = false;
56         boolean lflst1_3 = false;
57
58         for (Node<?> node : compositeNode.getChildren()) {
59             assertEquals("lflst1", node.getNodeType().getLocalName());
60             assertTrue(node instanceof SimpleNode<?>);
61             SimpleNode<?> simpleNode = (SimpleNode<?>) node;
62             if (simpleNode.getValue().equals("45")) {
63                 lflst1_1 = true;
64             } else if (simpleNode.getValue().equals("55")) {
65                 lflst1_2 = true;
66             } else if (simpleNode.getValue().equals("66")) {
67                 lflst1_3 = true;
68             }
69         }
70
71         assertTrue(lflst1_1);
72         assertTrue(lflst1_2);
73         assertTrue(lflst1_3);
74
75     }
76
77     /**
78      * List contains 4 items and in every item are other elements. It is
79      * supposed that there should be: lf11, lflst11, cont11, lst11
80      */
81     @Test
82     public void multipleItemsInListTest() {
83         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json",
84                 true);
85         assertNotNull(compositeNode);
86
87         assertEquals("lst", compositeNode.getNodeType().getLocalName());
88
89         verityMultipleItemsInList(compositeNode);
90     }
91
92     @Test
93     public void nullArrayToSimpleNodeWithNullValueTest() {
94         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/array-with-null.json", true);
95         assertNotNull(compositeNode);
96         assertEquals("cont", compositeNode.getNodeType().getLocalName());
97
98         assertNotNull(compositeNode.getChildren());
99         assertEquals(1, compositeNode.getChildren().size());
100         Node<?> lfNode = compositeNode.getChildren().iterator().next();
101
102         assertTrue(lfNode instanceof SimpleNode<?>);
103         assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
104
105     }
106
107     @Test
108     public void incorrectTopLevelElementsTest() {
109         Throwable cause1 = null;
110         try {
111             compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json", true);
112         } catch (WebApplicationException e) {
113             cause1 = e;
114         }
115
116         assertNotNull(cause1);
117         assertTrue(cause1
118                 .getCause()
119                 .getMessage()
120                 .contains(
121                         "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
122
123         Throwable cause2 = null;
124         try {
125             compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json", true);
126         } catch (WebApplicationException e) {
127             cause2 = e;
128         }
129         assertNotNull(cause2);
130         assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
131
132         Throwable cause3 = null;
133         try {
134             compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json", true);
135         } catch (WebApplicationException e) {
136             cause3 = e;
137         }
138         assertNotNull(cause3);
139         assertTrue(cause3
140                 .getCause()
141                 .getMessage()
142                 .contains(
143                         "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
144
145     }
146
147     /**
148      * if leaf list with no data is in json then no corresponding data is
149      * created in composite node. if leaf with no data then exception is raised
150      */
151     @Test
152     public void emptyDataReadTest() {
153         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json", true);
154
155         assertNotNull(compositeNode);
156
157         assertEquals("cont", compositeNode.getNodeType().getLocalName());
158         assertTrue(compositeNode instanceof CompositeNode);
159         List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
160         assertEquals(1, children.size());
161         assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
162         assertEquals("45", children.get(0).getValue());
163
164         String reason = null;
165         try {
166             compositeContainerFromJson("/json-to-composite-node/empty-data1.json", true);
167         } catch (JsonSyntaxException e) {
168             reason = e.getMessage();
169         }
170
171         assertTrue(reason.contains("Expected value at line"));
172
173     }
174
175     /**
176      * Tests whether namespace <b>stay unchanged</b> if concrete values are
177      * present in composite or simple node and if the method for update is
178      * called.
179      * 
180      */
181     @Test
182     public void notSupplyNamespaceIfAlreadySupplied() {
183
184         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
185         assertNotNull(compositeNode);
186
187         DataSchemaNode dataSchemaNode1 = null;
188         DataSchemaNode dataSchemaNode2 = null;
189         try {
190             dataSchemaNode1 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang",
191                     "simple-list-yang1");
192             dataSchemaNode2 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang",
193                     "simple-list-yang2");
194         } catch (FileNotFoundException e) {
195             LOG.error(e.getMessage());
196             assertTrue(false);
197         }
198         assertNotNull(dataSchemaNode1);
199         assertNotNull(dataSchemaNode2);
200
201         // supplement namespaces according to first data schema -
202         // "simple:data:types1"
203         TestUtils.supplementNamespace(dataSchemaNode1, compositeNode);
204
205         assertTrue(compositeNode instanceof CompositeNodeWrapper);
206         CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
207
208         assertEquals("lst", compNode.getNodeType().getLocalName());
209         verifyCompositeNode(compNode, "simple:list:yang1");
210
211         // dataSchemaNode2 should't be taken into account, because compNode
212         // isn't CompositeNodeWrapper
213         TestUtils.supplementNamespace(dataSchemaNode2, compNode);
214         verifyCompositeNode(compNode, "simple:list:yang1");
215
216     }
217
218     private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
219             String moduleName) {
220         CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
221         assertNotNull(compositeNode);
222
223         DataSchemaNode dataSchemaNode = null;
224         try {
225             dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
226         } catch (FileNotFoundException e) {
227             LOG.error(e.getMessage());
228             assertTrue(false);
229         }
230         assertNotNull(dataSchemaNode);
231
232         TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
233
234         assertTrue(compositeNode instanceof CompositeNodeWrapper);
235         CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
236
237         assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
238         verifyCompositeNode(compNode, namespace);
239     }
240
241     private void verityMultipleItemsInList(CompositeNode compositeNode) {
242         List<Node<?>> childrenNodes = compositeNode.getChildren();
243         assertEquals(4, childrenNodes.size());
244         boolean lf11Found = false;
245         boolean cont11Found = false;
246         boolean lst11Found = false;
247         for (Node<?> lst1Item : childrenNodes) {
248             assertEquals("lst1", lst1Item.getNodeType().getLocalName());
249             assertTrue(lst1Item instanceof CompositeNode);
250
251             List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
252             assertEquals(1, childrenLst1.size());
253             String localName = childrenLst1.get(0).getNodeType().getLocalName();
254             if (localName.equals("lf11")) {
255                 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
256                 lf11Found = true;
257             } else if (localName.equals("lflst11")) {
258                 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
259                 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
260                 lf11Found = true;
261             } else if (localName.equals("cont11")) {
262                 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
263                 cont11Found = true;
264             } else if (localName.equals("lst11")) {
265                 lst11Found = true;
266                 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
267                 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
268             }
269
270         }
271         assertTrue(lf11Found);
272         assertTrue(cont11Found);
273         assertTrue(lst11Found);
274     }
275
276     private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
277         boolean cont1Found = false;
278         boolean lst1Found = false;
279         boolean lflst1_1Found = false;
280         boolean lflst1_2Found = false;
281         boolean lf1Found = false;
282
283         assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
284
285         for (Node<?> node : compositeNode.getChildren()) {
286             if (node.getNodeType().getLocalName().equals("cont1")) {
287                 if (node instanceof CompositeNode) {
288                     cont1Found = true;
289                     assertEquals(0, ((CompositeNode) node).getChildren().size());
290                 }
291             } else if (node.getNodeType().getLocalName().equals("lst1")) {
292                 if (node instanceof CompositeNode) {
293                     lst1Found = true;
294                     assertEquals(0, ((CompositeNode) node).getChildren().size());
295                 }
296             } else if (node.getNodeType().getLocalName().equals("lflst1")) {
297                 if (node instanceof SimpleNode) {
298                     if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
299                         lflst1_1Found = true;
300                     } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
301                         lflst1_2Found = true;
302                     }
303                 }
304
305             } else if (node.getNodeType().getLocalName().equals("lf1")) {
306                 if (node instanceof SimpleNode) {
307                     if (((SimpleNode<?>) node).getValue().equals("lf1")) {
308                         lf1Found = true;
309                     }
310                 }
311             }
312             assertEquals(namespace, node.getNodeType().getNamespace().toString());
313         }
314         assertTrue(cont1Found);
315         assertTrue(lst1Found);
316         assertTrue(lflst1_1Found);
317         assertTrue(lflst1_2Found);
318         assertTrue(lf1Found);
319     }
320
321     private CompositeNode compositeContainerFromJson(String jsonPath) {
322         return compositeContainerFromJson(jsonPath, false);
323     }
324
325     private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
326             throws WebApplicationException {
327
328         JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
329         InputStream jsonStream = FromJsonToCompositeNodeTest.class.getResourceAsStream(jsonPath);
330         try {
331             CompositeNode compositeNode = jsonToCompositeNodeProvider
332                     .readFrom(null, null, null, null, null, jsonStream);
333             assertTrue(compositeNode instanceof CompositeNodeWrapper);
334             if (dummyNamespaces) {
335                 try {
336                     TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
337                     return ((CompositeNodeWrapper) compositeNode).unwrap(null);
338                 } catch (URISyntaxException e) {
339                     LOG.error(e.getMessage());
340                     assertTrue(e.getMessage(), false);
341                 }
342             }
343             return compositeNode;
344         } catch (IOException e) {
345             LOG.error(e.getMessage());
346             assertTrue(e.getMessage(), false);
347         }
348         return null;
349     }
350
351 }