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