BUG 1082 Migrate sal-rest-connector to Async Data Broker API
[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         Node<?> node = TestUtils.readInputToCnSn("/xml-to-cnsn/leafref/xml/data.xml", false,
36                 XmlToCompositeNodeProvider.INSTANCE);
37         assertTrue(node instanceof CompositeNode);
38         CompositeNode compositeNode = (CompositeNode)node;
39
40
41         assertNotNull(dataSchemaNode);
42         TestUtils.normalizeCompositeNode(compositeNode, modules, schemaNodePath);
43
44         assertEquals("cont", compositeNode.getNodeType().getLocalName());
45
46         SimpleNode<?> lf2 = null;
47         for (Node<?> childNode : compositeNode.getValue()) {
48             if (childNode instanceof SimpleNode) {
49                 if (childNode.getNodeType().getLocalName().equals("lf2")) {
50                     lf2 = (SimpleNode<?>) childNode;
51                     break;
52                 }
53             }
54         }
55
56         assertNotNull(lf2);
57         assertTrue(lf2.getValue() instanceof String);
58         assertEquals("121", lf2.getValue());
59     }
60
61     @Test
62     public void testXmlBlankInput() throws Exception {
63         InputStream inputStream = new ByteArrayInputStream("".getBytes());
64         Node<?> node =
65                 XmlToCompositeNodeProvider.INSTANCE.readFrom(null, null, null, null, null, inputStream);
66
67         assertNull( node );
68     }
69
70     @Test
71     public void testXmlBlankInputUnmarkableStream() throws Exception {
72         InputStream inputStream = new ByteArrayInputStream("".getBytes()) {
73             @Override
74             public boolean markSupported() {
75                 return false;
76             }
77         };
78         Node<?> node =
79                 XmlToCompositeNodeProvider.INSTANCE.readFrom(null, null, null, null, null, inputStream);
80
81         assertNull( node );
82     }
83
84 }