1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
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;
13 import javax.ws.rs.WebApplicationException;
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;
25 import com.google.gson.JsonSyntaxException;
27 public class FromJsonToCompositeNodeTest {
29 private static final Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNodeTest.class);
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");
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");
44 * test if for every leaf list item is simple node instance created
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());
53 boolean lflst1_1 = false;
54 boolean lflst1_2 = false;
55 boolean lflst1_3 = false;
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")) {
63 } else if (simpleNode.getValue().equals("55")) {
65 } else if (simpleNode.getValue().equals("66")) {
77 * List contains 4 items and in every item are other elements. It is
78 * supposed that there should be: lf11, lflst11, cont11, lst11
81 public void multipleItemsInListTest() {
82 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json",
84 assertNotNull(compositeNode);
86 assertEquals("lst", compositeNode.getNodeType().getLocalName());
88 verityMultipleItemsInList(compositeNode);
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());
97 assertNotNull(compositeNode.getChildren());
98 assertEquals(1, compositeNode.getChildren().size());
99 Node<?> lfNode = compositeNode.getChildren().iterator().next();
101 assertTrue(lfNode instanceof SimpleNode<?>);
102 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
107 public void incorrectTopLevelElementsTest() {
108 Throwable cause1 = null;
110 compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json", true);
111 } catch (WebApplicationException e) {
115 assertNotNull(cause1);
120 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
122 Throwable cause2 = null;
124 compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json", true);
125 } catch (WebApplicationException e) {
128 assertNotNull(cause2);
129 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
131 Throwable cause3 = null;
133 compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json", true);
134 } catch (WebApplicationException e) {
137 assertNotNull(cause3);
142 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
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
151 public void emptyDataReadTest() {
152 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json", true);
154 assertNotNull(compositeNode);
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());
163 String reason = null;
165 compositeContainerFromJson("/json-to-composite-node/empty-data1.json", true);
166 } catch (JsonSyntaxException e) {
167 reason = e.getMessage();
170 assertTrue(reason.contains("Expected value at line"));
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
181 public void notSupplyNamespaceIfAlreadySupplied() {
183 CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
184 assertNotNull(compositeNode);
186 DataSchemaNode dataSchemaNode1 = null;
187 DataSchemaNode dataSchemaNode2 = null;
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());
197 assertNotNull(dataSchemaNode1);
198 assertNotNull(dataSchemaNode2);
200 // supplement namespaces according to first data schema -
201 // "simple:data:types1"
202 TestUtils.supplementNamespace(dataSchemaNode1, compositeNode);
204 assertTrue(compositeNode instanceof CompositeNodeWrapper);
205 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
207 assertEquals("lst", compNode.getNodeType().getLocalName());
208 verifyCompositeNode(compNode, "simple:list:yang1");
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");
217 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
219 CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
220 assertNotNull(compositeNode);
222 DataSchemaNode dataSchemaNode = null;
224 dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
225 } catch (FileNotFoundException e) {
226 LOG.error(e.getMessage());
229 assertNotNull(dataSchemaNode);
231 TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
233 assertTrue(compositeNode instanceof CompositeNodeWrapper);
234 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
236 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
237 verifyCompositeNode(compNode, namespace);
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);
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);
256 } else if (localName.equals("lflst11")) {
257 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
258 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
260 } else if (localName.equals("cont11")) {
261 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
263 } else if (localName.equals("lst11")) {
265 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
266 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
270 assertTrue(lf11Found);
271 assertTrue(cont11Found);
272 assertTrue(lst11Found);
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;
282 assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString());
284 for (Node<?> node : compositeNode.getChildren()) {
285 if (node.getNodeType().getLocalName().equals("cont1")) {
286 if (node instanceof CompositeNode) {
288 assertEquals(0, ((CompositeNode) node).getChildren().size());
290 } else if (node.getNodeType().getLocalName().equals("lst1")) {
291 if (node instanceof CompositeNode) {
293 assertEquals(0, ((CompositeNode) node).getChildren().size());
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;
304 } else if (node.getNodeType().getLocalName().equals("lf1")) {
305 if (node instanceof SimpleNode) {
306 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
311 assertEquals(namespace, node.getNodeType().getNamespace().toString());
313 assertTrue(cont1Found);
314 assertTrue(lst1Found);
315 assertTrue(lflst1_1Found);
316 assertTrue(lflst1_2Found);
317 assertTrue(lf1Found);
320 private CompositeNode compositeContainerFromJson(String jsonPath) {
321 return compositeContainerFromJson(jsonPath, false);
324 private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces)
325 throws WebApplicationException {
327 JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
328 InputStream jsonStream = FromJsonToCompositeNodeTest.class.getResourceAsStream(jsonPath);
330 CompositeNode compositeNode = jsonToCompositeNodeProvider
331 .readFrom(null, null, null, null, null, jsonStream);
332 assertTrue(compositeNode instanceof CompositeNodeWrapper);
333 if (dummyNamespaces) {
335 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
336 return ((CompositeNodeWrapper) compositeNode).unwrap();
337 } catch (URISyntaxException e) {
338 LOG.error(e.getMessage());
339 assertTrue(e.getMessage(), false);
342 return compositeNode;
343 } catch (IOException e) {
344 LOG.error(e.getMessage());
345 assertTrue(e.getMessage(), false);