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