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.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17 import java.util.HashSet;
18 import java.util.List;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
23 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
24 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import org.opendaylight.yangtools.yang.data.api.Node;
29 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class JsonToCnSnTest {
36 private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class);
39 public void simpleListTest() {
40 simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1",
45 public void simpleContainerTest() {
46 simpleTest("/json-to-cnsn/simple-container.json", "/json-to-cnsn/simple-container-yang", "cont",
47 "simple:container:yang", "simple-container-yang");
51 * test if for every leaf list item is simple node instance created
54 public void multipleItemsInLeafList() {
55 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true,
56 JsonToCompositeNodeProvider.INSTANCE);
57 assertNotNull(compositeNode);
58 assertEquals(3, compositeNode.getValue().size());
60 boolean lflst1_1 = false;
61 boolean lflst1_2 = false;
62 boolean lflst1_3 = false;
64 for (Node<?> node : compositeNode.getValue()) {
65 assertEquals("lflst1", node.getNodeType().getLocalName());
66 assertTrue(node instanceof SimpleNode<?>);
67 SimpleNode<?> simpleNode = (SimpleNode<?>) node;
68 if (simpleNode.getValue().equals("45")) {
70 } else if (simpleNode.getValue().equals("55")) {
72 } else if (simpleNode.getValue().equals("66")) {
84 * List contains 4 items and in every item are other elements. It is supposed that there should be: lf11, lflst11,
88 public void multipleItemsInListTest() {
89 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true,
90 JsonToCompositeNodeProvider.INSTANCE);
92 assertNotNull(compositeNode);
93 assertEquals("lst", compositeNode.getNodeType().getLocalName());
95 verityMultipleItemsInList(compositeNode);
99 public void nullArrayToSimpleNodeWithNullValueTest() {
100 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true,
101 JsonToCompositeNodeProvider.INSTANCE);
102 assertNotNull(compositeNode);
103 assertEquals("cont", compositeNode.getNodeType().getLocalName());
105 assertNotNull(compositeNode.getValue());
106 assertEquals(1, compositeNode.getValue().size());
107 Node<?> lfNode = compositeNode.getValue().iterator().next();
109 assertTrue(lfNode instanceof SimpleNode<?>);
110 assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
115 public void incorrectTopLevelElementsTest() {
116 RestconfDocumentedException cause1 = null;
119 .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE);
120 } catch (RestconfDocumentedException e) {
124 assertNotNull(cause1);
130 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
132 RestconfDocumentedException cause2 = null;
135 .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE);
136 } catch (RestconfDocumentedException e) {
139 assertNotNull(cause2);
140 assertTrue(cause2.getErrors().get(0).getErrorMessage().contains("Json Object should contain one element"));
142 RestconfDocumentedException cause3 = null;
146 .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE);
147 } catch (RestconfDocumentedException e) {
150 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 created in composite node. if leaf with no
162 * 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.getValue();
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 (RestconfDocumentedException e) {
182 reason = e.getErrors().get(0).getErrorMessage();
185 assertTrue(reason.contains("Expected value at line"));
190 public void testJsonBlankInput() throws Exception {
191 InputStream inputStream = new ByteArrayInputStream("".getBytes());
192 CompositeNode compositeNode = JsonToCompositeNodeProvider.INSTANCE.readFrom(null, null, null, null, null,
194 assertNull(compositeNode);
198 * Tests whether namespace <b>stay unchanged</b> if concrete values are present in composite or simple node and if
199 * the method for update is called.
203 public void notSupplyNamespaceIfAlreadySupplied() {
205 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false,
206 JsonToCompositeNodeProvider.INSTANCE);
207 assertNotNull(compositeNode);
209 // supplement namespaces according to first data schema -
210 // "simple:data:types1"
211 Set<Module> modules1 = new HashSet<>();
212 Set<Module> modules2 = new HashSet<>();
213 modules1 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/1");
214 modules2 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/2");
215 assertNotNull(modules1);
216 assertNotNull(modules2);
218 TestUtils.normalizeCompositeNode(compositeNode, modules1, "simple-list-yang1:lst");
220 assertTrue(compositeNode instanceof CompositeNodeWrapper);
221 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
223 assertEquals("lst", compNode.getNodeType().getLocalName());
224 verifyCompositeNode(compNode, "simple:list:yang1");
226 TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst");
227 verifyCompositeNode(compNode, "simple:list:yang1");
231 public void jsonIdentityrefToCompositeNode() {
232 CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false,
233 JsonToCompositeNodeProvider.INSTANCE);
234 assertNotNull(compositeNode);
236 Set<Module> modules = TestUtils.loadModulesFrom("/json-to-cnsn/identityref");
237 assertEquals(2, modules.size());
239 TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont");
241 assertEquals("cont", compositeNode.getNodeType().getLocalName());
243 List<Node<?>> childs = compositeNode.getValue();
244 assertEquals(1, childs.size());
245 Node<?> nd = childs.iterator().next();
246 assertTrue(nd instanceof CompositeNode);
247 assertEquals("cont1", nd.getNodeType().getLocalName());
249 childs = ((CompositeNode) nd).getValue();
250 assertEquals(4, childs.size());
251 SimpleNode<?> lf11 = null;
252 SimpleNode<?> lf12 = null;
253 SimpleNode<?> lf13 = null;
254 SimpleNode<?> lf14 = null;
255 for (Node<?> child : childs) {
256 assertTrue(child instanceof SimpleNode);
257 if (child.getNodeType().getLocalName().equals("lf11")) {
258 lf11 = (SimpleNode<?>) child;
259 } else if (child.getNodeType().getLocalName().equals("lf12")) {
260 lf12 = (SimpleNode<?>) child;
261 } else if (child.getNodeType().getLocalName().equals("lf13")) {
262 lf13 = (SimpleNode<?>) child;
263 } else if (child.getNodeType().getLocalName().equals("lf14")) {
264 lf14 = (SimpleNode<?>) child;
268 assertTrue(lf11.getValue() instanceof QName);
269 assertEquals("iden", ((QName) lf11.getValue()).getLocalName());
270 assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString());
272 assertTrue(lf12.getValue() instanceof QName);
273 assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName());
274 assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString());
276 assertTrue(lf13.getValue() instanceof QName);
277 assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName());
278 assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString());
280 assertTrue(lf14.getValue() instanceof QName);
281 assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName());
282 assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString());
287 public void loadDataAugmentedSchemaMoreEqualNamesTest() {
288 loadAndNormalizeData("/common/augment/json/dataa.json", "/common/augment/yang", "cont", "main");
289 loadAndNormalizeData("/common/augment/json/datab.json", "/common/augment/yang", "cont", "main");
293 private void simpleTest(final String jsonPath, final String yangPath, final String topLevelElementName,
294 final String namespace, final String moduleName) {
295 CompositeNode compNode = loadAndNormalizeData(jsonPath, yangPath, topLevelElementName, moduleName);
296 verifyCompositeNode(compNode, namespace);
299 private CompositeNode loadAndNormalizeData(final String jsonPath, final String yangPath,
300 final String topLevelElementName, final String moduleName) {
301 CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE);
302 assertNotNull(compositeNode);
304 Set<Module> modules = null;
305 modules = TestUtils.loadModulesFrom(yangPath);
306 assertNotNull(modules);
308 TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName);
310 assertTrue(compositeNode instanceof CompositeNodeWrapper);
311 CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
313 assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
317 private void verityMultipleItemsInList(final CompositeNode compositeNode) {
318 List<Node<?>> childrenNodes = compositeNode.getValue();
319 assertEquals(4, childrenNodes.size());
320 boolean lf11Found = false;
321 boolean cont11Found = false;
322 boolean lst11Found = false;
323 for (Node<?> lst1Item : childrenNodes) {
324 assertEquals("lst1", lst1Item.getNodeType().getLocalName());
325 assertTrue(lst1Item instanceof CompositeNode);
327 List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getValue();
328 assertEquals(1, childrenLst1.size());
329 String localName = childrenLst1.get(0).getNodeType().getLocalName();
330 if (localName.equals("lf11")) {
331 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
333 } else if (localName.equals("lflst11")) {
334 assertTrue(childrenLst1.get(0) instanceof SimpleNode);
335 assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
337 } else if (localName.equals("cont11")) {
338 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
340 } else if (localName.equals("lst11")) {
342 assertTrue(childrenLst1.get(0) instanceof CompositeNode);
343 assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getValue().size());
347 assertTrue(lf11Found);
348 assertTrue(cont11Found);
349 assertTrue(lst11Found);
352 private void verifyCompositeNode(final CompositeNode compositeNode, final String namespace) {
353 boolean cont1Found = false;
354 boolean lst1Found = false;
355 boolean lflst1_1Found = false;
356 boolean lflst1_2Found = false;
357 boolean lf1Found = false;
359 // assertEquals(namespace,
360 // compositeNode.getNodeType().getNamespace().toString());
362 for (Node<?> node : compositeNode.getValue()) {
363 if (node.getNodeType().getLocalName().equals("cont1")) {
364 if (node instanceof CompositeNode) {
366 assertEquals(0, ((CompositeNode) node).getValue().size());
368 } else if (node.getNodeType().getLocalName().equals("lst1")) {
369 if (node instanceof CompositeNode) {
371 assertEquals(0, ((CompositeNode) node).getValue().size());
373 } else if (node.getNodeType().getLocalName().equals("lflst1")) {
374 if (node instanceof SimpleNode) {
375 if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
376 lflst1_1Found = true;
377 } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
378 lflst1_2Found = true;
382 } else if (node.getNodeType().getLocalName().equals("lf1")) {
383 if (node instanceof SimpleNode) {
384 if (((SimpleNode<?>) node).getValue().equals("lf1")) {
389 assertEquals(namespace, node.getNodeType().getNamespace().toString());
391 assertTrue(cont1Found);
392 assertTrue(lst1Found);
393 assertTrue(lflst1_1Found);
394 assertTrue(lflst1_2Found);
395 assertTrue(lf1Found);
399 public void unsupportedDataFormatTest() {
400 String exceptionMessage = "";
402 TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true,
403 JsonToCompositeNodeProvider.INSTANCE);
404 } catch (RestconfDocumentedException e) {
405 exceptionMessage = e.getErrors().get(0).getErrorMessage();
407 assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));