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