df2c03f9d76f1306ddf88d352437531d8ff774ec
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / FromJsonToCompositeNode.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.*;
8 import java.net.*;
9 import java.util.List;
10
11 import javax.ws.rs.WebApplicationException;
12
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
15 import org.opendaylight.controller.sal.restconf.impl.*;
16 import org.opendaylight.yangtools.yang.data.api.*;
17 import org.slf4j.*;
18
19 import com.google.gson.JsonSyntaxException;
20
21 public class FromJsonToCompositeNode {
22     Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
23
24     @Test
25     public void simpleListTest() {
26         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
27         assertNotNull(compositeNode);
28
29         assertEquals("lst", compositeNode.getNodeType().getLocalName());
30         verifyCompositeNode(compositeNode);
31     }
32
33     @Test
34     public void simpleContainerTest() {
35         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-container.json");
36         assertNotNull(compositeNode);
37
38         assertEquals("cont", compositeNode.getNodeType().getLocalName());
39
40         verifyCompositeNode(compositeNode);
41     }
42
43     /**
44      * List contains 4 items and in every item are other elements. It is
45      * supposed that there should be: lf11, lflst11, cont11, lst11
46      */
47     @Test
48     public void multipleItemsInListTest() {
49         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json");
50         assertNotNull(compositeNode);
51
52         assertEquals("lst", compositeNode.getNodeType().getLocalName());
53
54         verityMultipleItemsInList(compositeNode);
55     }
56
57     @Test
58     public void incorrectTopLevelElementsTest() {
59         Throwable cause1 = null;
60         try {
61             compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json");
62         } catch (WebApplicationException e) {
63             cause1 = e;
64         }
65
66         assertNotNull(cause1);
67         assertTrue(cause1
68                 .getCause()
69                 .getMessage()
70                 .contains(
71                         "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
72
73         Throwable cause2 = null;
74         try {
75             compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json");
76         } catch (WebApplicationException e) {
77             cause2 = e;
78         }
79         assertNotNull(cause2);
80         assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
81
82         Throwable cause3 = null;
83         try {
84             compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json");
85         } catch (WebApplicationException e) {
86             cause3 = e;
87         }
88         assertNotNull(cause3);
89         assertTrue(cause3
90                 .getCause()
91                 .getMessage()
92                 .contains(
93                         "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
94
95     }
96
97     /**
98      * if leaf list with no data is in json then no corresponding data is
99      * created in composite node. if leaf with no data then exception is raised
100      */
101     @Test
102     public void emptyDataReadTest() {
103         CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json");
104
105         assertNotNull(compositeNode);
106
107         assertEquals("cont", compositeNode.getNodeType().getLocalName());
108         assertTrue(compositeNode instanceof CompositeNode);
109         List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
110         assertEquals(1, children.size());
111         assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
112         assertEquals("45", children.get(0).getValue());
113
114         String reason = null;
115         try {
116             compositeContainerFromJson("/json-to-composite-node/empty-data1.json");
117         } catch (JsonSyntaxException e) {
118             reason = e.getMessage();
119         }
120
121         assertTrue(reason.contains("Expected value at line"));
122
123     }
124
125     private void verityMultipleItemsInList(CompositeNode compositeNode) {
126         List<Node<?>> childrenNodes = compositeNode.getChildren();
127         assertEquals(4, childrenNodes.size());
128         boolean lf11Found = false;
129         boolean cont11Found = false;
130         boolean lst11Found = false;
131         for (Node<?> lst1Item : childrenNodes) {
132             assertEquals("lst1", lst1Item.getNodeType().getLocalName());
133             assertTrue(lst1Item instanceof CompositeNode);
134
135             List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
136             assertEquals(1, childrenLst1.size());
137             String localName = childrenLst1.get(0).getNodeType().getLocalName();
138             if (localName.equals("lf11")) {
139                 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
140                 lf11Found = true;
141             } else if (localName.equals("lflst11")) {
142                 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
143                 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
144                 lf11Found = true;
145             } else if (localName.equals("cont11")) {
146                 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
147                 cont11Found = true;
148             } else if (localName.equals("lst11")) {
149                 lst11Found = true;
150                 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
151                 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
152             }
153
154         }
155
156         assertTrue(lf11Found);
157         assertTrue(cont11Found);
158         assertTrue(lst11Found);
159
160     }
161
162     private void verifyCompositeNode(CompositeNode compositeNode) {
163         boolean cont1Found = false;
164         boolean lst1Found = false;
165         boolean lflst1_1Found = false;
166         boolean lflst1_2Found = false;
167         boolean lf1Found = false;
168
169         for (Node<?> node : compositeNode.getChildren()) {
170             if (node.getNodeType().getLocalName().equals("cont1")) {
171                 if (node instanceof CompositeNode) {
172                     cont1Found = true;
173                     assertEquals(0, ((CompositeNode) node).getChildren().size());
174                 }
175             } else if (node.getNodeType().getLocalName().equals("lst1")) {
176                 if (node instanceof CompositeNode) {
177                     lst1Found = true;
178                     assertEquals(0, ((CompositeNode) node).getChildren().size());
179                 }
180             } else if (node.getNodeType().getLocalName().equals("lflst1")) {
181                 if (node instanceof SimpleNode) {
182                     if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
183                         lflst1_1Found = true;
184                     } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
185                         lflst1_2Found = true;
186                     }
187                 }
188
189             } else if (node.getNodeType().getLocalName().equals("lf1")) {
190                 if (node instanceof SimpleNode) {
191                     if (((SimpleNode<?>) node).getValue().equals("lf1")) {
192                         lf1Found = true;
193                     }
194                 }
195             }
196         }
197         assertTrue(cont1Found);
198         assertTrue(lst1Found);
199         assertTrue(lflst1_1Found);
200         assertTrue(lflst1_2Found);
201         assertTrue(lf1Found);
202     }
203
204     private CompositeNode compositeContainerFromJson(String jsonPath) throws WebApplicationException {
205
206         JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
207         InputStream jsonStream = FromJsonToCompositeNode.class.getResourceAsStream(jsonPath);
208         try {
209             CompositeNode compositeNode = jsonToCompositeNodeProvider
210                     .readFrom(null, null, null, null, null, jsonStream);
211             assertTrue(compositeNode instanceof CompositeNodeWrapper);
212             try {
213                 addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
214                 return ((CompositeNodeWrapper) compositeNode).unwrap(null);
215             } catch (URISyntaxException e) {
216                 LOG.error(e.getMessage());
217                 assertTrue(e.getMessage(), false);
218             }
219         } catch (IOException e) {
220             LOG.error(e.getMessage());
221             assertTrue(e.getMessage(), false);
222         }
223         return null;
224     }
225
226     private void addDummyNamespaceToAllNodes(NodeWrapper<?> wrappedNode) throws URISyntaxException {
227         wrappedNode.setNamespace(new URI(""));
228         if (wrappedNode instanceof CompositeNodeWrapper) {
229             for (NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
230                 addDummyNamespaceToAllNodes(childNodeWrapper);
231             }
232         }
233     }
234 }