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