2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.HashSet;
16 import java.util.List;
19 import javax.ws.rs.WebApplicationException;
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
24 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
25 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
26 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29 import org.opendaylight.yangtools.yang.data.api.Node;
30 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.JsonSyntaxException;
37 public class JsonToCnSnTest {
39 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
42 public void simpleListTest() {
43 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1",
48 public void simpleContainerTest() {
49 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
50 "simple:container:yang", "simple-container-yang");
54 * test if for every leaf list item is simple node instance created
57 public void multipleItemsInLeafList() {
58 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true,
59 JsonToCompositeNodeProvider.INSTANCE);
60 assertNotNull(compositeNode);
61 assertEquals(3, compositeNode.getChildren().size());
63 boolean lflst1_1 = false;
64 boolean lflst1_2 = false;
65 boolean lflst1_3 = false;
67 for (Node<?> node : compositeNode.getChildren()) {
68 assertEquals("lflst1", node.getNodeType().getLocalName());
69 assertTrue(node instanceof SimpleNode<?>);
70 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
71 if (simpleNode.getValue().equals("45")) {
73 } else if (simpleNode.getValue().equals("55")) {
75 } else if (simpleNode.getValue().equals("66")) {
87 * List contains 4 items and in every item are other elements. It is
88 * supposed that there should be: lf11, lflst11, cont11, lst11
91 public void multipleItemsInListTest() {
92 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true,
93 JsonToCompositeNodeProvider.INSTANCE);
95 assertNotNull(compositeNode);
96 assertEquals("lst", compositeNode.getNodeType().getLocalName());
98 verityMultipleItemsInList(compositeNode);
102 public void nullArrayToSimpleNodeWithNullValueTest() {
103 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true,
104 JsonToCompositeNodeProvider.INSTANCE);
105 assertNotNull(compositeNode);
106 assertEquals("cont", compositeNode.getNodeType().getLocalName());
108 assertNotNull(compositeNode.getChildren());
109 assertEquals(1, compositeNode.getChildren().size());
110 Node<?> lfNode = compositeNode.getChildren().iterator().next();
112 assertTrue(lfNode instanceof SimpleNode<?>);
113 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
118 public void incorrectTopLevelElementsTest() {
119 Throwable cause1 = null;
122 .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE);
123 } catch (WebApplicationException e) {
127 assertNotNull(cause1);
132 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
134 Throwable cause2 = null;
137 .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE);
138 } catch (WebApplicationException e) {
141 assertNotNull(cause2);
142 assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
144 Throwable cause3 = null;
147 .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE);
148 } catch (WebApplicationException e) {
151 assertNotNull(cause3);
156 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
161 * if leaf list with no data is in json then no corresponding data is
162 * created in composite node. if leaf with no data then exception is raised
165 public void emptyDataReadTest() {
166 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/empty-data.json", true,
167 JsonToCompositeNodeProvider.INSTANCE);
169 assertNotNull(compositeNode);
171 assertEquals("cont", compositeNode.getNodeType().getLocalName());
172 assertTrue(compositeNode instanceof CompositeNode);
173 List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
174 assertEquals(1, children.size());
175 assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
176 assertEquals("45", children.get(0).getValue());
178 String reason = null;
180 TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE);
181 } catch (JsonSyntaxException e) {
182 reason = e.getMessage();
185 assertTrue(reason.contains("Expected value at line"));
190 * Tests whether namespace <b>stay unchanged</b> if concrete values are
191 * present in composite or simple node and if the method for update is
196 public void notSupplyNamespaceIfAlreadySupplied() {
198 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false,
199 JsonToCompositeNodeProvider.INSTANCE);
200 assertNotNull(compositeNode);
202 // supplement namespaces according to first data schema -
203 // "simple:data:types1"
204 Set<Module> modules1 = new HashSet<>();
205 Set<Module> modules2 = new HashSet<>();
206 modules1 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/1");
207 modules2 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/2");
208 assertNotNull(modules1);
209 assertNotNull(modules2);
211 TestUtils.normalizeCompositeNode(compositeNode, modules1, "simple-list-yang1:lst");
213 assertTrue(compositeNode instanceof CompositeNodeWrapper);
214 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
216 assertEquals("lst", compNode.getNodeType().getLocalName());
217 verifyCompositeNode(compNode, "simple:list:yang1");
219 TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst");
220 verifyCompositeNode(compNode, "simple:list:yang1");
224 public void jsonIdentityrefToCompositeNode() {
225 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false,
226 JsonToCompositeNodeProvider.INSTANCE);
227 assertNotNull(compositeNode);
229 Set<Module> modules = TestUtils.loadModulesFrom("/json-to-cnsn/identityref");
230 assertEquals(2, modules.size());
232 TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont");
234 assertEquals("cont", compositeNode.getNodeType().getLocalName());
236 List<Node<?>> childs = compositeNode.getChildren();
237 assertEquals(1, childs.size());
238 Node<?> nd = childs.iterator().next();
239 assertTrue(nd instanceof CompositeNode);
240 assertEquals("cont1", nd.getNodeType().getLocalName());
242 childs = ((CompositeNode) nd).getChildren();
243 assertEquals(4, childs.size());
244 SimpleNode<?> lf11 = null;
245 SimpleNode<?> lf12 = null;
246 SimpleNode<?> lf13 = null;
247 SimpleNode<?> lf14 = null;
248 for (Node<?> child : childs) {
249 assertTrue(child instanceof SimpleNode);
250 if (child.getNodeType().getLocalName().equals("lf11")) {
251 lf11 = (SimpleNode<?>) child;
252 } else if (child.getNodeType().getLocalName().equals("lf12")) {
253 lf12 = (SimpleNode<?>) child;
254 } else if (child.getNodeType().getLocalName().equals("lf13")) {
255 lf13 = (SimpleNode<?>) child;
256 } else if (child.getNodeType().getLocalName().equals("lf14")) {
257 lf14 = (SimpleNode<?>) child;
261 assertTrue(lf11.getValue() instanceof QName);
262 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
263 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
265 assertTrue(lf12.getValue() instanceof QName);
266 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
267 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
269 assertTrue(lf13.getValue() instanceof QName);
270 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
271 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
273 assertTrue(lf14.getValue() instanceof QName);
274 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
275 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
280 public void loadDataAugmentedSchemaMoreEqualNamesTest() {
281 boolean exceptionCaught = false;
283 loadAndNormalizeData("/common/augment/json/dataa.json", "/common/augment/yang", "cont", "main");
284 loadAndNormalizeData("/common/augment/json/datab.json", "/common/augment/yang", "cont", "main");
285 } catch (ResponseException e) {
286 exceptionCaught = true;
289 assertFalse(exceptionCaught);
292 private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
294 CompositeNode compNode = loadAndNormalizeData(jsonPath, yangPath, topLevelElementName, moduleName);
295 verifyCompositeNode(compNode, namespace);
298 private CompositeNode loadAndNormalizeData(String jsonPath, String yangPath, String topLevelElementName, String moduleName) {
299 CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE);
300 assertNotNull(compositeNode);
302 Set<Module> modules = null;
303 modules = TestUtils.loadModulesFrom(yangPath);
304 assertNotNull(modules);
306 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName);
308 assertTrue(compositeNode instanceof CompositeNodeWrapper);
309 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
311 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
315 private void verityMultipleItemsInList(CompositeNode compositeNode) {
316 List<Node<?>> childrenNodes = compositeNode.getChildren();
317 assertEquals(4, childrenNodes.size());
318 boolean lf11Found = false;
319 boolean cont11Found = false;
320 boolean lst11Found = false;
321 for (Node<?> lst1Item : childrenNodes) {
322 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
323 assertTrue(lst1Item instanceof CompositeNode);
325 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
326 assertEquals(1, childrenLst1.size());
327 String localName = childrenLst1.get(0).getNodeType().getLocalName();
328 if (localName.equals("lf11")) {
329 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
331 } else if (localName.equals("lflst11")) {
332 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
333 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
335 } else if (localName.equals("cont11")) {
336 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
338 } else if (localName.equals("lst11")) {
340 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
341 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
345 assertTrue(lf11Found);
346 assertTrue(cont11Found);
347 assertTrue(lst11Found);
350 private void verifyCompositeNode(CompositeNode compositeNode, String namespace) {
351 boolean cont1Found = false;
352 boolean lst1Found = false;
353 boolean lflst1_1Found = false;
354 boolean lflst1_2Found = false;
355 boolean lf1Found = false;
357 // assertEquals(namespace,
358 // compositeNode.getNodeType().getNamespace().toString());
360 for (Node<?> node : compositeNode.getChildren()) {
361 if (node.getNodeType().getLocalName().equals("cont1")) {
362 if (node instanceof CompositeNode) {
364 assertEquals(0, ((CompositeNode) node).getChildren().size());
366 } else if (node.getNodeType().getLocalName().equals("lst1")) {
367 if (node instanceof CompositeNode) {
369 assertEquals(0, ((CompositeNode) node).getChildren().size());
371 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
372 if (node instanceof SimpleNode) {
373 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
374 lflst1_1Found = true;
375 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
376 lflst1_2Found = true;
380 } else if (node.getNodeType().getLocalName().equals("lf1")) {
381 if (node instanceof SimpleNode) {
382 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
387 assertEquals(namespace, node.getNodeType().getNamespace().toString());
389 assertTrue(cont1Found);
390 assertTrue(lst1Found);
391 assertTrue(lflst1_1Found);
392 assertTrue(lflst1_2Found);
393 assertTrue(lf1Found);
397 public void unsupportedDataFormatTest() {
398 String exceptionMessage = "";
400 TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true,
401 JsonToCompositeNodeProvider.INSTANCE);
402 } catch (WebApplicationException e) {
403 exceptionMessage = e.getCause().getMessage();
405 assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));