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