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