Simplify method isMutualExclusive in Subnet.
[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.*;
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.CompositeNodeWrapper;
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         TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, "identityref-module:cont");
224
225         assertEquals("cont", compositeNode.getNodeType().getLocalName());
226
227         List<Node<?>> childs = compositeNode.getChildren();
228         assertEquals(1, childs.size());
229         Node<?> nd = childs.iterator().next();
230         assertTrue(nd instanceof CompositeNode);
231         assertEquals("cont1", nd.getNodeType().getLocalName());
232
233         childs = ((CompositeNode) nd).getChildren();
234         assertEquals(4, childs.size());
235         SimpleNode<?> lf11 = null;
236         SimpleNode<?> lf12 = null;
237         SimpleNode<?> lf13 = null;
238         SimpleNode<?> lf14 = null;
239         for (Node<?> child : childs) {
240             assertTrue(child instanceof SimpleNode);
241             if (child.getNodeType().getLocalName().equals("lf11")) {
242                 lf11 = (SimpleNode<?>) child;
243             } else if (child.getNodeType().getLocalName().equals("lf12")) {
244                 lf12 = (SimpleNode<?>) child;
245             } else if (child.getNodeType().getLocalName().equals("lf13")) {
246                 lf13 = (SimpleNode<?>) child;
247             } else if (child.getNodeType().getLocalName().equals("lf14")) {
248                 lf14 = (SimpleNode<?>) child;
249             }
250         }
251
252         assertTrue(lf11.getValue() instanceof QName);
253         assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
254         assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
255
256         assertTrue(lf12.getValue() instanceof QName);
257         assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
258         assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
259
260         assertTrue(lf13.getValue() instanceof QName);
261         assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
262         assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
263
264         assertTrue(lf14.getValue() instanceof QName);
265         assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
266         assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
267     }
268
269     private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
270             String moduleName) {
271         CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
272         assertNotNull(compositeNode);
273
274         DataSchemaNode dataSchemaNode = null;
275         try {
276             dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
277         } catch (FileNotFoundException e) {
278             LOG.error(e.getMessage());
279             assertTrue(false);
280         }
281         assertNotNull(dataSchemaNode);
282
283         TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
284
285         assertTrue(compositeNode instanceof CompositeNodeWrapper);
286         CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
287
288         assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
289         verifyCompositeNode(compNode, namespace);
290     }
291
292     private void verityMultipleItemsInList(CompositeNode compositeNode) {
293         List<Node<?>> childrenNodes = compositeNode.getChildren();
294         assertEquals(4, childrenNodes.size());
295         boolean lf11Found = false;
296         boolean cont11Found = false;
297         boolean lst11Found = false;
298         for (Node<?> lst1Item : childrenNodes) {
299             assertEquals("lst1", lst1Item.getNodeType().getLocalName());
300             assertTrue(lst1Item instanceof CompositeNode);
301
302             List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
303             assertEquals(1, childrenLst1.size());
304             String localName = childrenLst1.get(0).getNodeType().getLocalName();
305             if (localName.equals("lf11")) {
306                 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
307                 lf11Found = true;
308             } else if (localName.equals("lflst11")) {
309                 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
310                 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
311                 lf11Found = true;
312             } else if (localName.equals("cont11")) {
313                 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
314                 cont11Found = true;
315             } else if (localName.equals("lst11")) {
316                 lst11Found = true;
317                 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
318                 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
319             }
320
321         }
322         assertTrue(lf11Found);
323         assertTrue(cont11Found);
324         assertTrue(lst11Found);
325     }
326
327     private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
328         boolean cont1Found = false;
329         boolean lst1Found = false;
330         boolean lflst1_1Found = false;
331         boolean lflst1_2Found = false;
332         boolean lf1Found = false;
333
334         assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
335
336         for (Node<?> node : compositeNode.getChildren()) {
337             if (node.getNodeType().getLocalName().equals("cont1")) {
338                 if (node instanceof CompositeNode) {
339                     cont1Found = true;
340                     assertEquals(0, ((CompositeNode) node).getChildren().size());
341                 }
342             } else if (node.getNodeType().getLocalName().equals("lst1")) {
343                 if (node instanceof CompositeNode) {
344                     lst1Found = true;
345                     assertEquals(0, ((CompositeNode) node).getChildren().size());
346                 }
347             } else if (node.getNodeType().getLocalName().equals("lflst1")) {
348                 if (node instanceof SimpleNode) {
349                     if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
350                         lflst1_1Found = true;
351                     } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
352                         lflst1_2Found = true;
353                     }
354                 }
355
356             } else if (node.getNodeType().getLocalName().equals("lf1")) {
357                 if (node instanceof SimpleNode) {
358                     if (((SimpleNode<?>) node).getValue().equals("lf1")) {
359                         lf1Found = true;
360                     }
361                 }
362             }
363             assertEquals(namespace, node.getNodeType().getNamespace().toString());
364         }
365         assertTrue(cont1Found);
366         assertTrue(lst1Found);
367         assertTrue(lflst1_1Found);
368         assertTrue(lflst1_2Found);
369         assertTrue(lf1Found);
370     }
371
372     private CompositeNode compositeContainerFromJson(String jsonPath) {
373         return compositeContainerFromJson(jsonPath, false);
374     }
375
376     private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
377             throws WebApplicationException {
378
379         JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
380         InputStream jsonStream = JsonToCnSnTest.class.getResourceAsStream(jsonPath);
381         try {
382             CompositeNode compositeNode = jsonToCompositeNodeProvider
383                     .readFrom(null, null, null, null, null, jsonStream);
384             assertTrue(compositeNode instanceof CompositeNodeWrapper);
385             if (dummyNamespaces) {
386                 try {
387                     TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
388                     return ((CompositeNodeWrapper) compositeNode).unwrap();
389                 } catch (URISyntaxException e) {
390                     LOG.error(e.getMessage());
391                     assertTrue(e.getMessage(), false);
392                 }
393             }
394             return compositeNode;
395         } catch (IOException e) {
396             LOG.error(e.getMessage());
397             assertTrue(e.getMessage(), false);
398         }
399         return null;
400     }
401
402 }