Added XmlTreeBuilder into yang-data-impl
[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.FileInputStream;
15 import java.io.InputStream;
16
17 import javax.xml.stream.XMLStreamException;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24
25 /**
26  * The class <code>XmlTreeBuilderTest</code> contains tests for the class
27  * {@link <code>XmlTreeBuilder</code>}
28  *
29  * @author Lukas Sedlak
30  */
31 public class XmlTreeBuilderTest {
32
33         private InputStream inputStream;
34         
35         /**
36          * Perform pre-test initialization
37          * 
38          * @throws Exception
39          */
40         @Before
41         public void setUp() throws Exception {
42                 final String inputFile = getClass().getResource("/rpc-getDeviceEquipment.xml").getPath();
43                 inputStream = new FileInputStream(inputFile);
44         }
45
46         /**
47          * Run the Node<?> buildDataTree(InputStream) method test
48          */
49         @SuppressWarnings("unchecked")
50         @Test
51         public void testBuildDataTree()
52         {
53                 Node<?> rootNode = null;
54                 try {
55                         rootNode = XmlTreeBuilder.buildDataTree(inputStream);
56                 } catch (XMLStreamException e) {
57                         e.printStackTrace();
58                 }
59                 assertNotNull(rootNode);
60                 assertTrue(rootNode instanceof CompositeNode);
61                 
62                 CompositeNode compRootNode = (CompositeNode)rootNode;
63                 assertNotNull(compRootNode.getChildren());
64                 
65                 SimpleNode<String> methodName = null;
66                 SimpleNode<String> emptyTag = null;
67                 CompositeNode params = null;
68                 for (final Node<?> childNode : compRootNode.getChildren()) {
69                         if (childNode instanceof SimpleNode) {
70                                 if ("emptyTag".equals(childNode.getNodeType().getLocalName())) {
71                                         emptyTag = (SimpleNode<String>) childNode;
72                                 } else if ("methodName".equals(childNode.getNodeType().getLocalName())) {
73                                         methodName = (SimpleNode<String>) childNode;
74                                 }
75                                 
76                         } else if (childNode instanceof CompositeNode) {
77                                 params = (CompositeNode) childNode;
78                         }
79                 }
80                 
81                 assertNotNull(methodName);
82                 assertNotNull(params);
83                 assertTrue(emptyTag.getValue().isEmpty());
84                 assertEquals(methodName.getValue(), "getDeviceEquipment");
85                 
86                 String deviceId = null;
87                 String deviceIP = null;
88                 for (final Node<?> param : params.getChildren()) {
89                         if (param instanceof CompositeNode) {
90                                 final Node<?> valueNode = ((CompositeNode) param).getChildren().get(0);
91                                 
92                                 assertTrue(valueNode instanceof CompositeNode);
93                                 final CompositeNode value = (CompositeNode) valueNode;
94                                 final Node<?> stringNode = value.getChildren().get(0);
95                                 assertTrue(stringNode instanceof SimpleNode);
96                                 
97                                 final SimpleNode<String> string = (SimpleNode<String>) stringNode;
98                                 if ("DeviceID123".equals(string.getValue())) {
99                                         deviceId = string.getValue();
100                                 } else if ("172.23.218.75".equals(string.getValue())) {
101                                         deviceIP = string.getValue();
102                                 }
103                         }
104                 }
105                 
106                 assertNotNull(deviceId);
107                 assertNotNull(deviceIP);
108         }
109 }