e2621d635bc3dab04629beffcb15c3d111b9eb21
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / xml / to / cnsn / test / XmlToCnSnTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
9
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;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
20 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
21 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.Node;
24 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
25
26 public class XmlToCnSnTest extends YangAndXmlAndDataSchemaLoader {
27
28     @BeforeClass
29     public static void initialize() {
30         dataLoad("/xml-to-cnsn/leafref");
31     }
32
33     @Test
34     public void testXmlLeafrefToCnSn() {
35         CompositeNode compositeNode = TestUtils.readInputToCnSn("/xml-to-cnsn/leafref/xml/data.xml", false,
36                 XmlToCompositeNodeProvider.INSTANCE);
37         assertNotNull(compositeNode);
38         assertNotNull(dataSchemaNode);
39         TestUtils.normalizeCompositeNode(compositeNode, modules, schemaNodePath);
40
41         assertEquals("cont", compositeNode.getNodeType().getLocalName());
42
43         SimpleNode<?> lf2 = null;
44         for (Node<?> childNode : compositeNode.getValue()) {
45             if (childNode instanceof SimpleNode) {
46                 if (childNode.getNodeType().getLocalName().equals("lf2")) {
47                     lf2 = (SimpleNode<?>) childNode;
48                     break;
49                 }
50             }
51         }
52
53         assertNotNull(lf2);
54         assertTrue(lf2.getValue() instanceof String);
55         assertEquals("121", lf2.getValue());
56     }
57
58     @Test
59     public void testXmlBlankInput() throws Exception {
60         InputStream inputStream = new ByteArrayInputStream("".getBytes());
61         CompositeNode compositeNode = XmlToCompositeNodeProvider.INSTANCE.readFrom(null, null, null, null, null,
62                 inputStream);
63
64         assertNull(compositeNode);
65     }
66
67     @Test
68     public void testXmlBlankInputUnmarkableStream() throws Exception {
69         InputStream inputStream = new ByteArrayInputStream("".getBytes()) {
70             @Override
71             public boolean markSupported() {
72                 return false;
73             }
74         };
75         CompositeNode compositeNode = XmlToCompositeNodeProvider.INSTANCE.readFrom(null, null, null, null, null,
76                 inputStream);
77
78         assertNull(compositeNode);
79     }
80
81 }