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