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