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