Merge "BUG-1382: eliminate QName.getPrefix()"
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / XmlTreeBuilderTest.java
1 /*
2  * Copyright (c) 2013 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.yangtools.yang.data.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.File;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17 import java.net.URI;
18 import java.util.List;
19
20 import javax.xml.stream.XMLStreamException;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.Node;
26 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
27
28 /**
29  * The class <code>XmlTreeBuilderTest</code> contains tests for the class
30  * {@link <code>XmlTreeBuilder</code>}
31  *
32  * @author Lukas Sedlak
33  */
34 public class XmlTreeBuilderTest {
35
36         private InputStream inputStream;
37
38         /**
39          * Perform pre-test initialization
40          *
41          * @throws Exception
42          */
43         @Before
44         public void setUp() throws Exception {
45                 final URI inputFile = getClass().getResource("/rpc-getDeviceEquipment.xml").toURI();
46                 inputStream = new FileInputStream(new File(inputFile));
47         }
48
49         /**
50          * Run the Node<?> buildDataTree(InputStream) method test
51          */
52         @SuppressWarnings("unchecked")
53         @Test
54         public void testBuildDataTree()
55         {
56                 Node<?> rootNode = null;
57                 try {
58                         rootNode = XmlTreeBuilder.buildDataTree(inputStream);
59                 } catch (XMLStreamException e) {
60                         e.printStackTrace();
61                 }
62                 assertNotNull(rootNode);
63                 assertTrue(rootNode instanceof CompositeNode);
64
65                 CompositeNode compRootNode = (CompositeNode)rootNode;
66                 assertNotNull(compRootNode.getValue());
67
68                 SimpleNode<String> methodName = null;
69                 SimpleNode<String> emptyTag = null;
70                 CompositeNode params = null;
71                 for (final Node<?> childNode : compRootNode.getValue()) {
72                         if (childNode instanceof SimpleNode) {
73                                 if ("emptyTag".equals(childNode.getNodeType().getLocalName())) {
74                                         emptyTag = (SimpleNode<String>) childNode;
75                                 } else if ("methodName".equals(childNode.getNodeType().getLocalName())) {
76                                         methodName = (SimpleNode<String>) childNode;
77                                 }
78
79                         } else if (childNode instanceof CompositeNode) {
80                                 params = (CompositeNode) childNode;
81                         }
82                 }
83
84                 assertNotNull(methodName);
85                 assertNotNull(params);
86                 assertTrue(emptyTag.getValue().isEmpty());
87                 assertEquals(methodName.getValue(), "getDeviceEquipment");
88
89                 String deviceId = null;
90                 String deviceIP = null;
91                 for (final Node<?> param : params.getValue()) {
92                         if (param instanceof CompositeNode) {
93                                 final Node<?> valueNode = ((CompositeNode) param).getValue().get(0);
94
95                                 assertTrue(valueNode instanceof CompositeNode);
96                                 final CompositeNode value = (CompositeNode) valueNode;
97                                 final Node<?> stringNode = value.getValue().get(0);
98                                 assertTrue(stringNode instanceof SimpleNode);
99
100                                 final SimpleNode<String> string = (SimpleNode<String>) stringNode;
101                                 if ("DeviceID123".equals(string.getValue())) {
102                                         deviceId = string.getValue();
103                                 } else if ("172.23.218.75".equals(string.getValue())) {
104                                         deviceIP = string.getValue();
105                                 }
106                         }
107                 }
108
109                 assertNotNull(deviceId);
110                 assertNotNull(deviceIP);
111         }
112
113         @Test
114         public void nodeMapInCompositeNodeTest() {
115             Node<?> rootNode = null;
116             try {
117                     rootNode = XmlTreeBuilder.buildDataTree(inputStream);
118             } catch (XMLStreamException e) {
119                     e.printStackTrace();
120             }
121
122             CompositeNode compRootNode = (CompositeNode)rootNode;
123             List<CompositeNode> params = compRootNode.getCompositesByName("params");
124             assertEquals(1, params.size());
125             List<CompositeNode> compositesByName = params.get(0).getCompositesByName("param");
126             assertEquals(2, compositesByName.size());
127         }
128 }