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