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;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
17 import javax.xml.stream.XMLStreamException;
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;
26 * The class <code>XmlTreeBuilderTest</code> contains tests for the class
27 * {@link <code>XmlTreeBuilder</code>}
29 * @author Lukas Sedlak
31 public class XmlTreeBuilderTest {
33 private InputStream inputStream;
36 * Perform pre-test initialization
41 public void setUp() throws Exception {
42 final String inputFile = getClass().getResource("/rpc-getDeviceEquipment.xml").getPath();
43 inputStream = new FileInputStream(inputFile);
47 * Run the Node<?> buildDataTree(InputStream) method test
49 @SuppressWarnings("unchecked")
51 public void testBuildDataTree()
53 Node<?> rootNode = null;
55 rootNode = XmlTreeBuilder.buildDataTree(inputStream);
56 } catch (XMLStreamException e) {
59 assertNotNull(rootNode);
60 assertTrue(rootNode instanceof CompositeNode);
62 CompositeNode compRootNode = (CompositeNode)rootNode;
63 assertNotNull(compRootNode.getChildren());
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;
76 } else if (childNode instanceof CompositeNode) {
77 params = (CompositeNode) childNode;
81 assertNotNull(methodName);
82 assertNotNull(params);
83 assertTrue(emptyTag.getValue().isEmpty());
84 assertEquals(methodName.getValue(), "getDeviceEquipment");
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);
92 assertTrue(valueNode instanceof CompositeNode);
93 final CompositeNode value = (CompositeNode) valueNode;
94 final Node<?> stringNode = value.getChildren().get(0);
95 assertTrue(stringNode instanceof SimpleNode);
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();
106 assertNotNull(deviceId);
107 assertNotNull(deviceIP);