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