2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
18 import java.util.List;
20 import javax.xml.stream.XMLStreamException;
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;
29 * The class <code>XmlTreeBuilderTest</code> contains tests for the class
30 * {@link <code>XmlTreeBuilder</code>}
32 * @author Lukas Sedlak
34 public class XmlTreeBuilderTest {
36 private InputStream inputStream;
39 * Perform pre-test initialization
44 public void setUp() throws Exception {
45 final URI inputFile = getClass().getResource("/rpc-getDeviceEquipment.xml").toURI();
46 inputStream = new FileInputStream(new File(inputFile));
50 * Run the Node<?> buildDataTree(InputStream) method test
52 @SuppressWarnings("unchecked")
54 public void testBuildDataTree()
56 Node<?> rootNode = null;
58 rootNode = XmlTreeBuilder.buildDataTree(inputStream);
59 } catch (XMLStreamException e) {
62 assertNotNull(rootNode);
63 assertTrue(rootNode instanceof CompositeNode);
65 CompositeNode compRootNode = (CompositeNode)rootNode;
66 assertNotNull(compRootNode.getValue());
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;
79 } else if (childNode instanceof CompositeNode) {
80 params = (CompositeNode) childNode;
84 assertNotNull(methodName);
85 assertNotNull(params);
86 assertTrue(emptyTag.getValue().isEmpty());
87 assertEquals(methodName.getValue(), "getDeviceEquipment");
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);
95 assertTrue(valueNode instanceof CompositeNode);
96 final CompositeNode value = (CompositeNode) valueNode;
97 final Node<?> stringNode = value.getValue().get(0);
98 assertTrue(stringNode instanceof SimpleNode);
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();
109 assertNotNull(deviceId);
110 assertNotNull(deviceIP);
114 public void nodeMapInCompositeNodeTest() {
115 Node<?> rootNode = null;
117 rootNode = XmlTreeBuilder.buildDataTree(inputStream);
118 } catch (XMLStreamException e) {
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());